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.Constans;
import com.brilliance.isc.swift.objmtconvert.util.Language;
import com.prowidesoftware.swift.model.field.*;
import com.prowidesoftware.swift.model.mt.mt9xx.MT972;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.brilliance.isc.swift.objmtconvert.model.busvalidator.ValidatorMethod.isFieldEmpty;

/**
 * 972报文的校验类，校验规则如下：<br/>
 * 1.60a, 62a, 64的币种的前两个字母必须相同。不符合时报错C27.<br/>
 * 来源：<a href="https://www2.swift.com/knowledgecentre/publications/us9m_20220722/2.0?topic=mt972-network-rules.htm">
 *     MT 972 Network Validated Rules</a>
 * @author liusheng
 */
public class MT972Validator extends AbsMTValidator {
    public MT972Validator (String mtStr, MtResult mtResult) { super(mtStr, mtResult); }
    @Override
    public void bussinessValidator() {
        // MtResult mtResult = getMtResult();
        MT972 mt972 = (MT972) getAbstractMT();
        //校验规则1.
        //60a, 62a, 64的币种的前两个字母必须相同。
        List<Field> fieldList = new ArrayList<>();
        fieldList.add(mt972.getField60F());
        fieldList.add(mt972.getField60M());
        fieldList.add(mt972.getField62F());
        fieldList.add(mt972.getField62M());

        Set<String> cur = new HashSet<>();
        Field64 field64 = mt972.getField64();
        String field64Cur = null;
        for (Field field:fieldList) {
            if(field != null) {
                //getComponent3即可得到币种。截取前两个字母。
                cur.add(field.getComponent(3).substring(0, 2));
            }
        }
        //截取64域的币种的前两个字母。
        if(!isFieldEmpty(field64)) { field64Cur = field64.getCurrency(); }
        if(field64Cur != null) { cur.add(field64Cur.substring(0, 2)); }
        //由于set中的元素不重复，当set中有多于一个的元素时说明有币种的前两个字母不相同。
        if(cur.size() > 1) {
            mtResult.addDescription(Language.getLanguage("MT972_C1","【MT972】"));
        }

        //   return mtResult;
    }
}
