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.field.Field;
import com.prowidesoftware.swift.model.mt.mt7xx.MT760;

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

/**
 * MT760
 * @author wanggang
 * @Date 2023-03-03
 */
public class MT760Validator extends AbsMTValidator {

    public MT760Validator(String mtStr, MtResult mtResult)
    {
        super(mtStr,mtResult);
    }

    @Override
    public void bussinessValidator() {
        // MtResult mtResult= getMtResult();
        MT760 mt760= (MT760) this.getAbstractMT();

        List<Field> fields = mt760.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, if field 23B is FIXD, then field 31E must be present, if field 23B is COND, then field 31E may be present, otherwise field 31E is not allowed (Error code(s): E01).
            //在序列B中，如果字段23B是FIXD，则字段31E必须存在；如果字段23B是COND，则可以存在字段31E，否则不允许出现字段31E,请检查!（错误代码：E01）。
        if (("FIXD".equals(fieldMap.get("23BSequenceB")) && !fieldMap.containsKey("31ESequenceB"))
            || (!"COND".equals(fieldMap.get("23BSequenceB")) && fieldMap.containsKey("31ESequenceB"))
            && (!"FIXD".equals(fieldMap.get("23BSequenceB")) && fieldMap.containsKey("31ESequenceB"))){
            mtResult.addDescription(Language.getLanguage("MT760_C1_1","[MT760]："));
        }
            //C1_2 In sequence C, if field 23B is FIXD, then field 31E must be present, if field 23B is COND, then field 31E may be present, otherwise field 31E is not allowed (Error code(s): E01).
                //在序列C中，如果字段23B是FIXD，则字段31E必须存在；如果字段23B是COND，则可以存在字段31E，否则不允许出现字段31E,请检查!（错误代码：E01）。
        if (("FIXD".equals(fieldMap.get("23BSequenceC")) && !fieldMap.containsKey("31ESequenceC"))
                || (!"COND".equals(fieldMap.get("23BSequenceC")) && fieldMap.containsKey("31ESequenceC"))
                && (!"FIXD".equals(fieldMap.get("23BSequenceC")) && fieldMap.containsKey("31ESequenceC"))){
            mtResult.addDescription(Language.getLanguage("MT760_C1_2","[MT760]："));
        }

        //C2_1 In sequence B, if field 23B is COND, then field 35G must be present, otherwise field 35G is not allowed (Error code(s): E02).
            //在序列B中，如果字段23B为COND，则字段35G必须存在，否则不允许字段35G,请检查!（错误代码：E02）。
        if (("COND".equals(fieldMap.get("23BSequenceB")) && !fieldMap.containsKey("35GSequenceB")) || (!"COND".equals(fieldMap.get("23BSequenceB")) && fieldMap.containsKey("35GSequenceB"))){
            mtResult.addDescription(Language.getLanguage("MT760_C2_1","[MT760]："));
        }
            //C2_2 In sequence C, if field 23B is COND, then field 35G must be present, otherwise field 35G is not allowed (Error code(s): E02).
                //在序列C中，如果字段23B为COND，则字段35G必须存在，否则不允许字段35G,请检查!（错误代码：E02）。
        if (("COND".equals(fieldMap.get("23BSequenceC")) && !fieldMap.containsKey("35GSequenceC")) || (!"COND".equals(fieldMap.get("23BSequenceC")) && fieldMap.containsKey("35GSequenceC"))){
            mtResult.addDescription(Language.getLanguage("MT760_C2_2","[MT760]："));
        }
        //C3_1 In sequence B, if field 23B is OPEN, then field 23F is not allowed (Error code(s): E03).
            //在序列B中，如果字段23B为OPEN，则不允许字段23F,请检查!（错误代码：E03）。
        if ("OPEN".equals(fieldMap.get("23BSequenceB")) && fieldMap.containsKey("23FSequenceB")){
            mtResult.addDescription(Language.getLanguage("MT760_C3_1","[MT760]："));
        }
            //C3_2 In sequence C, if field 23B is OPEN, then field 23F is not allowed (Error code(s): E03).
                //在序列C中，如果字段23B为OPEN，则不允许字段23F,请检查!（错误代码：E03）。
        if ("OPEN".equals(fieldMap.get("23BSequenceC")) && fieldMap.containsKey("23FSequenceC")){
            mtResult.addDescription(Language.getLanguage("MT760_C3_2","[MT760]："));
        }
        //C4 In sequence A, if field 22A is ISSU, then, in sequence B, field 50 must be present (Error code(s): C17).
            //在序列A中，如果字段22A是ISSU，那么在序列B中，字段50必须存在,请检查!（错误代码：C17）。
        if ("ISSU".equals(fieldMap.get("22ASequenceA")) && !fieldMap.containsKey("50SequenceB")){
            mtResult.addDescription(Language.getLanguage("MT760_C4","[MT760]："));
        }
        //C5_1 If in sequence A field 22A is ISSU and in sequence B field 22D is STBY, then field 49 must be present in sequence B (Error code(s): C18).
            //如果序列A中字段22A为ISSU，序列B中字段22D为STBY，则序列B中必须存在字段49,请检查!（错误代码：C18）。
        if ("ISSU".equals(fieldMap.get("22ASequenceA")) && "STBY".equals(fieldMap.get("22DSequenceB")) && !fieldMap.containsKey("49SequenceB")){
            mtResult.addDescription(Language.getLanguage("MT760_C5_1","[MT760]："));
        }
            //C5_2 In sequence B, if field 22D is DGAR, then field 49 is not allowed (Error code(s): C18).
                //在序列B中，如果字段22D是DGAR，则不允许字段49,请检查!（错误代码：C18）。
        if ("DGAR".equals(fieldMap.get("22DSequenceB")) && fieldMap.containsKey("49SequenceB")){
            mtResult.addDescription(Language.getLanguage("MT760_C5_2","[MT760]："));
        }
        //C6_1 In sequence A, if field 22A is ISCO or ICCO, then, in sequence B, fields 48D, 24E, and 24G are not allowed (Error code(s): C19).
            //在序列A中，如果字段22A是ISCO或ICCO，则在序列B中，不允许字段48D、24E和24G,请检查!（错误代码：C19）。
        if (("ISCO".equals(fieldMap.get("22ASequenceA")) || "ICCO".equals(fieldMap.get("22ASequenceA"))) && (fieldMap.containsKey("48DSequenceB") || fieldMap.containsKey("24ESequenceB") || fieldMap.containsKey("24GSequenceB")) ){
            mtResult.addDescription(Language.getLanguage("MT760_C6_1","[MT760]："));
        }
            //C6_2 In sequence A, if field 22A is ISCO or ICCO, then sequence C must be present, otherwise sequence C is not allowed (Error code(s): C19).
                //在序列A中，如果字段22A是ISCO或ICCO，则序列C必须存在，否则不允许序列C,请检查!（错误代码：C19）。
        if ((("ISCO".equals(fieldMap.get("22ASequenceA")) || "ICCO".equals(fieldMap.get("22ASequenceA"))) && !fieldMap.containsKey("15CSequenceC"))
        || ((!("ISCO".equals(fieldMap.get("22ASequenceA")) || "ICCO".equals(fieldMap.get("22ASequenceA"))) && fieldMap.containsKey("15CSequenceC")))){
            mtResult.addDescription(Language.getLanguage("MT760_C6_2","[MT760]："));
        }
        //C7 In sequence B, if field 57a is present, then field 56a must also be present (Error code(s): C81).
            //在序列B中，如果字段57a(57A/57D)存在，则字段56a(56A/56D)也必须存在,请检查!（错误代码：C81）。
        if ((fieldMap.containsKey("57ASequenceB") || fieldMap.containsKey("57DSequenceB")) && !(fieldMap.containsKey("56ASequenceB") || fieldMap.containsKey("56DSequenceB"))){
            mtResult.addDescription(Language.getLanguage("MT760_C7","[MT760]："));
        }
        //C8_1 In sequence B, if field 23F is absent, then fields 78, 26E, and 31S are not allowed (Error code(s): C16).
            //在序列B中，如果字段23F不存在，则不允许字段78、26E和31S,请检查!（错误代码：C16）。
        if (!fieldMap.containsKey("23FSequenceB") && (fieldMap.containsKey("78SequenceB") || fieldMap.containsKey("26ESequenceB") || fieldMap.containsKey("31SSequenceB")) ){
            mtResult.addDescription(Language.getLanguage("MT760_C8_1","[MT760]："));
        }
            //C8_2 In sequence C, if field 23F is absent, then fields 78, 26E, and 31S are not allowed (Error code(s): C16).
                //在序列C中，如果字段23F不存在，则不允许字段78、26E和31S,请检查!（错误代码：C16）。
        if (!fieldMap.containsKey("23FSequenceC") && (fieldMap.containsKey("78SequenceC") || fieldMap.containsKey("26ESequenceC") || fieldMap.containsKey("31SSequenceC"))){
            mtResult.addDescription(Language.getLanguage("MT760_C8_2","[MT760]："));
        }
        //C9 In sequence B, if field 49 is CONFIRM or field 49 is MAY ADD, then field 58a must be present, otherwise field 58a is not allowed (Error code(s): C20).
            //在序列B中，如果字段49是CONFIRM或字段49是MAY ADD，则字段58a(58A/58D)必须存在，否则不允许字段58a(58A/58D),请检查!（错误代码：C20）。
        if (("CONFIRM".equals(fieldMap.get("49SequenceB")) || "MAY ADD".equals(fieldMap.get("49SequenceB"))) && !(fieldMap.containsKey("58ASequenceB") || fieldMap.containsKey("58DSequenceB"))||
                !("CONFIRM".equals(fieldMap.get("49SequenceB")) || "MAY ADD".equals(fieldMap.get("49SequenceB"))) && (fieldMap.containsKey("58ASequenceB") || fieldMap.containsKey("58DSequenceB"))
        ){
            mtResult.addDescription(Language.getLanguage("MT760_C9","[MT760]："));
        }
        //C10 In sequence C, if field 22Y is present, then field 22K must be present (Error code(s): C33).
            //在序列C中，如果字段22Y存在，则字段22K必须存在,请检查!（错误代码：C33）。
        if (fieldMap.containsKey("22YSequenceC") && !fieldMap.containsKey("22KSequenceC")){
            mtResult.addDescription(Language.getLanguage("MT760_C10","[MT760]："));
        }
        //C11_1 In sequence B, if field 22D is DGAR, then field 41a is not allowed (Error code(s): C21).
            //在序列B中，如果字段22D是DGAR，则不允许字段41a(41F/41G),请检查!（错误代码：C21）。
        if ("DGAR".equals(fieldMap.get("22DSequenceB")) && fieldMap.containsKey("41FSequenceB") || fieldMap.containsKey("41GSequenceB")){
            mtResult.addDescription(Language.getLanguage("MT760_C11_1","[MT760]："));
        }
            //C11_2 In sequence C, if field 22D is DGAR, then field 41a is not allowed (Error code(s): C21).
                //在序列C中，如果字段22D是DGAR，则不允许字段41a(41F/41G),请检查!（错误代码：C21）。
        if ("DGAR".equals(fieldMap.get("22DSequenceC")) && fieldMap.containsKey("41FSequenceC") || fieldMap.containsKey("41GSequenceC")){
            mtResult.addDescription(Language.getLanguage("MT760_C11_2","[MT760]："));
        }
        //  return  mtResult;
    }

}