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


import com.brilliance.isc.swift.objmtconvert.model.AbsMTValidator;
import com.brilliance.isc.swift.objmtconvert.result.MtResult;
import com.brilliance.isc.swift.objmtconvert.util.Language;
import com.prowidesoftware.swift.model.Tag;
import com.prowidesoftware.swift.model.field.Field;
import com.prowidesoftware.swift.model.mt.mt7xx.MT767;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MT767Validator extends AbsMTValidator {
    public MT767Validator(String mtStr, MtResult mtResult) {
        super(mtStr,mtResult);
    }
    @Override
    public void bussinessValidator() {
        //    MtResult mtResult = getMtResult();
        List<Tag> tags = this.getAbstractMT().getSwiftMessage().getBlock4().getTags();
        MT767 mt767= (MT767) this.getAbstractMT();
        List<Field> fields = mt767.getFields();
        Map<String,String> fieldMap = new HashMap();
        // 按照序列拆分
        String sequence = "A";
        for (Field field: fields) {
            if (field.getName().equals("15B"))
                sequence = "B";
            else if (field.getName().equals("15C"))
                sequence = "C";

            if (sequence.equals("A"))
                fieldMap.put(field.getName() + "SequenceA", field.getValue());
            else if (sequence.equals("B"))
                fieldMap.put(field.getName() + "SequenceB", field.getValue());
            else
                fieldMap.put(field.getName() + "SequenceC", field.getValue());
        }
        //C1_1 In sequence B, either field 32B or field 33B, but not both, may be present (Error code(s): C12).
            //在B序列中，32B和33B不能同时存在，请检查!（错误代码：C12）。
        if (fieldMap.containsKey("32BSequenceB") && fieldMap.containsKey("33BSequenceB")){
            mtResult.addDescription(Language.getLanguage("MT767_C1_1","[MT767]："));
        }
            //C1_2 In sequence C, either field 32B or field 33B, but not both, may be present (Error code(s): C12).
                //在C序列中，32B和33B不能同时存在，请检查!（错误代码：C12）。
        if (fieldMap.containsKey("32BSequenceC") && fieldMap.containsKey("33BSequenceC")){
            mtResult.addDescription(Language.getLanguage("MT767_C1_2","[MT767]："));
        }

        //C2_1 In sequence B, if field 23B is COND, then field 35G must be present in sequence B, otherwise field 35G is not allowed (Error code(s): E02).
            //在序列B中，如果字段23B为COND，则序列B中必须存在字段35G，否则不允许字段35G,请检查!（错误代码：E02）。
        if (!fieldMap.containsKey("23BSequenceB") || (fieldMap.get("23BSequenceB").equals("COND") && !fieldMap.containsKey("35GSequenceB")) || ((!fieldMap.get("23BSequenceB").equals("COND") && fieldMap.containsKey("35GSequenceB")))){
            mtResult.addDescription(Language.getLanguage("MT767_C2_1","[MT767]："));
        }
            //C2_2 In sequence C, if field 23B is COND, then field 35G must be present in sequence C, otherwise field 35G is not allowed (Error code(s): E02).
                //在序列C中，如果字段23B为COND，则序列C中必须存在字段35G，否则不允许字段35G,请检查!（错误代码：E02）。
        if (!fieldMap.containsKey("23BSequenceC") || (fieldMap.get("23BSequenceC").equals("COND") && !fieldMap.containsKey("35GSequenceC")) || ((!fieldMap.get("23BSequenceC").equals("COND") && fieldMap.containsKey("35GSequenceC")))){
            mtResult.addDescription(Language.getLanguage("MT767_C2_2","[MT767]："));
        }

        //C3 In sequence A, if field 22A is ISCA or ICCA, then sequence C must be present, otherwise sequence C is not allowed (Error code(s): C19).
            //在序列A中，如果字段22A是ISCA或ICCA，则序列C必须存在，否则不允许序列C,请检查!（错误代码：C19）。
        if((fieldMap.get("22ASequenceA").equals("ISCA") && !fieldMap.containsKey("15CSequenceC"))
                && (!fieldMap.get("22ASequenceA").equals("ICCA") && fieldMap.containsKey("15CSequenceC"))
                || (!fieldMap.get("22ASequenceA").equals("ISCA") && fieldMap.containsKey("15CSequenceC"))){
            mtResult.addDescription(Language.getLanguage("MT767_C3","[MT767]："));
        }

        //C4 In sequence C, if field 15C is present, then at least one of the other fields of sequence C must be present (Error code(s): C98).
            //在序列C中，如果存在字段15C，则序列C中的至少一个其他字段必须存在,请检查!（错误代码：C98）。
        if (fieldMap.containsKey("15CSequenceC")){
            if (!fieldMap.containsKey("32BSequenceC") && !fieldMap.containsKey("33BSequenceC") &&
                    !fieldMap.containsKey("23BSequenceC") && !fieldMap.containsKey("31ESequenceC") &&
                    !fieldMap.containsKey("35GSequenceC") && !fieldMap.containsKey("59SequenceC") &&
                    !fieldMap.containsKey("77LSequenceC") && !fieldMap.containsKey("24ESequenceC") &&
                    !fieldMap.containsKey("24GSequenceC")){
                mtResult.addDescription(Language.getLanguage("MT767_C4","[MT767]："));
            }
        }
        //  return mtResult;
    }
}
