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

import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.swift.objmtconvert.result.MtResult;
import com.brilliance.isc.swift.objmtconvert.util.Constans;
import com.brilliance.isc.swift.objmtconvert.util.JsonUtil;
import com.brilliance.isc.swift.objmtconvert.util.Language;
import com.brilliance.isc.swift.swifteditor.util.SwiftUtils;
import com.prowidesoftware.swift.model.SwiftBlock3;
import com.prowidesoftware.swift.model.mt.AbstractMT;

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

/**
 * @author gechengyang
 * @date 2023/2/17
 */
public abstract class AbsMTCreator extends AbsCreator{

    protected AbstractMT abstractMT = null;

    private String jsonData;

    private Map jsonMap = new HashMap();

    private String msgType = "";

   // private MtResult mtResult = new MtResult();

    private String sendBic;

    private String recvBic;


    protected  MtResult mtResult;


    public AbsMTCreator(String jsonData,Object obj) {
        this.jsonData = jsonData;
        this.jsonMap = JsonUtil.json2Map(jsonData);
        this.msgType = (String) JsonUtil.getJsonObj(jsonData).get(Constans.MSG_TYPE);
        initAbstractMT();

    }

    public AbsMTCreator(String msgType) {
        this.msgType = msgType;
        initAbstractMT();
    }


    public AbsMTCreator()  {
    }

    public String getSendBic() {
        return sendBic;
    }

    public void setSendBic(String sendBic) {
        this.sendBic = sendBic;
        abstractMT.setSender(this.sendBic);
    }

    public String getRecvBic() {
        return recvBic;
    }

    public void setRecvBic(String recvBic) {
        this.recvBic = recvBic;
        abstractMT.setReceiver(this.recvBic);
    }

    public Map getJsonMap() {
        return jsonMap;
    }

    public void setJsonMap(Map jsonMap) {
        this.jsonMap = jsonMap;
    }

    protected void initAbstractMT() {
        String className = "com.prowidesoftware.swift.model.mt.mt" + msgType.charAt(0) + "xx" + ".MT" + msgType;
        className = className.trim();
        try {
            Object obj = Class.forName(className).newInstance();
            this.abstractMT = (AbstractMT) obj;
            //block3插入108
            if (SettleContext.isSavTransSta()) {
                SwiftBlock3 swiftBlock3 = this.abstractMT.getSwiftMessage().getBlock3();
                if (swiftBlock3 == null) {
                    swiftBlock3 = new SwiftBlock3();
                }
                swiftBlock3.builder().setField108(SwiftUtils.buildSwiftBlock3_108());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
     //   return abstractMT;
    }

    public AbstractMT getAbstractMT() {
        return abstractMT;
    }

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

    public String getJsonData() {
        return jsonData;
    }

    public void setJsonData(String jsonData) {
        this.jsonData = jsonData;
        this.jsonMap = JsonUtil.json2Map(jsonData);
    }

    public String getMsgType() {
        return msgType;
    }

    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }


    public void setSender() {
        String sendBic = (String) this.jsonMap.get(Constans.SEND_BIC);
        this.abstractMT.setSender(sendBic);
    }

    public void setReceiver() {
        String recvBic = (String) this.jsonMap.get(Constans.RECEIVE_BIC);
        this.abstractMT.setReceiver(recvBic);
    }


    public MtResult getMtResult() {
        return mtResult;
    }

    public void setMtResult(MtResult mtResult) {
        this.mtResult = mtResult;
    }

    public void set(String tag, Object value) {
        if (value == null) {
//            Exception e = new RuntimeException("the value of tag【" + tag + "】 is null,please check!");
            return;
            //throw new RuntimeException(e);
        }
        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);
            throw new RuntimeException(e);
        }
    }

    /**
     * 设置某个循环域，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);
        }
    }

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

    public   void pack(){

    }



    public void dealTags(List<AbsTagCreate> tagCreateList)
    {
        for(AbsTagCreate tag:tagCreateList)
        {
            tag.creator();
        }
    }
}
