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

import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.esb.account.IAccountFeign;
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.util.StringUtils;
import com.brilliance.isc.vo.FxpmodVo;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Component
public class FxpmodValidator extends AbstractModuleValidator<FxpmodVo> {

    @Autowired(required = false)
    private IAccountFeign accountFeign;

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

    @Validate(path="setmod.setglg.setgll.%s.sespay.fxpmod.resbch")
    public ValidResult validResbch(FxpmodVo fxpmod) {
        if (StringUtils.isEmpty(fxpmod.getResbch())) {
            return errorMandatory();
        } else {
//            String errorMsg = "";
//            try {
//                //调用8995接口判断resbch是否已经升级fxps2.0
//                InnerReq8995 req8995 = new InnerReq8995(fxpmod.getResbch());
//                InnerReq<InnerReq8995> innerReq8995 = new InnerReq<>(req8995);
//                //调用接口
//                InnerRes<InnerRes8995> innerRes8995 = accountFeign.call8995(innerReq8995);
//                if (innerRes8995 != null && "AAAAAAA".equals(innerRes8995.header.code) && innerRes8995.DATA != null) {
//                    if (!"1".equals(innerRes8995.DATA.shfobz)) {
//                        errorMsg = "此接收行"+fxpmod.getResbch()+"未升级2.0";
//                    }
//                }
//            } catch(Exception e) {
//                errorMsg = "调用接口8995失败，查询不到此接收行"+fxpmod.getResbch()+"是否已升级2.0";
//            }
//            if (StringUtils.isNotEmpty(errorMsg)) {
//                return ValidResult.build(false, errorMsg);
//            }
        }
        return null;
    }

    @Validate(path="setmod.setglg.setgll.%s.sespay.fxpmod.resbchnam")
    public ValidResult validResbchnam(FxpmodVo fxpmod) {
        if (StringUtils.isEmpty(fxpmod.getResbchnam())) {
            return errorMandatory();
        }
        return null;
    }

    @Validate(path="setmod.setglg.setgll.%s.sespay.fxpmod.commet")
    public ValidResult validCommet(FxpmodVo fxpmod) {
        if (StringUtils.isNotEmpty(fxpmod.getCommet())) {
            if (fxpmod.getCommet().length() > 210) {
                return ValidResult.build(false, "附言长度不能超过210");
            }
            List<String> checkMarks = this.checkmarks_commet(fxpmod.getCommet());
            if (checkMarks != null && checkMarks.size() > 0) {
                return ValidResult.build(false, Strings.getText("以下字符不可使用：$1", String.join("", checkMarks)));
            }
        }
        return null;
    }

    @Validate(path="setmod.setglg.setgll.%s.sespay.fxpmod.pyenam")
    public ValidResult validPyenam(FxpmodVo fxpmod) {
        if ("CPTOPN".equalsIgnoreCase(SettleContext.getDirectTransName())) {
            return null;
        }
        if (StringUtils.isEmpty(fxpmod.getPyenam())) {
            if ("111".equals(fxpmod.getFmttyp())) {
                return errorMandatory();
            } else {
                if (StringUtils.isEmpty(fxpmod.getPyeadr())) {
                    return ValidResult.build(false, "收款人名称，地址必须录入其中一项");
                }
            }
        } else {
            List<String> checkMarks = this.checkmarks2(fxpmod.getPyenam());
            if (checkMarks != null && checkMarks.size() > 0) {
                return ValidResult.build(false, Strings.getText("以下字符不可使用：$1", String.join("", checkMarks)));
            }
            if (fxpmod.getPyenam().length() > 60) {
                return ValidResult.build(false, "此栏位输入字符数不能大于60");
            }
        }
        return null;
    }

    @Validate(path="setmod.setglg.setgll.%s.sespay.fxpmod.pyeadr")
    public ValidResult validPyeadr(FxpmodVo fxpmod) {
        if ("CPTOPN".equalsIgnoreCase(SettleContext.getDirectTransName())) {
            return null;
        }
        if (StringUtils.isEmpty(fxpmod.getPyeadr()) && "111".equals(fxpmod.getFmttyp())) {
            return errorMandatory();
        }
        if (StringUtils.isNotEmpty(fxpmod.getPyeadr())) {
            List<String> checkMarks = this.checkmarks2(fxpmod.getPyeadr());
            if (checkMarks != null && checkMarks.size() > 0) {
                return ValidResult.build(false, Strings.getText("以下字符不可使用：$1", String.join("", checkMarks)));
            }
            if (fxpmod.getPyeadr().length() > 70) {
                return ValidResult.build(false, "此栏位输入字符数不能大于70");
            }
        }
        return null;
    }

    @Validate(path="setmod.setglg.setgll.%s.sespay.fxpmod.orcnam")
    public ValidResult validOrcnam(FxpmodVo fxpmod) {
        if ("CPTOPN".equalsIgnoreCase(SettleContext.getDirectTransName())) {
            return null;
        }
        if (StringUtils.isEmpty(fxpmod.getOrcnam())) {
            if ("111".equals(fxpmod.getFmttyp())) {
                return errorMandatory();
            } else {
                if (StringUtils.isEmpty(fxpmod.getOrcadr())) {
                    return ValidResult.build(false, "汇款人名称，地址必须录入其中一项");
                }
            }
        } else {
            List<String> checkMarks = this.checkmarks2(fxpmod.getOrcnam());
            if (checkMarks != null && checkMarks.size() > 0) {
                return ValidResult.build(false, Strings.getText("以下字符不可使用：$1", String.join("", checkMarks)));
            }
            if (fxpmod.getOrcnam().length() > 60) {
                return ValidResult.build(false, "此栏位输入字符数不能大于60");
            }
        }
        return null;
    }

    @Validate(path="setmod.setglg.setgll.%s.sespay.fxpmod.orcadr")
    public ValidResult validOrcadr(FxpmodVo fxpmod) {
        if ("CPTOPN".equalsIgnoreCase(SettleContext.getDirectTransName())) {
            return null;
        }
        if (StringUtils.isEmpty(fxpmod.getOrcadr()) && "111".equals(fxpmod.getFmttyp())) {
            return errorMandatory();
        }
        if (StringUtils.isNotEmpty(fxpmod.getOrcadr())) {
            List<String> checkMarks = this.checkmarks2(fxpmod.getOrcadr());
            if (checkMarks != null && checkMarks.size() > 0) {
                return ValidResult.build(false, Strings.getText("以下字符不可使用：$1", String.join("", checkMarks)));
            }
            if (fxpmod.getOrcadr().length() > 70) {
                return ValidResult.build(false, "此栏位输入字符数不能大于70");
            }
        }
        return null;
    }

    /**
     *   source:attp.0080.script
     */
    public List<String> checkmarks2(String strtxt) {
        if (StringUtils.isEmpty(strtxt)) {
            return null;
        }
        String marks = "'`~!@#$%^&*-_+=[]{}\\;':<>?/,.";
        List<String> checkMarkList = new ArrayList<>();
        for (char c : marks.toCharArray()) {
            String m = String.valueOf(c);
            if (strtxt.indexOf(m) > -1) {
                if (!checkMarkList.contains(m)) {
                    checkMarkList.add(m);
                }
            }
        }
        return checkMarkList;
    }

    //附言栏位筛查字符放宽，调整为如下字符。（保鲜需求，对应老系统R24021049需求中附言校验的改动）
    public List<String> checkmarks_commet(String strtxt) {
        if (StringUtils.isEmpty(strtxt)) {
            return null;
        }
        String marks = "'`~$^{}\'<>";
        List<String> checkMarkList = new ArrayList<>();
        for (char c : marks.toCharArray()) {
            String m = String.valueOf(c);
            if (strtxt.indexOf(m) > -1) {
                if (!checkMarkList.contains(m)) {
                    checkMarkList.add(m);
                }
            }
        }
        return checkMarkList;
    }

}
