package com.brilliance.isc.common.transaction.check;

import com.brilliance.isc.bo.Pta;
import com.brilliance.isc.bo.Pts;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.util.I18nUtil;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class PtsptaCheck implements ICheck<PtsptaVo> {

    @Override
    public Map<String, String> check(PtsptaVo vo) {
        Map<String, String> maps = new HashMap<>();
        if (vo.getRolsetflg().equals("Y")) {
            maps.putAll(checkPtsptaPtsExtkeyN960(vo));
            maps.putAll(checkPtsptaPtsAdrblkN950(vo));
            maps.putAll(checkPtsptaPtsAdrblkN970(vo));
        }
        return maps;
    }

    /**
     * source:ptsp.0013.script
     * <p>
     * target="ptspta.pts.extkey",order=960
     */
//    @Check(target="ptspta.pts.extkey",order=960)
    public Map<String, String> checkPtsptaPtsExtkeyN960(PtsptaVo vo) {
        String resultKey = vo.getRol() + ".pts.extkey";
        // check address block if not currently on details panel (on that panel its checked seperately)
//        String curPOPUP = NULLSTR;
//            curPOPUP = Systems.getContext(tdContextPOPUPPANEL);
//            if( MdaUtils.compareTo(curPOPUP, MdaDriver.getModuleInfo(this,tdModuleInfoFullName) + "\\DETP") != 0 && Strings.pos(curPOPUP,"PTSMOD\\ADRP0") == 0 )
//            {
//                ptsCheckAdrblk( "EXTKEY" );
//            }
        return new HashMap<>(ptsCheckAdrblk(vo, resultKey, "EXTKEY"));
    }

    /**
     * source:ptsp.0021.script
     */
    public Map<String, String> ptsCheckAdrblk(PtsptaVo vo, String resultKey, String argTrigger) {
        Map<String, String> resultMap = new HashMap<>();
        //! Set error, if addresses are to be stored in database, but address block is set and not PTAINR
        //! used in Check ADRBLK and EXTKEY of PTSP only. The calling field is passed as ArgTrigger
        //屏蔽添加临时地址的功能
        //相应的check Rule 做相应的变动
        Pta pta = vo.getPta();
        Pts pts = vo.getPts();
        if (!(MdaUtils.isEmpty(Strings.getLine(pts.getAdrblk(), 1)) || MdaUtils.isEmpty(Strings.getLine(pts.getAdrblk(), 4)))) {
            if (MdaUtils.isEmpty(pta.getInr()) && (!MdaUtils.isEmpty(pts.getAdrblk())) && ptsIsPtaAdrRequired(pts.getRol())) {
//                if (MdaUtils.isEmpty(vo.getTmpflg())) {
//                    ctx.setErrorMessage(MdaUtils.getI18NString("ptsp", "CT000009"), "");
//                } else {
                if (MdaUtils.compareTo(argTrigger, "ADRBLK") == 0) {
                    resultMap.put(resultKey, I18nUtil.getMessage("0038"));
                }
//                }
            }
        }
        // mandatory check
        if (MdaUtils.isEmpty(pts.getAdrblk()) || MdaUtils.isEmpty(pts.getExtkey())) {
            // if called from ADRBLK, set error if ADRBLK is enabled and empty
            if (argTrigger.equals("ADRBLK")) {
                if (MdaUtils.isEmpty(pts.getAdrblk())) {
                    resultMap.put(resultKey, I18nUtil.getMessage("0002"));
                }
            } else {
                if (MdaUtils.isEmpty(pts.getExtkey())) {
                    resultMap.put(resultKey, I18nUtil.getMessage("0002"));
                }
            }
        }
        //---GNZ---DZTEST---
        /*
         if not IsEmpty( PTSPTA\\PTS\\EXTKEY ) and IsEmpty( PTSPTA\\PTS\\ADRBLK ) and IsEmpty( PTSPTA\\CHNGUAFLG ) then
         */
        if (!MdaUtils.isEmpty(pts.getExtkey()) && MdaUtils.isEmpty(pts.getAdrblk())) {
            resultMap.put("ptspta.pts.adrblk", I18nUtil.getMessage("0002"));
        }
        return resultMap;
    }

    /**
     * source:ptssub.0037.script
     */
    public boolean ptsIsPtaAdrRequired(String argrol) {
        //! Decides if all contract addresses have to be stored as contract addresses aswell
        //! This setting is valid for all contract addrsses in installation
        //! The setting may vary depending on the role. Therefore role is passed aswell.
        // default in DOK5 is TRUE, exept for non contract parties in free correspondence, but may be changed to false by supporter if installation does not require this
        return MdaUtils.compareTo(argrol, "OTH") != 0 || MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(), 4, 3), "FRE") != 0;

    }

    /**
     * source:ptsp.0014.script
     * <p>
     * target="ptspta.pts.adrblk",order=950
     */
//    @Check(target="ptspta.pts.adrblk",order=950)
    public Map<String, String> checkPtsptaPtsAdrblkN950(PtsptaVo vo) {
        String resultKey = vo.getRol() + ".pts.adrblk";
        // check address block if not currently on details panel (on that panel its checked seperately)
//        String curPOPUP = NULLSTR;
//        if (MdaUtils.compareTo(vo.getPansta(), PanStaAdd) == 0 || MdaUtils.compareTo(vo.getPansta(), PanStaEdit) == 0) {
//            curPOPUP = Systems.getContext(tdContextPOPUPPANEL);
//            if( MdaUtils.compareTo(curPOPUP, MdaDriver.getModuleInfo(this,tdModuleInfoFullName) + "\\DETP") != 0 && Strings.pos(curPOPUP,"PTSMOD\\ADRP0") == 0 )
//            {
//                ptsCheckAdrblk( "ADRBLK" );
//            }
        //        }

        return new HashMap<>(ptsCheckAdrblk(vo, resultKey, "ADRBLK"));
    }

    /**
     * source:ptsp.0046.script
     * <p>
     * target="ptspta.pts.adrblk",order=970
     */
//    @Check(target="ptspta.pts.adrblk",order=970)
    public Map<String, String> checkPtsptaPtsAdrblkN970(PtsptaVo vo) {
        Map<String, String> resultMap = new HashMap<>();
        String resultKey = vo.getRol() + ".pts.adrblk";
        boolean slashFound = false;
        boolean dataFound = false;
        String line;
        for (int i = 1; i <= 4; i++) {
            line = Strings.trim(Strings.getLine(vo.getPts().getAdrblk(), i));
            if (MdaUtils.compareTo(Strings.mid(line, 1, 1), "/") == 0) {
                if (MdaUtils.compareTo(dataFound, true) == 0) {
                    return resultMap; // / not first Character = OK
                }
                slashFound = true;
            } else {
                if (!MdaUtils.isEmpty(line)) {
                    dataFound = true;
                }
            }
        }
        if (MdaUtils.compareTo(slashFound, true) == 0 && MdaUtils.compareTo(dataFound, false) == 0) {
            resultMap.put(resultKey, I18nUtil.getMessage("0039"));
        }

        return resultMap;
    }

    public Map<String, String> check(PtsptaVo vo, int i) {
        Map<String, String> checked = check(vo);
        Map<String, String> checked2 =  new HashMap<>();
        checked.forEach((key, value) -> {
            checked2.put(key + "[" + i + "]", value);
            //checked.remove(key);
        });
        return checked2;
    }
}
