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

import com.brilliance.isc.common.trnmod.vo.TrnmodVo;
import com.brilliance.isc.mda.dao.PublicMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class TrnmodCheck implements ICheck<TrnmodVo> {

    @Resource
    PublicMapper publicMapper;

    public Map<String, String> check(TrnmodVo vo) {
        Map<String, String> maps = new HashMap<>();
        /*maps.putAll(checkSwiaddInsbnkN1000(vo));
        maps.putAll(checkCipmodCfkrgjN1234(vo));
        maps.putAll(checkRecbchkeyN1000(vo));
        maps.putAll(checkSwiaddShptoelcN1000(vo));
        maps.putAll(checkSwiaddLcrdocN1000(vo));
        maps.putAll(checkReconebchnamN1000(vo));
        maps.putAll(checkSwiaddNartxtN1001(vo));
        maps.putAll(checkSecmodN1000(vo));

        maps.putAll(checkSwiaddAdlcndN1000(vo));
        maps.putAll(checkRecbchnamN1000(vo));
        maps.putAll(checkSwiaddLcrgodN1000(vo));
        maps.putAll(checkSwiaddShpfroelcN1000(vo));
        maps.putAll(checkOrcnamN1000(vo));*/
        return maps;
    }


    /*public Map<String, String> checkSwiaddAdlcndN1000(TrnmodVo trnmodVo) {
        Map<String, String> resultMap = new HashMap<>();
        String frame = SettleContext.getTransName();
        String adlcnd = trnmodVo.getSwiadd().getAdlcnd();
        if (StringUtils.pos("-DITAME-", frame) != 0 && StringUtils.isNotEmpty(adlcnd) && adlcnd.contains("\"")) {
            //!允许输入中文的栏位不允许输入双引号，否则导致发报失败 "
//            trnmodVo.getErrorsMap().put("trnmod.SwiaddVo.adlcnd",
//                StringUtils.formatText(MdaUtils.getI18NString("sysmod","CT000665"), "\""));
            resultMap.put("trnmod.swiadd.adlcnd", "字符输入不合法");
        }
        return resultMap;
    }

    public Map<String, String> checkRecbchnamN1000(TrnmodVo trnmodVo) {
        Map<String, String> resultMap = new HashMap<>();
        //接收行
        if (StringUtils.isEmpty(trnmodVo.getRecbchnam())) {
//            trnmodVo.getErrorsMap().put("trnmod.recbchnam", MdaUtils.getI18NString("sysmod","CT000373"));
            resultMap.put("trnmod.recbchnam", "This field is mandatory");
        }
        return resultMap;
    }

    public Map<String, String> checkSwiaddLcrgodN1000(TrnmodVo trnmodVo) {
        Map<String, String> resultMap = new HashMap<>();
        String frame = SettleContext.getTransName();
        String lcrgod = trnmodVo.getSwiadd().getLcrgod();
        if (StringUtils.pos("-DITAME-", frame) != 0 && StringUtils.isNotEmpty(lcrgod) && lcrgod.contains("\"")) {
            //!允许输入中文的栏位不允许输入双引号，否则导致发报失败 "
//            trnmodVo.getErrorsMap().put("trnmod.SwiaddVo.lcrgod",
//                StringUtils.formatText(MdaUtils.getI18NString("sysmod","CT000665"), "\""));
            resultMap.put("trnmod.SwiaddVo.adlcnd", "字符输入不合法");
        }
        return resultMap;
    }

    public Map<String, String> checkSwiaddShpfroelcN1000(TrnmodVo trnmodVo) {
        Map<String, String> resultMap = new HashMap<>();
        String frame = SettleContext.getTransName();
        String shpfroelc = trnmodVo.getSwiadd().getShpfroelc();
        if (StringUtils.pos("-DITAME-", frame) != 0 && StringUtils.isNotEmpty(shpfroelc) && shpfroelc.contains("\"")) {
            //!允许输入中文的栏位不允许输入双引号，否则导致发报失败 "
//            trnmodVo.getErrorsMap().put("trnmod.SwiaddVo.shpfroelc",
//                StringUtils.formatText(MdaUtils.getI18NString("sysmod","CT000665"), "\""));
            resultMap.put("trnmod.SwiaddVo.shpfroelc", "This field is mandatory");
        }
        return resultMap;
    }

    public Map<String, String> checkOrcnamN1000(TrnmodVo trnmodVo) {
        Map<String, String> resultMap = new HashMap<>();
        String key = "trnmod.orcnam";
        if (StringUtils.isEmpty(trnmodVo.getOrcnam())) {
            //trnmodVo.getErrorsMap().put("trnmod.orcnam", MdaUtils.getI18NString("sysmod","CT000373"));
            resultMap.put("trnmod.orcnam", "This field is mandatory");
        } else {
            String transName = SettleContext.getTransName();
            if ("BRTSET".equals(transName) || "BCTSET".equals(transName) || "GITSET".equals(transName) || "DFTMSG".equals(transName)) {
                resultMap.putAll(filterCNY1(key, trnmodVo, trnmodVo.getOrcnam(), 60));
                resultMap.putAll(filterCNY2(key, trnmodVo, trnmodVo.getOrcnam(), 60));
            }
        }
        return resultMap;
    }

    public Map<String, String> filterCNY1(String key, TrnmodVo trnmodVo, String argTxt, int argNum) {
        Map<String, String> resultMap = new HashMap<>();
        //过滤一代支付CMT报文
        if ("CMT".equals(trnmodVo.getSwftyp())) {
            if (filtmarks(argTxt)) {
                resultMap.put(key, "此栏位不能出现半角\":\",不能以\"，\"或\"。\"或\"-\"开始!");
            }
            if (argTxt.length() > argNum) {
                resultMap.put(key, "The name is too long, need to be less than " + argNum + "!");
            }
        }
        return resultMap;

    }

    public Map<String, String> filterCNY2(String key, TrnmodVo trnmodVo, String argTxt, int argNum) {
        Map<String, String> resultMap = new HashMap<>();
        //过滤二代支付HVP支付报文
        if (StringUtils.pos("-HVP-HVQ-BEP-BEQ-BES-", trnmodVo.getSwftyp()) > 0) {
            //特殊标点符号过滤
            if (filtmarks(argTxt)) {
                resultMap.put(key, "此栏位不能出现半角\":\",不能以\"，\"或\"。\"或\"-\"开始!");
            }
            //长度限制过滤
            int lenth = chineseFldLength(argTxt);
            if (lenth > argNum) {
                resultMap.put(key, "The name is too long, need to be less than " + argNum + "!");
            }
        }
        return resultMap;
    }

    public int chineseFldLength(String txt) {
        //!判断字符串长度（含中文）函数
        //!一个汉字当作一个字符来计算
        //!=========================================
        //!返回值：实际字符长度
        //!变量：   txt     待判断的字符串
        //!           number  小数部分数字
        //!=========================================
        //计算规则:UTF-8的长度和GBK计算长度的差异来进行计算
        int len = Strings.len(txt);
        int len1 = Strings.len(StringUtils.convertBlock(txt, 11));
        int len2 = Strings.len(StringUtils.convertBlock(txt, 13));
        int lenChinese = (len2 - len1);  //因为convert会把XML特殊字符进行转意，所以需要用这种方式计算
        return len - lenChinese;

    }

    public boolean filtmarks(String strtxt) {
        //!人民币跨境过滤标点符号函数
        //!==================================================
        //!返回值：过滤成功与否
        //!变量:   过滤栏位内容
        //!==================================================
        String[] marks1 = {":", "-", ",", "."};
        for (String str : marks1) {
            if (strtxt.contains(str)) {
                return true;
            }
        }
        String[] marks2 = {"，。", "，．"};
        for (String str : marks2) {
            if (strtxt.contains(str)) {
                return true;
            }
        }
        return false;
    }

    *//**
     * source:trnmod.0232.script
     * <p>
     * target="swiadd.insbnk",order=1000)
     *//*

    public Map<String, String> checkSwiaddInsbnkN1000(TrnmodVo trnmod) {
        Map<String, String> resultMap = new HashMap<>();

        String frame = SettleContext.getUserSession().getAtp().getCod();
        if ("-DITAME-".contains(frame)) {
            String errorInfo = checkAllowCN(trnmod.getSwiadd().getInsbnk());
            if (StringUtils.isNotEmpty(errorInfo)) {
                resultMap.put("trnmod.swiadd.insbnk", errorInfo);
            }
        }

        return resultMap;
    }

    *//**
     * source:trnmod.0224.script
     * <p>
     * target="cipmod.cfkrgj",order=1234)
     *//*

    public Map<String, String> checkCipmodCfkrgjN1234(TrnmodVo trnmod) {
        Map<String, String> resultMap = new HashMap<>();

        if ("-BRTSET-BCTSET-GITSET-DFTMSG-".contains(SettleContext.getTransName())) {
            if ("CIV".equals(trnmod.getSwftyp()) && StringUtils.isEmpty(trnmod.getCipmodVo().getCfkrgj())) {
                resultMap.put("trnmod.cipmod.cfkrgj", MdaUtils.getI18NString("sysmod", "CT000373"));
            }
        }

        return resultMap;
    }

    *//**
     * source:trnmod.0125.script
     * <p>
     * target="recbchkey",order=1000)
     *//*

    public Map<String, String> checkRecbchkeyN1000(TrnmodVo trnmod) {
        Map<String, String> resultMap = new HashMap<>();

        //!收报行行号
        if (StringUtils.isEmpty(trnmod.getRecbchkey())) {
            resultMap.put("trnmod.recbchkey", "This field is mandatory");
        }

        return resultMap;
    }

    *//**
     * source:trnmod.0226.script
     * <p>
     * target="swiadd.shptoelc",order=1000)
     *//*

    public Map<String, String> checkSwiaddShptoelcN1000(TrnmodVo trnmod) {
        Map<String, String> resultMap = new HashMap<>();

        String frame = SettleContext.getUserSession().getAtp().getCod();
        if ("-DITAME-".contains(frame) && StringUtils.isNotEmpty(trnmod.getSwiadd().getShptoelc())) {
            String errorInfo = checkAllowCN(trnmod.getSwiadd().getShptoelc());
            if (StringUtils.isNotEmpty(errorInfo)) {
                resultMap.put("trnmod.swiadd.shptoelc", errorInfo);
            }
        }

        return resultMap;
    }

    *//**
     * source:trnmod.0229.script
     * <p>
     * target="swiadd.lcrdoc",order=1000)
     *//*

    public Map<String, String> checkSwiaddLcrdocN1000(TrnmodVo trnmod) {
        Map<String, String> resultMap = new HashMap<>();

        String frame = SettleContext.getUserSession().getAtp().getCod();
        if ("-DITAME-".contains(frame)) {
            String errorInfo = checkAllowCN(trnmod.getSwiadd().getLcrdoc());
            if (StringUtils.isNotEmpty(errorInfo)) {
                resultMap.put("trnmod.swiadd.lcrdoc", errorInfo);
            }
        }

        return resultMap;
    }

    *//**
     * source:trnmod.0180.script
     * <p>
     * target="reconebchnam",order=1000)
     *//*

    public Map<String, String> checkReconebchnamN1000(TrnmodVo trnmod) {
        Map<String, String> resultMap = new HashMap<>();
        String key = "trnmod.reconebchnam";
        String swf;
        String reconebchnam;
        String sql;
        if (StringUtils.isEmpty(trnmod.getReconebchnam())) {
            resultMap.put("trnmod.reconebchnam", "This field is mandatory");
        } else {
            resultMap.putAll(filterCNYCIPS(key, trnmod, trnmod.getReconebchnam(), 140));
        }
        swf = trnmod.getSwftyp();
        //控制输入错误行号或名称的校验
        if (!StringUtils.isEmpty(trnmod.getReconebchnam()) && "CI".equals(swf.substring(1, 3))) {
            reconebchnam = Strings.getLine(trnmod.getReconebchnam(),1) + Strings.getLine(trnmod.getReconebchnam(),2) + Strings.getLine(trnmod.getReconebchnam(),3) + Strings.getLine(trnmod.getReconebchnam(),4);
            sql = "SELECT KJCYHH,CYZWMC FROM BNM WHERE (CYZWMC = "+reconebchnam+" OR CYYWMC = "+reconebchnam+") or KJCYHH = "+trnmod.getReconebch()+" AND CYZGBS='DRPT' AND YOUXBZ='VLID' AND BLYWZT='ENBL' AND XTDLZT='LOGN' ";
            Map<String,Object> sqlMap = new HashMap<>();
            sqlMap.put("sql",sql);
            Map<String, Object> result = publicMapper.dyncQueryMap(sqlMap);
            String kjcyhh = (String) result.get("KJCYHH");
            String cyzwmc = (String) result.get("CYZWMC");
            if (StringUtils.isEmpty(kjcyhh)) {
//                    ctx.setErrorMessage(MdaUtils.getI18NString("trnmod","CT000225"));
                resultMap.put("trnmod.reconebchnam", "收款直接参与者名称错误，请重新输入！");
            }
            if (MdaUtils.compareTo(trnmod.getReconebch(), kjcyhh) != 0) {
//                    ctx.setErrorMessage(MdaUtils.getI18NString("trnmod","CT000226"));
                resultMap.put("trnmod.reconebchnam", "收款直接参与者行号错误，请重新输入！");
            }
            if (MdaUtils.compareTo(trnmod.getReconebchnam(), cyzwmc) != 0) {
//                    ctx.setErrorMessage(MdaUtils.getI18NString("trnmod","CT000227"));
                resultMap.put("trnmod.reconebchnam", "收款直接参与者名称错误，请重新输入！");
            }
        }
        return resultMap;
    }

    *//**
     * source:trnmod.0231.script
     * <p>
     * target="swiadd.nartxt",order=1001)
     *//*

    public Map<String, String> checkSwiaddNartxtN1001(TrnmodVo trnmod) {
        Map<String, String> resultMap = new HashMap<>();

        String frame = SettleContext.getUserSession().getAtp().getCod();
        if ("-DITAME-".contains(frame)) {
            String errorInfo = checkAllowCN(trnmod.getSwiadd().getNartxt());
            if (!StringUtils.isEmpty(errorInfo)) {
                resultMap.put("trnmod.swiadd.nartxt", errorInfo);
            }
        }

        return resultMap;
    }

    public String checkAllowCN(String attributeValue) {
        String errorInfo = NULLSTR;
        //!允许输入中文的栏位不允许输入双引号，否则导致发报失败 "
        String marks = "\"";
        int txtLen = Strings.len(attributeValue);
        String substr = NULLSTR;
        int pos = 0;
        for (int idx = 1; idx <= txtLen; idx++) {
            substr = Strings.mid(attributeValue, idx, 1);
            pos = Strings.pos(marks, substr);
            if (pos != 0) {
                errorInfo = MdaUtils.getI18NString("sysmod", "CT000665");
                break;
            }
        }
        return errorInfo;
    }

    *//**
     * source:trnmod.0143.script
     * <p>
     * target="secmod",order=1000)
     *//*
    public Map<String, String> checkSecmodN1000(TrnmodVo trnmod) {
        Map<String, String> resultMap = new HashMap<>();
        //!二代模式校验
        if (StringUtils.isEmpty(trnmod.getSecmod())) {
            resultMap.put("trnmod.secmod", "请点击\"获取\"按钮得到报文类型！");
        }

        return resultMap;
    }

    public Map<String, String> filterCNYCIPS(String key, TrnmodVo trnmod, String argTxt, int argNum) {
        Map<String, String> resultMap = new HashMap<>();
        //过滤CIPS支付报文
        String swftyp = "";
        //if GetFramename = "CPTOPN" or GetFramename = "CPTADV" then
//        if ("CP".equals(SettleContext.getTransName().substring(1, 3))) {
//            swftyp = Datas.getContent(this,"\\CPDGRP\\REC\\SWFTYP");
//        } else {
        swftyp = trnmod.getSwftyp();
//        }
        *//*
         if Pos( "-CIU-CIV-", $swftyp ) > 0 then
         *//*
        int lenth = 0;
        if (Strings.pos("-CIU-CIV-CIW-", swftyp) > 0) {
            //特殊标点符号过滤
            if (filtmarks(argTxt)) {
//                ctx.setErrorMessage(MdaUtils.getI18NString("trnmod","CT000206"));
                resultMap.put(key, "此栏位不能出现半角\":\",不能以\"，\"或\"。\"或\"-\"开始!");
            }
            //长度限制过滤
            lenth = chineseFldLength(argTxt);
            if (MdaUtils.compareTo(lenth, argNum) > 0) {
//                ctx.setErrorMessage(MdaUtils.getI18NString("trnmod","CT000207"),argNum);
                resultMap.put(key, "The name is too long, need to be less than " + argNum + "!");
            }
        }
        return resultMap;
    }*/
}
