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


import com.brilliance.isc.swift.objmtconvert.model.AbsMTCreator;
import com.brilliance.isc.swift.objmtconvert.result.MtResult;
import com.brilliance.isc.swift.objmtconvert.util.MTUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.*;

/**
 * @author gechengyang
 * @date 2023/2/27
 */
public class MtCfg {
    private static Properties MtCfgProps = new Properties();

    private static Properties LanguageProps = new Properties();

    public static  String LANGUAGE="CHINESE";
   // private static Map<String,String> MtCfgMap = new HashMap<String,String>();

    private static Map<String,MtAttribute> mtAttributeMap=new HashMap<>();
    private static   boolean validator=true;
    static{
        try {
            MtCfgProps.load(MtCfg.class.getClassLoader().getResourceAsStream("swift/mtcfg.properties"));
            for(Map.Entry<Object, Object> entry :MtCfgProps.entrySet())
            {
                String key = (String)entry.getKey();
                String value = (String) entry.getValue();
                //MtCfgMap.put(key,value);
                if(StringUtils.isEmpty(value)){
                  value="true;false";
                }
                String[] values=value.split(";");
                if(values.length>=2)
                {
                    MtAttribute mtAttribute=new MtAttribute(key,Boolean.valueOf(values[0]),Boolean.valueOf(values[1]));
                    mtAttributeMap.put(key,mtAttribute);
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        try {
            LanguageProps.load(MtCfg.class.getClassLoader().getResourceAsStream("swift/Language/language.properties"));
            for(Map.Entry<Object, Object> entry :LanguageProps.entrySet())
            {
                String key = (String)entry.getKey();
                String value = (String) entry.getValue();
                LANGUAGE=value;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *校验MT报文Tag的格式
     * @param absMTCreator
     * @return
     */
    public static  boolean validatorMtTags(AbsMTCreator absMTCreator)
    {
        String msgType=absMTCreator.getMsgType();
        boolean validator=true;
        MtAttribute mtAttribute = mtAttributeMap.get(msgType);
        if(mtAttribute!=null)
        {
            validator=mtAttribute.isCheckTags();
        }
        return  validator;
    }

    /**
     * 校验MT报文的业务栏位格式
     * @param absMTCreator
     * @return
     */
    public static  boolean validatorMtBusiness(AbsMTCreator absMTCreator)
    {
        String msgType=absMTCreator.getMsgType();
        boolean validator=false;
        MtAttribute mtAttribute = mtAttributeMap.get(msgType);
        if(mtAttribute!=null)
        {
            validator=mtAttribute.isCheckBussiness();
        }
        return  validator;
    }
    /**
     * 校验MT报文的业务栏位格式
     * @param mtResult
     * @return
     */
    public static  boolean validatorMtBusiness(MtResult mtResult)
    {
        //String msgType=mtResult.getAbstractMT().getMessageType();
        String msgType= MTUtil.getSwiftMtType(mtResult);
        boolean validator=false;
        MtAttribute mtAttribute = mtAttributeMap.get(msgType.substring(0,3));
        if(mtAttribute!=null)
        {
            validator=mtAttribute.isCheckBussiness();
        }else {
            return false;
        }
        return  validator;
    }

}
