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

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.brilliance.swift.util.StringUtil;
import com.prowidesoftware.swift.model.Tag;
import com.prowidesoftware.swift.model.field.*;
import com.prowidesoftware.swift.model.mt.mt9xx.MT942;
import java.util.*;

/**
 * @author wuzhe
 * @create 2023-03-08
 */
public class MT942Validator extends AbsMTValidator {

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

    @Override
    public void bussinessValidator() {
        //   MtResult mtResult=getMtResult();
        List<Tag> tags = this.getAbstractMT().getSwiftMessage().getBlock4().getTags();
        //有一个34F必填所以先获得第一个34F的currency
        MT942 mt942 = (MT942) this.getAbstractMT();
        boolean flag = false;
        List<Field34F> listField34F = mt942.getField34F();
        String field34FCurrency = listField34F.get(0).getCurrency().substring(0,2);
        Field90C field90C = mt942.getField90C();
        if(field90C!=null){
            String field90CCurrency = field90C.getCurrency().substring(0,2);
            if(!field34FCurrency.equals(field90CCurrency)){
                flag = true;
            }
        }
        Field90D field90D = mt942.getField90D();
        if(field90D!=null){
            String field90DCurrency = field90D.getCurrency().substring(0,2);
            if(!field34FCurrency.equals(field90DCurrency)){
                flag = true;
            }
        }
        //C2 When only one field 34F is present, the second subfield must not be used. When both fields 34F are present, subfield 2 of the first 34F must contain the value 'D', and subfield 2 of the second 34F must contain the value 'C' (Error code(s): C23).
            //当仅存在一个字段34F时，不得使用第二个子字段。当两个字段34F都存在时，第一个34F的子字段2必须包含值“D”，第二个34F中的子字段2中必须包含值‘C’（错误代码：C23）。
        if(listField34F.size()==2){
            System.out.println(listField34F.get(0));
            String dcMark1 = listField34F.get(0).getDCMark();
            String dcMark2 = listField34F.get(1).getDCMark();
            if(!("D".equals(dcMark1)&&"C".equals(dcMark2))){
                mtResult.addDescription(Language.getLanguage("MT942_C2","[MT942]："));
                //mtResult.addDescription("[MT942]：当两个34F都存在时，第一个34F的子字段2必须包含值“D”，第二个34F的子字段2必须包含值“C”，请检查!");
            }
            String field34FCurrency2 = listField34F.get(1).getCurrency().substring(0,2);
            if(!field34FCurrency.equals(field34FCurrency2)){
                flag = true;
            }
        }else {
            String dcMark = listField34F.get(0).getDCMark();
            if(StringUtil.isNotEmpty(dcMark)){
                mtResult.addDescription(Language.getLanguage("MT942_C2","[MT942]："));
                //mtResult.addDescription("[MT942]：当只有一个字段34F时，不能使用第二个子字段，请检查!");
            }
        }
        //C1 The first two characters of the three character currency code in fields 34F, 90D, and 90C must be the same (Error code(s): C27).
            //34F、90D、90C三个字符货币代码的前两个字符必须相同，请检查!（错误代码：C27）。
        if(flag){
            mtResult.addDescription(Language.getLanguage("MT942_C1","[MT942]："));
        }
        //C3 If field 86 is present in any occurrence of the repetitive sequence, it must be preceded by a field 61 except if that field 86 is the last field in the message, then field 61 is optional. In addition, if field 86 is present, it must be on the same page (message) of the statement as the related field 61 (Error code(s): C24).
            //如果字段86出现在重复序列的任何位置，则必须在它前面加上字段61，除非该字段86是消息中的最后一个字段，那么字段61是可选的，请检查!（错误代码：C24）。
        List<Field86> listField86 = mt942.getField86();
        List<Field61> listField61 = mt942.getField61();
        if(listField86!=null){
            int field86Size = listField86.size();
            if(field86Size!=1){
                int field61Size = listField61.size();
                int size = field86Size - field61Size;
                if(!(size==0||size==1)){
                    mtResult.addDescription(Language.getLanguage("MT942_C3","[MT942]："));
                }
            }
        }
        //   return mtResult;
    }
}
