package com.brilliance.isc.common.register.umd;

import com.brilliance.isc.bo.Gid;
import com.brilliance.isc.bo.Nid;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.umdmod.vo.UmdmodVo;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.GidMapper;
import com.brilliance.isc.mda.dao.NidMapper;
import com.brilliance.isc.utils.StringUtils;
import com.brilliance.isc.vo.lg.GcdBizInfoVo;
import com.brilliance.isc.vo.lg.GidBizInfoVo;
import com.brilliance.isc.vo.lg.NcdBizInfoVo;
import com.brilliance.isc.vo.lg.NidBizInfoVo;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public abstract class AbstractUmdRegister {
    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;
    @Autowired
    private GidMapper gidMapper;
    @Autowired
    private NidMapper nidMapper;

    public PtsptaVo getRol(List<PtsptaVo> ptsptas, String ptString) {
        PtsptaVo ptsptaVo = ptsptaToolComponent.getPtsFromListByRol(ptsptas, ptString);
        if (ptsptaVo == null) {
            ptsptaVo = new PtsptaVo();
        }
        PtsptaVo clonePtspta = MdaUtils.deepClone(ptsptaVo);
        //补充PTSPTA属性
        ptsptaToolComponent.fillPtsptaAllProperties(clonePtspta);
        return clonePtspta;

    }

    /**
     * 国际进口保函获取默认的bennam
     * @param umdmodVo
     * @param apl
     * @param ben
     * @param bec
     * @param gidinr
     * @param hndtyp
     */
    public void genBennamInGit(UmdmodVo umdmodVo, PtsptaVo apl, PtsptaVo ben, PtsptaVo bec, String gidinr, String hndtyp) {
        if (!umdmodVo.isModified("bennam")) {
            Gid gid = new Gid();
            gid.setInr(gidinr);
            gid = gidMapper.selectByPrimaryKey(gid);
            String bennam = "";
            if (gid != null) {
                bennam = gid.getOppnam();
            }
            boolean getFlag = false;
            if ("GITPAM|GITAME".indexOf(SettleContext.getTransName()) > -1) {
                getFlag = true;
            }
            if (StringUtils.isEmpty(bennam) || getFlag) {
                if ("OL".equals(hndtyp)) {
                    //国际直开，交易对手是受益人ben
                    if (ben != null && ben.getPts() != null) {
                        bennam = ben.getPts().getEnnam();
                    }
                } else if ("OC".equals(hndtyp)) {
                    //国际反担保反反担保，交易对手sequenceC下面受益人bec
                    if (bec != null && bec.getPts() != null) {
                        bennam = bec.getPts().getEnnam();
                    }
                } else if ("OT".equals(hndtyp)) {
                    //转开的是APL
                    if (apl != null && apl.getPts() != null) {
                        bennam = apl.getPts().getEnnam();
                    }
                }
            }
            umdmodVo.setBennam(bennam);
        }
    }


    /**
     * 国内进口保函获取默认的bennam
     * @param umdmodVo
     * @param apl
     * @param ben
     * @param nidinr
     * @param hndtyp
     * @param uil
     */
    public void genBennamInNit(UmdmodVo umdmodVo, PtsptaVo apl, PtsptaVo ben, String nidinr, String hndtyp, String uil) {
        if (!umdmodVo.isModified("bennam")) {
            Nid nid = new Nid();
            nid.setInr(nidinr);
            nid = nidMapper.selectByPrimaryKey(nid);
            String bennam = "";
            if (nid != null) {
                bennam = nid.getOppnam();
            }
            boolean getFlag = false;
            if ("NITPAM|NITAME".indexOf(SettleContext.getTransName()) > -1) {
                getFlag = true;
            }
            if (StringUtils.isEmpty(bennam) || getFlag) {
                //国内直开和反担保，交易对手是受益人ben
                if ("OL".equals(hndtyp) || "OC".equals(hndtyp)) {
                    if (ben != null && ben.getPts() != null) {
                        if ("EN".equals(uil)) {
                            bennam = ben.getPts().getEnnam();
                        } else {
                            bennam = ben.getPts().getCnnam();
                        }
                    }
                } else if ("OT".equals(hndtyp)) {
                    //国内转开，交易对手是受益人apl
                    if (apl != null && apl.getPts() != null) {
                        if ("EN".equals(uil)) {
                            bennam = apl.getPts().getEnnam();
                        } else {
                            bennam = apl.getPts().getCnnam();
                        }
                    }
                }
            }
            umdmodVo.setBennam(bennam);
        }
    }

    /**
     * 国际出口保函获取默认的bennam
     * @param umdmodVo
     * @param apl
     * @param gidinr
     */
    public void genBennamInGet(UmdmodVo umdmodVo, PtsptaVo apl, String gidinr) {
        if (!umdmodVo.isModified("bennam")) {
            Gid gid = new Gid();
            gid.setInr(gidinr);
            gid = gidMapper.selectByPrimaryKey(gid);
            String bennam = "";
            if (gid != null) {
                bennam = gid.getOppnam();
            }
            if (StringUtils.isEmpty(bennam)) {
                //转开的是APL
                if (apl != null && apl.getPts() != null) {
                    bennam = apl.getPts().getEnnam();
                }
            }
            umdmodVo.setBennam(bennam);
        }
    }


    /**
     * 国内出口保函获取默认的bennam
     * @param umdmodVo
     * @param apl
     * @param nidinr
     * @param uil
     */
    public void genBennamInNet(UmdmodVo umdmodVo, PtsptaVo apl, String nidinr, String uil) {
        if (!umdmodVo.isModified("bennam")) {
            Nid nid = new Nid();
            nid.setInr(nidinr);
            nid = nidMapper.selectByPrimaryKey(nid);
            String bennam = "";
            if (nid != null) {
                bennam = nid.getOppnam();
            }
            if (StringUtils.isEmpty(bennam)) {
                //国内转开，交易对手是受益人apl
                if (apl != null && apl.getPts() != null) {
                    if ("EN".equals(uil)) {
                        bennam = apl.getPts().getEnnam();
                    } else {
                        bennam = apl.getPts().getCnnam();
                    }
                }
            }
            umdmodVo.setBennam(bennam);
        }
    }


    /**
     * 前端交易对手名称自动回显的逻辑
     *
     * @param gidBizInfoVo
     * @param apl
     * @param ben
     * @return
     */
    public void getBennamInGIT(GidBizInfoVo gidBizInfoVo, UmdmodVo umdmodVo, PtsptaVo apl, PtsptaVo ben, PtsptaVo bec) {
        if (!umdmodVo.isModified("bennam")) {
            Gid gid = new Gid();
            gid.setInr(gidBizInfoVo.getObjinr());
            gid = gidMapper.selectByPrimaryKey(gid);
            String bennam = "";
            if (gid != null) {
                bennam = gid.getOppnam();
            }
            if (StringUtils.isEmpty(bennam)) {
                String hndtyp = gidBizInfoVo.getHndtyp();
                if ("OL".equals(hndtyp)) {
                    //国际直开，交易对手是受益人ben
                    if (ben.getPts() != null) {
                        bennam = ben.getPts().getEnnam();
                    }
                } else if ("OC".equals(hndtyp)) {
                    //国际反担保反反担保，交易对手sequenceC下面受益人bec
                    if (bec.getPts() != null) {
                        bennam = bec.getPts().getEnnam();
                    }
                } else if ("OT".equals(hndtyp)) {
                    //转开的是APL
                    if (apl.getPts() != null) {
                        bennam = apl.getPts().getEnnam();
                    }
                }
            }
            umdmodVo.setBennam(bennam);
        }
    }



    public void getBennamInGIT(GcdBizInfoVo gidBizInfoVo, UmdmodVo umdmodVo, PtsptaVo apl, PtsptaVo ben, PtsptaVo bec) {
        String bennam = umdmodVo.getBennam();

        if (StringUtils.isEmpty(bennam)) {
            Gid gid = new Gid();
            if (!StringUtils.isEmpty(gidBizInfoVo.getObjinr())) {
                gid.setInr(gidBizInfoVo.getObjinr());
                Gid gid1 = gidMapper.selectByPrimaryKey(gid);
                umdmodVo.setBennam(gid1.getOppnam());
            }
            if (MdaUtils.compareTo(gidBizInfoVo.getHndtyp(), "OL") == 0) {
                //国际直开，交易对手是受益人ben
                if (!MdaUtils.isEmpty(ben)) {
                    bennam = ben.getNam();
                }
            } else if (MdaUtils.compareTo(gidBizInfoVo.getHndtyp(), "OC") == 0) {
                //国际反担保反反担保，交易对手sequenceC下面受益人bec
                if (!MdaUtils.isEmpty(bec)) {
                    bennam = bec.getNam();
                }
            } else if (MdaUtils.compareTo(gidBizInfoVo.getHndtyp(), "OT") == 0) {
                //转开的是APL
                if (!MdaUtils.isEmpty(apl)) {
                    bennam = apl.getNam();
                }
            }
        }
        umdmodVo.setBennam(bennam);
    }

    public void getBennamInNIT(NidBizInfoVo nidBizInfoVo, UmdmodVo umdmodVo, PtsptaVo apl, PtsptaVo ben) {
        if (!umdmodVo.isModified("bennam")) {
            Nid nid = new Nid();
            nid.setInr(nidBizInfoVo.getObjinr());
            nid = nidMapper.selectByPrimaryKey(nid);
            String bennam = "";
            if (nid != null) {
                bennam = nid.getOppnam();
            }
            if (StringUtils.isEmpty(bennam)) {
                //国内直开和反担保，交易对手是受益人ben
                if ("OL".equals(nidBizInfoVo.getHndtyp()) || "OC".equals(nidBizInfoVo.getHndtyp())) {
                    if (ben != null) {
                        if ("EN".equals(nidBizInfoVo.getGiduil())) {
                            bennam = ben.getPts().getEnnam();
                        } else {
                            bennam = ben.getPts().getCnnam();
                        }
                    }
                } else if ("OT".equals(nidBizInfoVo.getHndtyp())) {
                    //国内转开，交易对手是受益人apl
                    if (apl != null) {
                        if ("EN".equals(nidBizInfoVo.getGiduil())) {
                            bennam = apl.getPts().getEnnam();
                        } else {
                            bennam = apl.getPts().getCnnam();
                        }
                    }
                }
            }
            umdmodVo.setBennam(bennam);
        }
    }

    public void getBennamInNIT(NcdBizInfoVo nidBizInfoVo, UmdmodVo umdmodVo, PtsptaVo apl, PtsptaVo ben) {
        String bennam = umdmodVo.getBennam();
        if (StringUtils.isEmpty(bennam)) {
            Nid nid = new Nid();
            if (!StringUtils.isEmpty(nidBizInfoVo.getObjinr())) {
                nid.setInr(nidBizInfoVo.getObjinr());
                Nid nid1 = nidMapper.selectByPrimaryKey(nid);
                umdmodVo.setBennam(nid1.getOppnam());
            }

            //国内直开和反担保，交易对手是受益人ben
            if (MdaUtils.compareTo(nidBizInfoVo.getHndtyp(), "OL") == 0 || MdaUtils.compareTo(nidBizInfoVo.getHndtyp(), "OC") == 0) {
                if (!MdaUtils.isEmpty(ben)) {
                    bennam = ben.getNam();
                }
            } else if (MdaUtils.compareTo(nidBizInfoVo.getHndtyp(), "OT") == 0) {
                //国内转开，交易对手apl
                if (!MdaUtils.isEmpty(apl)) {
                    bennam = apl.getNam();
                }
            }
        }

        umdmodVo.setBennam(bennam);
    }


    public void getBennamInGET(GidBizInfoVo gidBizInfoVo, UmdmodVo umdmodVo, PtsptaVo apl) {
        String bennam = umdmodVo.getBennam();
        Gid gid = new Gid();
        if (StringUtils.isEmpty(umdmodVo.getBennam())) {
            if (!StringUtils.isEmpty(gidBizInfoVo.getObjinr())) {
                gid.setInr(gidBizInfoVo.getObjinr());
                Gid gid1 = gidMapper.selectByPrimaryKey(gid);
                umdmodVo.setBennam(gid1.getOppnam());
            }

            if (!MdaUtils.isEmpty(apl)) {
                bennam = apl.getNam();
            }
        }
        umdmodVo.setBennam(bennam);
    }

    //出口交易用申请人
    public void getBennamInNET(NidBizInfoVo nidBizInfoVo, UmdmodVo umdmodVo, PtsptaVo apl) {
        String bennam = umdmodVo.getBennam();
        Nid nid = new Nid();
        if (StringUtils.isEmpty(umdmodVo.getBennam())) {
            if (!StringUtils.isEmpty(nidBizInfoVo.getObjinr())) {
                nid.setInr(nidBizInfoVo.getObjinr());
                Nid nid1 = nidMapper.selectByPrimaryKey(nid);
                umdmodVo.setBennam(nid1.getOppnam());
            }

            if (!MdaUtils.isEmpty(apl)) {
                bennam = apl.getNam();
            }
        }

        umdmodVo.setBennam(bennam);
    }

    public boolean isPtsHaveBic(PtsptaVo rolPts, Argument<String> argTag) {
        //!得到PTSPTA中国家信息
        //!==========================================================
        //!参数：rolPts - 需要取得国别信息的ptspta模块
        //!      ArgTag - BIC或者名称地址信息
        //!返回值：是否存在BIC
        //!==========================================================
        if (ptsptaToolComponent.ptsIsRolSet(rolPts)) {
            if (!MdaUtils.isEmpty(rolPts.getPta().getBic())) {
                argTag.value = rolPts.getPta().getBic();
                return true;
            } else {
                argTag.value = rolPts.getAdrblk();
                return false;
            }
        }
        return false;
    }

    public boolean isBic(String argBIC) {
        //! Check if BIC is correct
        //  check if length of BIC is 8 or 11 characters
        if (Strings.len(argBIC) != 8 && Strings.len(argBIC) != 11 && Strings.len(argBIC) > 0) {
            return false;
            //return ; //   ####   goto done
        }
        String bic = Strings.trim(argBIC);
        String let = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String num = "0123456789";
        // position 1-4 Bank-Code    A-Z are allowed
        // position 5-6 Country-Code A-Z are allowed
        int i = 1;
        for (; i <= 6; i++) {
            if (Strings.pos(let, Strings.mid(bic, i, 1)) == 0) {
                // if substring is not found, return value is 0
                return false;
            }
        }
        // position 7 Location-Code   A-Z and 2-9 are allowed
        if (Strings.pos(let, Strings.mid(bic, i, 1)) == 0 && Strings.pos("23456789", Strings.mid(bic, 7, 1)) == 0) {
            return false;
        }
        // position 8 Location-Code   A-Z exept O and 0-9 are allowed
        if (Strings.pos("ABCDEFGHIJKLMNPQRSTUVWXYZ", Strings.mid(bic, 8, 1)) == 0 && Strings.pos(num, Strings.mid(bic, 8, 1)) == 0) {
            return false;
        }
        // position 9-11 Branch-Code  A-Z and 0-9 are allowed
        if (Strings.len(bic) > 8) {
            for (i = 9; i <= 11; i++) {
                if (Strings.pos(let, Strings.mid(bic, i, 1)) == 0 && Strings.pos(num, Strings.mid(bic, i, 1)) == 0) {
                    return false;
                }
            }
        }
        return true;
        //    ####    label done
    }
}

