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.Field12;
import com.prowidesoftware.swift.model.field.Field34F;
import com.prowidesoftware.swift.model.mt.mt9xx.MT920;

import java.util.List;

/**
 * @author zh
 * @create 2023-03-03-10:27
 */
public class MT920Validator extends AbsMTValidator {

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

    @Override
    public void bussinessValidator() {
        // MtResult mtResult=getMtResult();
        MT920 mt920 = (MT920) this.getAbstractMT();
        List<Field12> listField12 = mt920.getField12();
        boolean flag = false;
        for (Field12 field12 : listField12) {
            if(field12.getValue().contains("942")){
                flag = true;
            }
        }
        List<Tag> tags = this.getAbstractMT().getSwiftMessage().getBlock4().getTags();
        //count1表示循环节点出现的次数
        //count2表示Debit/(Debit and Credit) Floor Limit Indicator出现的次数
        //count3表示当只有一个34F时出现的次数
        //count4表示2个34F均出现合在一起的次数
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        //标记第一次34F出现
        boolean flag2 = true;
        //标记34F的币种不同
        boolean flag3 = false;
        //标记是否存在34F
        boolean flag4 = false;
        String currency = "";
        for (Tag tag : tags) {
            if(tag.getName().contains("12")){
                count1++;
            }
            if(tag.getName().contains("34")){
                flag4 = true;
                Field34F field34F = (Field34F) tag.asField();
                if("D".equals(field34F.getDCMark())|| StringUtil.isEmpty(field34F.getDCMark())){
                    count2++;
                }
                if(StringUtil.isEmpty(field34F.getDCMark())){
                    count3++;
                }
                if("D".equals(field34F.getDCMark())|| "C".equals(field34F.getDCMark())){
                    count4++;
                }
                if (flag2){
                    currency = field34F.getCurrency();
                    flag2 = false;
                }else {
                    String currency2 = field34F.getCurrency();
                    if(!currency.equals(currency2)){
                        flag3 = true;
                    }
                }
            }
        }
        if (flag) {
            if(count1 != count2){
                mtResult.addDescription(Language.getLanguage("MT920_C1","【MT920】"));
            }
        }
        if(flag4){
            //除2会强转，所以左右2边都乘以2比较，不相等的话证明不符合规则
            if((count1*2) != (count3*2+count4)){
                mtResult.addDescription(Language.getLanguage("MT920_C2","【MT920】"));
            }
            if(flag3){
                mtResult.addDescription(Language.getLanguage("MT920_C3","【MT920】"));
            }
        }
        //    return mtResult;
    }
}
