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

import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.sespay.vo.SespayVo;
import com.brilliance.isc.common.setgll.vo.SetgllVo;
import com.brilliance.isc.common.transaction.newcheck.AbstractModuleValidator;
import com.brilliance.isc.common.transaction.newcheck.ValidResult;
import com.brilliance.isc.common.transaction.newcheck.Validate;
import com.brilliance.isc.common.trndoc.service.DoceotService;
import com.brilliance.isc.common.trnmod.service.TrnmodService;
import com.brilliance.isc.utils.StringUtils;
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.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class SespayValidator extends AbstractModuleValidator<SespayVo> {

    @Autowired
    private TrnmodService trnmodService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Autowired
    private DoceotService doceotService;

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

    /**
     *   source:sespay.0008.script
     */
    @Validate(path="setmod.setglg.setgll.%s.sespay.intacc")
    public ValidResult checkIntacc(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        if(!MdaUtils.isEmpty(sespayVo.getIntacc()) && !ptsptaToolComponent.ptsIsRolSet(setgllVo.getSetgrp().getTmpint())) {
            return ValidResult.build(false, "Please enter Intermediary or clear the account!");
        }
        return null;
    }

    /**
     *   source:sespay.0009.script
     */
    @Validate(path="setmod.setglg.setgll.%s.sespay.rcvcoracc")
    public ValidResult checkRcvcoracc(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        if(!MdaUtils.isEmpty(sespayVo.getRcvcoracc()) && !ptsptaToolComponent.ptsIsRolSet(setgllVo.getSetgrp().getRcvcor())) {
            return ValidResult.build(false, "Please enter Receiver′s Correspondent or clear the account!");
        }
        return null;
    }

    /**
     *   source:sespay.0010.script
     */
    @Validate(path="setmod.setglg.setgll.%s.sespay.triacc")
    public ValidResult checkTriacc(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        if(!MdaUtils.isEmpty(sespayVo.getTriacc()) && !ptsptaToolComponent.ptsIsRolSet(setgllVo.getSetgrp().getTri())) {
            return ValidResult.build(false, "Please enter 3rd. Reimbursement Institution or clear the account!");
        }
        return null;
    }

    /**
     *   source:sespay.0011.script
     */
    @Validate(path="setmod.setglg.setgll.%s.sespay.benacc")
    public ValidResult checkBenacc(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        if(!MdaUtils.isEmpty(sespayVo.getBenacc()) && !ptsptaToolComponent.ptsIsRolSet(setgllVo.getSetgrp().getRms())) {
            return ValidResult.build(false, "Please enter Beneficiary or clear the account!");
        }
        return null;
    }

    /**
     *   source:sespay.0012.script
     */
    @Validate(path="setmod.setglg.setgll.%s.sespay.beiacc")
    public ValidResult checkBeiacc(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        if(!MdaUtils.isEmpty(sespayVo.getBeiacc()) && !ptsptaToolComponent.ptsIsRolSet(setgllVo.getSetgrp().getBei())) {
            return ValidResult.build(false, "Please enter Beneficiary Institution or clear the account!");
        }
        return null;
    }

    /**
     *   source:sespay.0026.script
     */
    @Validate(path="setmod.setglg.setgll.%s.setgrp.actser.pts.extkey")
    public ValidResult checkActserpPtsptaPtsExtkeyN1000(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        switch(setgllVo.getSftmt()) {
            case "400":
                if(MdaUtils.isEmpty(setgllVo.getSetgrp().getActser().getPts().getExtkey()) && !MdaUtils.isEmpty(setgllVo.getAccamt())) {
                    //有57A时才为必输项
                    if(!MdaUtils.isEmpty(setgllVo.getSetgrp().getAwi().getPts().getExtkey())) {
                        return ValidResult.build(false, "MT400报文的57有值，因此53，54为必输");
                    }
                }
                break;
        }
        return null;
    }

    /**
     *   source:sespay.0027.script
     */
    @Validate(path="setmod.setglg.setgll.%s.sespay.strinf")
    public ValidResult checkStrinf(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        //---SWT2021---
        // check field for codes of Tag72/ MT103
        switch(setgllVo.getSftmt()) {
            case "103":
                if (StringUtils.isNotEmpty(sespayVo.getStrinf())) {
                    String errorMsg = trnmodService.checkSWTCode(sespayVo.getStrinf(), true, "72", "103", true, "", 6, 35);
                    if (StringUtils.isNotEmpty(errorMsg)) {
                        return ValidResult.build(false, errorMsg);
                    }
                    String regex = "(.*)/(ACC|INS)/(.+)";
                    Pattern p = Pattern.compile(regex);
                    String[] split = sespayVo.getStrinf().split("[\\r\\n]{1,2}");
                    for (String s : split) {
                        String s1 = s.toUpperCase();
                        int i = s1.indexOf("/ACC/");
                        int j = s1.indexOf("/INS/");
                        if (i > -1 || j > -1) {
                            Matcher matcher = p.matcher(s1);
                            if (!matcher.find()) {
                                return ValidResult.build(false, "非独立短语["+s+"]后必须有内容");
                            }
                        }
                    }
                }
                break;
        }
        return null;
    }

    @Validate(path = "setmod.setglg.setgll.%s.sespay.strinf202")
    public ValidResult checkStrinf202(SespayVo sespayVo) {
        SetgllVo setgllVo = (SetgllVo)getParent();
        if ("756".equals(setgllVo.getSftmt()) || "202".equals(setgllVo.getSftmt())) {
            if (StringUtils.isNotEmpty(setgllVo.getSespay().getStrinf202())) {
                String errorMsg = trnmodService.checkSWTCode(sespayVo.getStrinf202(), true, "72", "202", true, "", 6, 35);
                if (StringUtils.isNotEmpty(errorMsg)) {
                    return ValidResult.build(false, errorMsg);
                }
                String regex = "(.*)/(ACC|INS)/(.+)";
                Pattern p = Pattern.compile(regex);
                String[] split = sespayVo.getStrinf202().split("[\\r\\n]{1,2}");
                for (String s : split) {
                    String s1 = s.toUpperCase();
                    int i = s1.indexOf("/ACC/");
                    int j = s1.indexOf("/INS/");
                    if (i > -1 || j > -1) {
                        Matcher matcher = p.matcher(s1);
                        if (!matcher.find()) {
                            return ValidResult.build(false, "非独立短语["+s+"]后必须有内容");
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     *   source:sespay.0031.script
     */
    @Validate(path="setmod.setglg.setgll.%s.setgrp.actser.pts.extkey")
    public ValidResult checkActserpPtsptaPtsExtkeyN1100(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        if(!MdaUtils.isEmpty(setgllVo.getSetgrp().getActser().getPts().getExtkey())) {
            if( MdaUtils.compareTo(setgllVo.getSetgrp().getActser().getPts().getExtkey(), setgllVo.getSetgrp().getBei().getPts().getExtkey()) == 0
                    && MdaUtils.isEmpty(setgllVo.getDirindflg())) {
                return ValidResult.build(false, "For indirect payments Beneficiary Institution and Senders Correspondent may not be the same!");
            }
        }
        return null;
    }

    /**
     *   source:sespay.0036.script
     */
    @Validate(path="setmod.setglg.setgll.%s.setgrp.revcor2.pts.extkey")
    public ValidResult checkRcvcorpPtsptaPtsExtkeyN2000(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        if(MdaUtils.compareTo(setgllVo.getSftmt(), "400") == 0) {
            if(!MdaUtils.isEmpty(setgllVo.getSetgrp().getAwi().getPts().getExtkey())
                    && MdaUtils.isEmpty(setgllVo.getSetgrp().getRcvcor2().getPts().getExtkey()) ) {
                return ValidResult.build(false, "MT400报文的57有值，因此53，54为必输");
            }
        }
        return null;
    }

    /**
     *   source:sespay.0040.script
     */
    @Validate(path = "setmod.setglg.setgll.%s.sespay.triacc")
    public ValidResult checkTripPtsptaPtsExtkey(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        //cover 202报文的56，57check刚好相反
        if( MdaUtils.compareTo(setgllVo.getSftmt(),"756") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"400") == 0 || xxxsetFlag(setgllVo.getSftmt())) {
            if(!ptsptaToolComponent.ptsIsRolSet(setgllVo.getSetgrp().getTri()) && ptsptaToolComponent.ptsIsRolSet(setgllVo.getSetgrp().getRcvcor())) {
                return ValidResult.build(false, "Please enter Account with Institut or clear Intermediary (Main)!");
            }
        }
        //---------------TESTTEST--------------
        /**
         //电汇改造：选202cov时生成103报文
         //swift 升级改造 202cover
         if SFTMT == "202" then
         //56A有值的话57A必输
         if IsEmpty( TRIP\\PTSPTA\\PTS\\EXTKEY ) and not IsEmpty( RCVCORP\\PTSPTA\\PTS\\EXTKEY ) then
         Error( 'LG000144' )
         endif
         endi**/
        return null;
    }

    /**
     *   source:sespay.0055.script
     */
    @Validate(path="setmod.setglg.setgll.%s.setgrp.int.pts.extkey", enable = "N")
    public ValidResult checkIntpPtsptaPtsExtkeyN1050(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        if ("202".equals(setgllVo.getSftmt())) {
            if (!MdaUtils.isEmpty(setgllVo.getSetgrp().getTmpint().getPts().getExtkey())) {
                if (!"A".equals(doceotService.getPtmField(setgllVo.getSetgrp().getTmpint().getPtainr(), "SWT", "AUTFLG"))) {
                    return ValidResult.build(false, "此机构与我行无密押关系，请重新输入");
                }
            }
        }
        return null;
    }

    /**
     *   source:sespay.0056.script
     */
    @Validate(path="setmod.setglg.setgll.%s.setgrp.awi.pts.extkey")
    public ValidResult checkAwipPtsptaPtsExtkeyN1050(SespayVo sespayVo){
        SetgllVo setgllVo = (SetgllVo)getParent();
        if ("CPTOPN".equals(SettleContext.getTransName())) {
            if ("103".equals(setgllVo.getSftmt()) || "202".equals(setgllVo.getSftmt())) {
                if (MdaUtils.isEmpty(setgllVo.getSetgrp().getAwi().getPts().getExtkey()) && MdaUtils.isEmpty(setgllVo.getSetgrp().getTmpint().getPts().getExtkey())) {
                    return errorMandatory();
                }
                if (MdaUtils.isEmpty(setgllVo.getSetgrp().getAwi().getPts().getExtkey()) && !MdaUtils.isEmpty(setgllVo.getSetgrp().getTmpint().getPts().getExtkey())) {
                    return ValidResult.build(false, "Please enter Account with Institut or clear Intermediary (Main)!");
                }

                if ("202".equals(setgllVo.getSftmt())) {
                    if (!MdaUtils.isEmpty(setgllVo.getSetgrp().getAwi().getPts().getExtkey()) && MdaUtils.isEmpty(setgllVo.getSetgrp().getTmpint().getPts().getExtkey())) {
                        if (!"A".equals(doceotService.getPtmField(setgllVo.getSetgrp().getAwi().getPtainr(), "SWT", "AUTFLG"))) {
                            return ValidResult.build(false, "此机构与我行无密押关系，请重新输入");
                        }
                    }
                }
            }
        }
        return null;
    }

    private boolean xxxsetFlag(String sftmt) {
        String transName = SettleContext.getTransName();
        if ("202".equals(sftmt) && ("NITSET".equals(transName) || "NITPSE".equals(transName))) {
            return true;
        }
        return false;
    }
}
