package com.brilliance.isc.swift.objmtconvert;

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.Tag;
import com.prowidesoftware.swift.model.mt.AbstractMT;
import com.prowidesoftware.swift.model.mt.mt7xx.MT700;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gechengyang
 * @date 2023/2/14
 */
public class ObjAndMtConvert {


    protected MtResult result = new MtResult();
    protected AbstractMT abstractMT;
    private String mtType;

    /**
     * 对象转MT报文
     *
     * @param mtType
     */
    public ObjAndMtConvert(String mtType) {
        this.mtType = mtType;
        initAbstractMt();
    }

    /**
     * MT报文转MT对象
     *
     * @param file
     */
    public ObjAndMtConvert(File file) {
        try {
            String mtStr = FileUtils.readFileToString(file);
            this.abstractMT = AbstractMT.parse(String.valueOf(mtStr));
            MT700 mt700 = (MT700) abstractMT;
            System.out.println(111);
        } catch (IOException e) {
            setError(e);
            throw new RuntimeException(e);
        }
    }


    public MtResult getResult() {
        result.setMessage(this.abstractMT.message());
        return result;
    }

    public void setResult(MtResult result) {
        this.result = result;
    }

    public AbstractMT getAbstractMT() {
        return abstractMT;
    }

    public void setAbstractMT(AbstractMT abstractMT) {
        this.abstractMT = abstractMT;
    }

    private void setError(Exception e) {
        result.setSuccess(false);
        result.addDescription(e.getMessage());
    }

    public void initAbstractMt() {

        //String className = "com.prowidesoftware.swift.model.mt" + mtType.charAt(0) + "xx" + ".MT" + mtType;
        String className = "com.prowidesoftware.swift.model.mt" + mtType.charAt(0) + "xx.MT" + mtType;
        className = className.trim();
        try {
            Object obj = Class.forName(className).newInstance();
            this.abstractMT = (AbstractMT) obj;
        } catch (Exception e) {
            setError(e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 设置发报行
     * @param bic
     */
    public void setSender(String bic) {
        this.abstractMT.setSender(bic);
    }

    /**
     * 设置收报行
     * @param bic
     */
    public void setReceiver(String bic) {
        this.abstractMT.setReceiver(bic);
    }

    /**
     * 设置某个单一域，value可以是String和List类型
     * @param tag
     * @param value
     */
    public void set(String tag, Object value) {
        if(value instanceof String)
        {
            this.abstractMT.set(tag, (String) value);
        } else if (value instanceof List) {
            List<String> list= (List<String>) value;
            StringBuffer sb=new StringBuffer();
            for (int i=0;i<list.size();i++) {
                String currentStr=list.get(i);
                if(i!=(list.size()-1))
                {
                    sb.append(currentStr+ Constans.NEW_LINE);
                }
                else
                {
                    sb.append(currentStr);
                }
            }
            set(tag,sb.toString());
        }

        else  if( value instanceof BigDecimal)
        {
            String newValue= String.valueOf(value).replace(".",",");
            set(tag,newValue);
        }
        else
        {
//            String errDsp="tag【"+tag+"】的数据类型是"+getDataType(value)+",目前仅只支持String、BigDecimal、List类型，请检查!";
            String errDsp= Language.getLanguage("SYS_C25")+tag+Language.getLanguage("SYS_C26")+getDataType(value)+Language.getLanguage("SYS_C27");

            Exception e=new RuntimeException(errDsp);
            setError(e);
            throw new RuntimeException(e);
        }
    }

    private String getDataType(Object obj)
    {
        return  obj.getClass().getTypeName();
    }

    /**
     * 设置某个循环域，tag一致，但是对应的value不同
     * @param tag
     * @param value
     * @param cycle
     */
    public void set(String tag, Object value, boolean cycle) {
        if(cycle)
        {
            List<String> list= (List<String>) value;
            for (String val:list) {
                this.abstractMT.set(tag,val);
            }
        }
        else
        {
            set(tag, value);
        }
    }



    public String getSender()
    {
        return this.abstractMT.getSender();
    }

    public String getReceiver() {
        return this.abstractMT.getReceiver();
    }

    public Object get(String tagKey) {

        return abstractMT.getSwiftMessage().getBlock4().getTagByName(tagKey).getValue();
    }

    public Object get(String tagKey, boolean cycle) {
        if(cycle)
        {
          Tag[] tags= abstractMT.getSwiftMessage().getBlock4().getTagsByName(tagKey);
          List<String> list=new ArrayList<>();
            for (Tag tag:tags) {
                list.add(tag.getValue());
            }
            return list;
        }
        else
            return  get(tagKey);

    }



}
