package com.brilliance.isc.common.fecism.calcfee.impl;

import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.*;
import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.fecism.calcfee.AbstractFeeCalculator;
import com.brilliance.isc.common.fecism.service.FecismService;
import com.brilliance.isc.common.fecism.vo.CalcFeeRequest;
import com.brilliance.isc.common.fecism.vo.CalcFeeResponse;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component("0_FeeCalculator")
public class FixedAmtFeeCalculator extends AbstractFeeCalculator {

    @Autowired
    private FecismService fecismService;

    @Autowired
    private XrtmodService xrtmodService;

    @Autowired
    private CodetableService codetableService;

    @Override
    public CalcFeeResponse calculate(CalcFeeRequest request) {
        CalcFeeResponse response = new CalcFeeResponse();
        buildInfdetstm(response, request.getRelcur(), request.getRelAmt());
        //费用计算细节
        String inftxt = buildInftxt(request);
        //inftxt += CR + Strings.getText("Calculation based on $1 i.e. $2 $3", codetableService.getTblLabel(request.getCalcbs(), "FECAMT"), request.getRelcur(), Utils.fmtAmount(request.getRelAmt(), request.getRelcur()));
        inftxt += CR + Strings.getText("计费规则是固定金额，$1 $2", request.getRatcur(), Utils.fmtAmount(xrtmodService.amtRnd(request.getUntamt(), request.getRatcur()), request.getRatcur()));
        int unt = request.getUnt();
        // calculate at least one unit if not modified
        if (unt == 0 && !request.isModified("unt")) {
            unt = 1;
        }
        BigDecimal fecamt = request.getUntamt();
        Argument<String> roundInftxtArg = new Argument<>("", "");
        fecamt = roundAmt(roundInftxtArg, fecamt, request.getRoundFlag());
        if (StringUtils.isNotEmpty(roundInftxtArg.value)) {
            inftxt += CR + roundInftxtArg.value;
        }
        String feecur = request.getFeecur();
        if (MdaUtils.compareTo(request.getRatcur(), feecur) != 0) {
            Argument<String> inftxtArg = new Argument<>("", "");
            fecamt = fecismService.fecIsmAmtConv(inftxtArg, request.getRatcur(), fecamt, feecur, Dates.today());
            if (StringUtils.isNotEmpty(inftxtArg.value)) {
                inftxt += CR + inftxtArg.value;
            }
            roundInftxtArg = new Argument<>("", "");
            fecamt = roundAmt(roundInftxtArg, fecamt, request.getRoundFlag());
            if (StringUtils.isNotEmpty(roundInftxtArg.value)) {
                inftxt += CR + roundInftxtArg.value;
            }
        }
        inftxt += CR + Strings.getText("本次收取$1单元$2", unt, codetableService.getTblLabel(request.getFeecod(), "FEETXT"));
        fecamt = Decimals.mul(fecamt, unt);
        inftxt += CR + Strings.getText("计算后应收 $1 $2", feecur, Utils.fmtAmount(xrtmodService.amtRnd(fecamt, feecur), feecur));
        BigDecimal ogiamt = xrtmodService.amtRndUnt(fecamt, feecur);
        BigDecimal amt = xrtmodService.amtRndUnt(Decimals.sub(fecamt, request.getDctamt()), feecur);
        response.setUnt(unt);
        response.setInftxt(inftxt);
        response.setOgiamt(ogiamt);
        response.setCur(feecur);
        response.setAmt(amt);
        response.setDat1(request.getDat1());
        response.setDat2(request.getDat2());
        return response;
    }
}
