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

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.Field;
import com.prowidesoftware.swift.model.field.Field32A;
import com.prowidesoftware.swift.model.field.Field32B;
import com.prowidesoftware.swift.model.field.Field32K;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author zh
 * @create 2023-03-03-15:08
 */
public class ValidatorMethod {

    public static MtResult mt4xxField202132(List<Tag> tags, MtResult mtResult,String msgType){
        int count20 = 0;
        for (Tag tag : tags) {
            if(tag.getName().contains("20")){
                count20++;
            }
        }
        if(count20>10){
            mtResult.addDescription(Language.getLanguage("MT410_C1","【"+msgType+"】"));
        }
        return mtResult;
    }

    public static MtResult mt4xxField32(List<Field32A> listField32A, List<Field32B> listField32B, List<Field32K> listField32K, MtResult mtResult,String msgType){
        String field32ACurrency = "";
        String field32BCurrency = "";
        String field32KCurrency = "";
        boolean flag = false;
        if(listField32A!=null&&listField32A.size()>0){
            field32ACurrency = listField32A.get(0).getCurrency();
            for (Field32A field32A : listField32A) {
                if(!field32ACurrency.equals(field32A.getCurrency())){
                    flag = true;
                }
            }
        }
        if(listField32B!=null&&listField32B.size()>0){
            field32BCurrency = listField32B.get(0).getCurrency();
            for (Field32B field32B : listField32B) {
                if(!field32BCurrency.equals(field32B.getCurrency())){
                    flag = true;
                }
            }
        }
        if(listField32K!=null&&listField32K.size()>0){
            field32KCurrency =  listField32K.get(0).getCurrency();
            for (Field32K field32K : listField32K) {
                if(!field32KCurrency.equals(field32K.getCurrency())){
                    flag = true;
                }
            }
        }
        if(StringUtil.isNotEmpty(field32ACurrency)&&StringUtil.isNotEmpty(field32BCurrency)){
            if(!field32ACurrency.equals(field32BCurrency)){
                flag = true;
            }
        }
        if(StringUtil.isNotEmpty(field32ACurrency)&&StringUtil.isNotEmpty(field32KCurrency)){
            if(!field32ACurrency.equals(field32KCurrency)){
                flag = true;
            }
        }
        if(StringUtil.isNotEmpty(field32BCurrency)&&StringUtil.isNotEmpty(field32KCurrency)){
            if(!field32BCurrency.equals(field32KCurrency)){
                flag = true;
            }
        }
        if(flag){
            mtResult.addDescription(Language.getLanguage("MT410_C2","【"+msgType+"】"));
        }
        return mtResult;
    }


    /**
     * 判断指定的报文域是否为空。<br/>
     * 使用了getValue()进行判断，对于50a等域可能会出问题，到时候再排查。
     * @param field 要判断的域。
     * @return 指定域是否为空。true表示为空。
     */
    public static boolean isFieldEmpty(Field field) {
        return field == null || StringUtils.isEmpty(field.getValue());
    }

    public static boolean isFieldListEmpty(List<Field> list) {
        return (list == null || list.size()==0);
    }

    /**
     * 判断指定的报文域列表是否全为空。<br/>
     * 调用了isFieldEmpty方法。isFieldEmpty方法改动后也要测试本方法是否能正常运行。<br/>
     * @param list 要判断的域的列表。
     * @return 指定列表是否全为空。true表示全为空。
     */
    public static boolean isFieldListAllEmpty(List<Field> list) {
        for (Field field:list ) {
            if(!isFieldEmpty(field)) { return false; }
        }
        return true;
    }
}
