package com.brilliance.isc.common.transaction.newcheck.gedmod;

import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.gedmod.service.GedmodService;
import com.brilliance.isc.common.gedmod.vo.GedmodVo;
import com.brilliance.isc.common.transaction.newcheck.*;
import com.brilliance.isc.common.transaction.vo.BaseTransactionVo;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.mda.runtime.mda.CodetableItem;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static com.brilliance.mda.runtime.mda.Constants.NULLSTR;

@Component
public class GedmodValidator extends AbstractModuleValidator<GedmodVo> {

    @Autowired
    private GedmodService gedmodService;

    @Override
    @PostConstruct
    public void register() {
        super.register();
    }

    private static List<CodetableItem> paytypList;

    @PostConstruct
    public void initFintypList() {
        paytypList = new ArrayList<>();
        paytypList.add(new CodetableItem("信用证", "L"));
        paytypList.add(new CodetableItem("保函", "G"));
        paytypList.add(new CodetableItem("托收", "C"));
        paytypList.add(new CodetableItem("电汇", "T"));
        paytypList.add(new CodetableItem("票汇", "D"));
        paytypList.add(new CodetableItem("信汇", "M"));
        paytypList.add(new CodetableItem("其他", "O"));
        paytypList.add(new CodetableItem("电汇", "0"));
        paytypList.add(new CodetableItem("票汇", "1"));
        paytypList.add(new CodetableItem("信汇", "2"));
        paytypList.add(new CodetableItem("其他", "3"));

    }

    /**
     *   source:gedmod.0001.script
     */
    @Validate(path="gedmod.dzghxx.rmzhlx")
    public ValidResult checkDzghxxRmzhlxN1000(GedmodVo vo){
        if("1".equals(vo.getZjly())) {
            if(MdaUtils.isEmpty(vo.getDzghxx().getRmzhlx())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:gedmod.0004.script
     */
    @Validate(path="gedmod.dzghxx.ghedbh")
    public ValidResult checkDzghxxGhedbhN1000(GedmodVo vo){
        if("1".equals(vo.getZjly())) {
            if(MdaUtils.isEmpty(vo.getDzghxx().getGhedbh())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:gedmod.0008.script
     */
    @Validate(path="gedmod.dzghxx.cur")
    public ValidResult checkDzghxxCurN1000(GedmodVo vo){
        if("1".equals(vo.getZjly())) {
            if( MdaUtils.isEmpty(vo.getDzghxx().getCur()) ) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:gedmod.0009.script
     */
    @Validate(path="gedmod.dzghxx.amtgwf")
    public ValidResult checkDzghxxAmtgwfN1000(GedmodVo vo){
        if("1".equals(vo.getZjly())) {
            BaseTransactionVo baseTransactionVo = (BaseTransactionVo)getParent();
            if(MdaUtils.isEmpty(vo.getDzghxx().getAmtgwf())) {
                return errorMandatory();
            } else {
                Argument<BigDecimal> amtArg = new Argument<>();
                gedmodService.getKehuzhAndAmtwibzhh(vo, baseTransactionVo.getSetmod(), baseTransactionVo.getSetglg(), null, null, amtArg);
                if(!MdaUtils.isEmpty(amtArg.getValue())) {
                    if("JSTOPN".equals(SettleContext.getTransName())) {
                        if(MdaUtils.compareTo(amtArg.getValue(), vo.getDzghxx().getAmtgwf()) != 0 ) {
                            return ValidResult.build(false, "当前交易金额应大于等于额度编号下已购未付金额");
                        }
                    }
                }
            }
        }
        return null;
    }

    @Warning(path="gedmod.dzghxx.amtgwf")
    public WarningResult warnDzghxxAmtgwfN1000(GedmodVo vo){
        if("1".equals(vo.getZjly())) {
            BaseTransactionVo baseTransactionVo = (BaseTransactionVo)getParent();
            if(!MdaUtils.isEmpty(vo.getDzghxx().getAmtgwf())) {
                Argument<BigDecimal> amtArg = new Argument<>();
                gedmodService.getKehuzhAndAmtwibzhh(vo, baseTransactionVo.getSetmod(), baseTransactionVo.getSetglg(), null, null, amtArg);
                if(!MdaUtils.isEmpty(amtArg.getValue())) {
                    if(MdaUtils.compareTo(amtArg.getValue(), vo.getDzghxx().getAmtgwf()) > 0 ) {
                        return WarningResult.build("SUBAMTGWF", "当前交易金额大于额度编号下已购未付金额，请确定账户资金充足");
                    }
                }
            }
        }
        return WarningResult.buildEmpty("SUBAMTGWF");
    }

    /**
     *   source:gedmod.0010.script
     */
    @Validate(path="gedmod.dzghxx.edusta")
    public ValidResult checkDzghxxEdustaN1000(GedmodVo vo){
        if("1".equals(vo.getZjly())) {
            if(MdaUtils.isEmpty(vo.getDzghxx().getEdusta())) {
                return errorMandatory();
            }
        }
        return null;
    }

    /**
     *   source:gedmod.0011.script
     */
    @Validate(path="gedmod.dzghxx.wibzhh")
    public ValidResult checkDzghxxWibzhhN1000(GedmodVo vo){
        if("1".equals(vo.getZjly())) {
            if( MdaUtils.isEmpty(vo.getDzghxx().getWibzhh())) {
                return errorMandatory();
            } else {
                BaseTransactionVo baseTransactionVo = (BaseTransactionVo)getParent();
                Argument<String> accountArg = new Argument<>();
                gedmodService.getKehuzhAndAmtwibzhh(vo, baseTransactionVo.getSetmod(), baseTransactionVo.getSetglg(), accountArg, null, null);
                if(MdaUtils.compareTo(vo.getDzghxx().getWibzhh(), accountArg.getValue()) != 0 ) {
                    return ValidResult.build(false, "客户账号与外币账号不一致，请检查费用账务页面客户同币种支出账号");
                }
            }
        }
        return null;
    }

    /**
     *   source:gedmod.0014.script
     */
    @Warning(path="gedmod.dzghxx.paytyp")
    public WarningResult checkDzghxxPaytypN1000(GedmodVo vo){
        // L－信用证/G－保函/C－托收/T－电汇/D－票汇/M－信汇/O－其他
        String paytyp = NULLSTR;
        switch(SettleContext.getDirectTransName()) {
            case "CPTOPN":
            case "CPTATO":
                BaseTransactionVo baseTransactionVo = (BaseTransactionVo)getParent();
                paytyp = Utils.getFieldValue(baseTransactionVo, "cpdgrp//rec//paytype", "");
                /*switch( paytyp ) {
                    case "0":
                        paytyp = "T";
                        break;
                    case "1":
                        paytyp = "D";
                        break;
                    case "2":
                        paytyp = "L";
                        break;
                    case "3":
                        paytyp = "O";
                        break;
                }*/
                break;
            case "BRTSET":
                paytyp = "L";
                break;
            case "BCTSET":
                paytyp = "C";
                break;
            case "MCTFEE":
            case "JSTOPN":
                paytyp = "";
                break;
            default:
                paytyp = "O";
        }
        if(!MdaUtils.isEmpty(vo.getDzghxx().getPaytyp()) && !MdaUtils.isEmpty(paytyp)) {
            if(MdaUtils.compareTo(vo.getDzghxx().getPaytyp(), paytyp) != 0) {
                String warningText = "当前付汇类型为" + getPaytypName(paytyp) + ",额度编号对应付汇类型为" + getPaytypName(vo.getDzghxx().getPaytyp());
                return WarningResult.build("SUBPAYTYP", warningText);
            }
        }
        return WarningResult.buildEmpty("SUBPAYTYP");
    }

    /**
     *   source:gedmod.0016.script
     */
    @Validate(path="gedmod.dzghxx.amtunfrez")
    public ValidResult checkDzghxxAmtunfrezN1000(GedmodVo vo){
        //解冻金额由自动赋值改为界面栏位手动录入，增加校验规则
        if("1".equals(vo.getZjly())) {
            if(MdaUtils.isEmpty(vo.getDzghxx().getAmtunfrez())) {
                return errorMandatory();
            } else {
                if(MdaUtils.compareTo(vo.getDzghxx().getAmtunfrez(), vo.getDzghxx().getAmtgwf()) > 0) {
                    return ValidResult.build(false, "本次付汇金额不能大于已购未付金额");
                } else {
                    BaseTransactionVo baseTransactionVo = (BaseTransactionVo)getParent();
                    Argument<BigDecimal> amtwibzhhArg = new Argument<>();
                    gedmodService.getKehuzhAndAmtwibzhh(vo, baseTransactionVo.getSetmod(), baseTransactionVo.getSetglg(),null, amtwibzhhArg,null);
                    if(MdaUtils.compareTo(vo.getDzghxx().getAmtunfrez(), amtwibzhhArg.getValue()) > 0) {
                        return ValidResult.build(false, "本次付汇金额不能大于该账号下扣款金额累加");
                    }
                }
            }
        }
        return null;
    }

    private String getPaytypName(String paytyp) {
        Optional<CodetableItem> item = paytypList.stream().filter(f -> paytyp.equals(f.getValue())).findFirst();
        if (item.isPresent()) {
            return item.get().getLabel();
        }
        return paytyp;
    }

}
