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

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.Field19;
import com.prowidesoftware.swift.model.field.Field32B;
import com.prowidesoftware.swift.model.mt.mt2xx.MT203;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author huangmengyuan
 * @create 2023/3/3
 */
public class MT203Validator extends AbsMTValidator {
    public MT203Validator(String mtStr, MtResult mtResult) {
        super(mtStr, mtResult);
    }

    @Override
    public void bussinessValidator() {
        //MtResult mtResult = getMtResult();
        MT203 mt203 = (MT203) this.getAbstractMT();
        List<Field32B> field32B = mt203.getField32B();
        Field19 field19 = mt203.getField19();
        BigDecimal sum = new BigDecimal("0");
        for (Field32B temp : field32B) {
            sum = sum.add(temp.amount());
            //循环域内32B币种相同
            if (!temp.getCurrency().equals(field32B.get(0).getCurrency())) {
//                mtResult.addDescription("【MT203】中循环域内Field32B金额类型需保持一致，请检查");
                mtResult.addDescription(Language.getLanguage("MT203_C1","【MT203】"));
            }
        }
        if (!field19.amount().equals(sum) ) {
//            mtResult.addDescription("【MT203】中Field19需与32B金额总和一致，请检查");
            mtResult.addDescription(Language.getLanguage("MT203_C2","【MT203】"));
        }
        //循环域至少2，不超过10 ,（32B必填）
        if (field32B.size() < 2 || field32B.size() > 10) {
//            mtResult.addDescription("【MT203】中循环域至少出现2次,至多出现10次,请检查");
            mtResult.addDescription(Language.getLanguage("MT203_C3","【MT203】"));
        }


        List<MT203.Loop1> loop1List = mt203.getLoop1List();
        for (MT203.Loop1 tags : loop1List) {
            if (tags.containsTag(56)){
                if (!tags.containsTag(57)){
//                    mtResult.addDescription("【MT203】中事务中如果56A或者56D存在,那么57A或者57B或者57D也必须存在,请检查");
                    mtResult.addDescription(Language.getLanguage("MT203_C4","【MT203】"));
                }
            }
        }


        //  return mtResult;
    }
}
