package com.brilliance.isc.swift.objmtconvert.model.busvalidator.mt1xx;

import com.brilliance.isc.swift.objmtconvert.model.AbsMTValidator;
import com.brilliance.isc.swift.objmtconvert.result.MtResult;
import com.brilliance.isc.swift.objmtconvert.util.Constans;
import com.brilliance.isc.swift.objmtconvert.util.Language;
import com.prowidesoftware.swift.model.field.*;
import com.prowidesoftware.swift.model.mt.mt1xx.MT103_STP;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.stream.Collectors;

public class MT103STPValidator extends AbsMTValidator {
    public MT103STPValidator(String mtStr, MtResult mtResult)
    {
        super(mtStr,mtResult);
    }
    @Override
    public void bussinessValidator() {
       // MtResult mtResult=getMtResult();
        MT103_STP mt103 = (MT103_STP) this.getAbstractMT();
        //C1:如果字段33B存在并且货币代码不同于字段32A中的货币代码，则字段36必须存在，否则不允许字段36(错误代码:D75)
        Field33B field33B =mt103.getField33B();
        Field32A field32A =mt103.getField32A();
        String field32ACurrency="";
        if (field32A!=null){
             field32ACurrency =field32A.getCurrency();
        }//33B存在
        if(field33B!=null&& StringUtils.isNotEmpty(field33B.getValue())){
            String field33BCurrency =field33B.getCurrency();
            //33B货币代码不同于32A的货币代码
            if(!field32ACurrency.equals(field33BCurrency)){
                Field36 field36= mt103.getField36();
                if(field36==null||StringUtils.isEmpty(field36.getValue()))
                {
                    //mtResult.setSuccess(false);
                    mtResult.addDescription(Language.getLanguage("MT103STP_C1_1","【MT103_STP校验失败】:"));
                    return ;
                }
            }//33B货币代码相等于32A的货币代码
            else{
                Field36 field36= mt103.getField36();
                if(field36!=null){
                    if(field36!=null||StringUtils.isNotEmpty(field36.getValue()))
                    {
                        mtResult.addDescription(Language.getLanguage("MT103STP_C1_2","【MT103_STP校验失败】:"));
                        return ;
                    }
                }
            }
        }//33B不存在
        else{

            Field36 field36= mt103.getField36();
            if(field36!=null){
                if(field36!=null||StringUtils.isNotEmpty(field36.getValue()))
                {
                    //mtResult.setSuccess(false);
                    mtResult.addDescription(Language.getLanguage("MT103STP_C1_3","【MT103_STP校验失败】:"));
                    return ;
                }
            }

        }

        //C2:如果发件人和接收方BIC的国家代码在以下列表中: AD、AT、 BE BG、BV、CH、 CY CZ、 DE、DK、EE、 ES、F FR、GB、GF、GI、GP、GR、Hu、IE、IS、IT、Li、LT
        //、LU、LV、MC、MQ、MT、NL、NO、PL、PM、PT、RE、RO、SE、Si、SJ、SK、SM、TF和VA
        // 则字段33B是强制性的,否则字段33B是可选的。
        String[] words = {"AD","AT","BE","BG","BV","CH","CY","CZ","DE","DK","EE","ES","FI","FR","GB","GF","GI","GP","GR","HU","IE","IS","IT","LI", "LT", "LU", "LV", "MC", "MQ", "MT", "NL", "NO", "PL", "PM", "PT", "RE", "RO", "SE", "SI", "SJ", "SK", "SM", "TF", "VA"};
        String RecvBic = mt103.getReceiver();
        String SendBic=mt103.getSender();
        String  RecvBicC2 = RecvBic.substring(4,6);
        String  SendBicC2 = SendBic.substring(4,6);
        for(String w:words){
            if(RecvBicC2.contains(w)&&SendBicC2.contains(w)){
                if(field33B==null ){
                    mtResult.addDescription(Language.getLanguage("MT103STP_C2","【MT103_STP校验失败】:"));
                    return  ;
                }
            }
        }


        //C3:如果字段23B包含代码SPRI，字段23E可以包含只有编码SDVA或INTC(错误代码:E01)。
        //如果字段23B包含代码SSTD或SPAY之一，字段不能使用23E(错误代码:F02)。
        Field23B field23B= mt103.getField23B();
        String field23BValue = field23B.getValue();
        //List field23E = mt103.getField23E();
        List<String> field23E  = mt103.getField23E().stream().map(e -> e.getValue()).collect(Collectors.toList());
             if ((field23BValue).contains("SPRI")) {
                 for (int i = 0; i < field23E.size(); i++) {
                     if(!("SDVA".equals(field23E.get(i)) ||("INTC".equals(field23E.get(i))))){
                         mtResult.addDescription(Language.getLanguage("MT103STP_C3_1","【MT103_STP校验失败】:"));
                         return  ;
                     }
                 }
            }else if ("SSTD".equals(field23BValue)||"SPAY".equals(field23BValue)) {
                if(mt103.getField23E()!=null|| mt103.getField23E().size()>0){
                    mtResult.addDescription(Language.getLanguage("MT103STP_C3_2","【MT103_STP校验失败】:"));
                    return  ;
                }
            }
        //C4:如果字段55A存在，则字段53A和54A必须同时存在(错误代码: E06)。
        Field55A field55A =mt103.getField55A();
            Field53A field53A= mt103.getField53A();
            Field54A field54A= mt103.getField54A();
            //55A存在
        if(field55A!=null&& StringUtils.isNotEmpty(field55A.getValue())){
            if(field53A==null||field54A==null){
                mtResult.addDescription(Language.getLanguage("MT103STP_C4","【MT103_STP校验失败】:"));
                return  ;
            }
        }

        //C5:如果字段56A存在，则字段57A也必须存在(错误代码:C81)。
        Field56A field56A =mt103.getField56A();
         Field57A field57A=mt103.getField57A();
        //56A存在
        if(field56A!=null&& StringUtils.isNotEmpty(field56A.getValue())){
            if(field57A==null){
                mtResult.addDescription(Language.getLanguage("MT103STP_C5","【MT103_STP校验失败】:"));
                return  ;
            }
        }

        //C6:如果字段23B包含代码SPRI，则字段56A必须不存在(错误代码:E16)。
        if("SPRI".equals(field23BValue)){
            if(field56A!=null&& StringUtils.isNotEmpty(field56A.getValue())){
                mtResult.addDescription(Language.getLanguage("MT103STP_C6","【MT103_STP校验失败】:"));
                return  ;
            }
        }
        //C7:如果字段71A包含OUR，则字段71F不允许和字段71G是可选的(错误代码:E13)。
        Field71A field71A =mt103.getField71A();
         List field71F =mt103.getField71F();
         Field71G field71G =mt103.getField71G();
        String field71AValue="";
        if(field71A!=null){
             field71AValue =field71A.getValue();
        }
        if("OUR".equals(field71AValue)){
            if(field71F!=null&&field71F.size()>0){
                mtResult.addDescription(Language.getLanguage("MT103STP_C7_1","【MT103_STP校验失败】:"));
                return  ;
            }
        }
        //如果字段71A包含SHA，则字段71F是可选的，字段不允许使用71G(错误代码:D50)。
        if("SHA".equals(field71AValue)){
            if(field71G!=null&& StringUtils.isNotEmpty(field71G.getValue())){
                mtResult.addDescription(Language.getLanguage("MT103STP_C7_2","【MT103_STP校验失败】:"));
                return  ;
            }
        }
        //如果字段71A包含BEN，则字段71F的至少一次出现是强制性的，并且字段不允许使用71G(错误代码:E15)。
        if("BEN".equals(field71AValue)){
            if(field71G!=null){
                mtResult.addDescription(Language.getLanguage("MT103STP_C7_3","【MT103_STP校验失败】:"));
                return  ;
            }else if(field71F==null||field71F.size()==0){
                mtResult.addDescription(Language.getLanguage("MT103STP_C7_4","【MT103_STP校验失败】:"));
                return  ;
            }
        }


        //C8:如果存在字段71F(至少一次出现)或字段71G，则字段33B为必填项，否则字段33B是可选的(错误代码:D51)。
        //注1:这两个领域的存在71F和71G还受网络验证规则C7(错误代码:E13、D50E15)的监管
        // 注二:字段33B的存在也由网络验证规则C2(错误代码:D49)调节
        if ((field71F!=null&&field71F.size()>0)||(field71G!=null&& StringUtils.isNotEmpty(field71G.getValue()))){
            if(field33B==null&& StringUtils.isEmpty(field33B.getValue())){
                mtResult.addDescription(Language.getLanguage("MT103STP_C8","【MT103_STP校验失败】:"));
                return  ;
            }
        }


        //C9:字段71G和32A中的货币代码必须相同(错误代码:C02
        if(field71G!=null&&field32A!=null){
            if(!field71G.getCurrency().equals(field32A.getCurrency())){
                mtResult.addDescription(Language.getLanguage("MT103STP_C9","【MT103_STP校验失败】:"));
                return  ;
            }
        }


        //C10:如果发件人和收件人的商业识别码在以下列表中: AD,AT,E,BG,BV,CH,CY,CZ,DE,DK,E,ES,L,FR,GB,F,G, GP,GR,HR,HU,E,L,S,IT,Ll,LT,LU,LV,MC,MQ,MT,NL,NO,PL, PM,PT,RE,RO, SE,S1,SJ,SK,SM,TF和VA,则适用于以下内容:
        //如果字段57A不存在，IBAN (IS013616)在字段59a的子字段中是必需的。 (错误代码:D19)
        // 如果57A字段存在，并且57A中金融机构BIC的国家代码在上述国家代码列表中，则在59a字段的账户子字段中必须提供1 BAN (S0 13616)。(错误代码:D19
        //在所有其他情况下，lbanlso 13616)的存在是可选的，其格式未在字段59a的子字段账户中验证。
        String[] wordsc10 = {"AD", "AT", "BE", "BG", "BV", "CH", "CY", "CZ", "DE", "DK", "EE", "ES", "FI", "FR", "GB", "GF", "GI", "GP", "GR", "HR", "HU", "IE", "IL", "IS", "IT", "LI", "LT", "LU", "LV", "MC", "MQ", "MT", "NL", "NO", "PL", "PM", "PT", "RE", "RO", "SE", "SI", "SJ", "SK", "SM", "TF" , "VA"};
        String RecvBicc10 = mt103.getReceiver();
        String SendBicc10=mt103.getSender();
        String  RecvBicCc10 = RecvBicc10.substring(4,6);
        String  SendBicCc10 = SendBicc10.substring(4,6);
        String field57Ac10 =field57A.getValue().substring(4,6);

        String field59aAccount = null;
        if(mt103.getField59()!=null){
            if (mt103.getField59().getAccount() != null) {
                field59aAccount = mt103.getField59().getAccount();
            }
        }
        if(mt103.getField59A()!=null){
             if (mt103.getField59A().getAccount() != null) {
                field59aAccount = mt103.getField59A().getAccount();
            }
        }
        if(mt103.getField59F()!=null){
            if (mt103.getField59F().getAccount() != null) {
                field59aAccount = mt103.getField59F().getAccount();
            }
        }

        for(String wc10:wordsc10){
            if(RecvBicCc10.contains(wc10)&&SendBicCc10.contains(wc10)){
                //57A存在
                if(field57A!=null && StringUtils.isNotEmpty(field57A.getValue())){
                    for (String A:wordsc10){
                        if(field57Ac10.contains(A)){
                             if(field59aAccount==null&& StringUtils.isEmpty(field59aAccount)){
                                 mtResult.addDescription(Language.getLanguage("MT103STP_C10_2","【MT103_STP校验失败】:") );
                                 return  ;
                             }

                        }
                    }
                } //57A不存在
                else{
                    if(field59aAccount==null&& StringUtils.isEmpty(field59aAccount)){
                        mtResult.addDescription(Language.getLanguage("MT103STP_C10_1","【MT103_STP校验失败】:"));
                        return  ;
                    }
                }
            }
        }
        //return  mtResult;
    }
}
