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

import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.Decimals;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
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.Utils;
import com.brilliance.isc.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component("B_FeeCalculator")
public class BasepointFeeCalculator extends AbstractFeeCalculator{

    @Autowired
    private CodetableService codetableService;

    @Autowired
    private FecismService fecismService;

    @Autowired
    private XrtmodService xrtmodService;

    @Override
    protected 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()));
        //计算费用
        String feecur = request.getFeecur();
        String relcur = request.getRelcur();
        BigDecimal relamt = request.getRelAmt();
        BigDecimal ratuse = request.getRatcal();
        BigDecimal fecamt = new BigDecimal("0");
        BigDecimal basis = new BigDecimal("10000");
        int unt = request.getUnt();
        if (!"u".equals(request.getModflg())) {
            unt = 1;
        }
        BigDecimal lftunt = new BigDecimal(unt);
        fecamt = Decimals.add(fecamt, (Decimals.div(Decimals.mul(Decimals.mul(relamt, ratuse), lftunt), basis)));
        inftxt += CR + Strings.getText("Calculation: amount $1 $2 * rate $3 * $4 units = $1 $5", relcur, Utils.fmtAmount(relamt, relcur), ratuse, unt, Utils.fmtAmount(fecamt, feecur));
        if (MdaUtils.compareTo(relcur, feecur) != 0) {
            //费用牌价取中间价
            Argument<String> inftxtArg = new Argument<>("", "");
            fecamt = fecismService.fecIsmAmtConv(inftxtArg, relcur, fecamt, feecur, Dates.today());
            if (StringUtils.isNotEmpty(inftxtArg.value)) {
                inftxt += CR + inftxtArg.value;
            }
        }
        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);
        compareMaxmin(response, request);
        return response;
    }
}
