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

import com.brilliance.isc.swift.objmtconvert.annotation.model.MtCfg;
import com.brilliance.isc.swift.objmtconvert.model.AbsMTCreator;
import com.brilliance.isc.swift.objmtconvert.model.mt1xx.*;
import com.brilliance.isc.swift.objmtconvert.model.mt2xx.*;
import com.brilliance.isc.swift.objmtconvert.model.mt2xx.MT205COVCreator;
import com.brilliance.isc.swift.objmtconvert.model.mt2xx.MT205Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt4xx.MT400Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt7xx.*;
import com.brilliance.isc.swift.objmtconvert.model.mt9xx.*;
import com.brilliance.isc.swift.objmtconvert.result.Mt2ObjResult;
import com.brilliance.isc.swift.objmtconvert.result.MtResult;
import com.brilliance.isc.swift.objmtconvert.util.*;
import com.google.gson.Gson;
import com.prowidesoftware.swift.model.SwiftBlock3;
import com.prowidesoftware.swift.model.mt.AbstractMT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @author gechengyang
 * @date 2023/2/18
 */
public class MtCreatorManager {

    private static Logger log = LoggerFactory.getLogger(MtCreatorManager.class);

    private static AbsMTCreator getMTCreator(String jsonData) {
        String msgType = MTUtil.getSwiftType(jsonData);
        AbsMTCreator absMTCreator = null;
        try {
            if ("700".equals(msgType))
                absMTCreator = new MT700Creator(msgType);
            else if ("710".equals(msgType)) {
                absMTCreator = new MT710Creator(msgType);
            }else if ("711".equals(msgType)) {
                absMTCreator = new MT711Creator(msgType);
            }else if ("732".equals(msgType)) {
                absMTCreator = new MT732Creator(msgType);
            }else if ("734".equals(msgType)) {
                absMTCreator = new MT734Creator(msgType);
            }else if ("740".equals(msgType)) {
                absMTCreator = new MT740Creator(msgType);
            }else if ("752".equals(msgType)) {
                absMTCreator = new MT752Creator(msgType);
            }else if ("769".equals(msgType)) {
                absMTCreator = new MT769Creator(msgType);
            }else if ("935".equals(msgType)) {
                absMTCreator = new MT935Creator(msgType);
            }else if ("920".equals(msgType)) {
                absMTCreator = new MT935Creator(msgType);
            }else if ("941".equals(msgType)) {
                absMTCreator = new MT941Creator(msgType);
            }else if ("942".equals(msgType)) {
                absMTCreator = new MT941Creator(msgType);
            }else if ("971".equals(msgType)) {
                absMTCreator = new MT971Creator(msgType);
            }else if ("972".equals(msgType)) {
                absMTCreator = new MT972Creator(msgType);
            }else if ("973".equals(msgType)) {
                absMTCreator = new MT973Creator(msgType);
            }else if ("985".equals(msgType)) {
                absMTCreator = new MT985Creator(msgType);
            }else if ("986".equals(msgType)) {
                absMTCreator = new MT986Creator(msgType);
            }else if("400".equals(msgType)){
                absMTCreator = new MT940Creator(msgType);
            }else if("940".equals(msgType)){
                absMTCreator = new MT400Creator(msgType);
            }
            else if ("105".equals(msgType)) {
                absMTCreator = new MT105Creator(msgType);
            }
            else if ("191".equals(msgType)) {
                absMTCreator = new MT191Creator(msgType);
            }
            else if ("192".equals(msgType)) {
                absMTCreator = new MT192Creator(msgType);
            }
            else if ("198".equals(msgType)) {
                absMTCreator = new MT198Creator(msgType);
            }
            else if ("199".equals(msgType)) {
                absMTCreator = new MT199Creator(msgType);
            }
            else if ("110".equals(msgType)) {
                absMTCreator = new MT110Creator(msgType);
            }
            else if ("112".equals(msgType)) {
                absMTCreator = new MT112Creator(msgType);
            }
            else if ("103_STP".equals(msgType)) {
                absMTCreator = new MT103STPCreator(jsonData);
            }
            else if ("103".equals(msgType)) {
                absMTCreator = new MT103Creator(msgType);
            }else if ("200".equals(msgType)){
                absMTCreator = new MT200Creator(msgType);
            } else if ("201".equals(msgType)) {
                absMTCreator = new MT201Creator(msgType);
            }else if ("202".equals(msgType)){
                absMTCreator = new MT202Creator(msgType);
            }else if ("202COV".equals(msgType)){
                absMTCreator = new MT202COVCreator(msgType);
            }else if ("203".equals(msgType)){
                absMTCreator = new MT203Creator(msgType);
            }else if ("204".equals(msgType)) {
                absMTCreator = new MT204Creator(msgType);
            }else if ("205".equals(msgType)) {
                absMTCreator = new MT205Creator(msgType);
            }else if ("205COV".equals(msgType)) {
                absMTCreator = new MT205COVCreator(msgType);
            }else if ("210".equals(msgType)){
                absMTCreator = new MT210Creator(msgType);
            }
            else
                return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return absMTCreator;
    }


    public static MtResult pack(AbsMTCreator absMTCreator,MtResult result)
    {
        if(absMTCreator==null)
        {
            result.setSuccess(false);
//          result.addDescription("无法找到swift【"+new Gson().toJson(absMTCreator)+"】,请检查");
            result.addDescription(Language.getLanguage("SYS_C20")+new Gson().toJson(absMTCreator)+Language.getLanguage("SYS_C21"));
            return  result;
        }
        try {
            result.setMsgType(absMTCreator.getMsgType());
            MtHandler mtHandler=new MtHandler(absMTCreator,result);
            mtHandler.packMsg();
            result.setAbstractMT(absMTCreator.getAbstractMT());
            result.setAbsMTCreator(absMTCreator);
        } catch (Exception e) {
            result.setSuccess(false);
            result.addDescription(e.getLocalizedMessage());
            throw new RuntimeException(e);
        }
        return  result;
    }

    public  static Mt2ObjResult unPackMT(String mtStr)
    {
        Mt2ObjResult mt2ObjResult=new Mt2ObjResult();

        AbstractMT abstractMT=null;
        try {
            abstractMT=AbstractMT.parse(mtStr);
            mt2ObjResult.setAbstractMT(abstractMT);
            mt2ObjResult.setMsgType(abstractMT.getMessageType());
            SwiftBlock3 block3 = abstractMT.getSwiftMessage().getBlock3();
            if(block3!=null) {
                Map<String, String> tagMap = block3.getTagMap();
                if (tagMap.containsKey("119") && "COV".equals(tagMap.get("119"))) {
                    mt2ObjResult.setMsgType(abstractMT.getMessageType() + "COV");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            mt2ObjResult.setSuccess(false);
            mt2ObjResult.setDescription(e.getLocalizedMessage());
            throw new RuntimeException(e);
        }
        return mt2ObjResult;
    }


    public static  MtResult packMt(String json)
    {
        AbsMTCreator absMTCreator=getMTCreator(json);
        if(absMTCreator==null)
        {
            MtResult mtResult=new MtResult();
            mtResult.setSuccess(false);
//            mtResult.addDescription("通过json:【"+json+"】返回的MTCreator为null，请检查");
            mtResult.addDescription(Language.getLanguage("SYS_C22")+json+Language.getLanguage("SYS_C23"));
            return  mtResult;
        }
        AbsMTCreator implCreator=new Gson().fromJson(json,absMTCreator.getClass());
        implCreator.setAbstractMT(absMTCreator.getAbstractMT());
        return  packMT(implCreator);
    }

    public static MtResult packMT(AbsMTCreator absMTCreator)
    {
        MtResult mtResult = new MtResult();
        //组报进行校验
        mtResult= MtCreatorManager.pack(absMTCreator,mtResult);
        if(mtResult.isSuccess()){
            if(MtCfg.validatorMtTags(absMTCreator)){
                MtValidatorUtil.checkMtMessage(mtResult);
            }
            if(mtResult.isSuccess()) {
                log.debug("业务报文为 {}", mtResult.getMessage());
                //组报文成功后，进行业务要素校验
                if(mtResult.isSuccess()&&MtCfg.validatorMtBusiness(mtResult)){
                    MtValidatorUtil.checkMTBusiness(mtResult);
                }
            }
        }
        return mtResult;

    }

    public static MtResult validatorMT(String mtMessage)
    {
        MtResult mtResult = new MtResult();
        mtResult.setMessage(mtMessage);
        //组报进行校验
        MtValidatorUtil.checkMtMessage(mtResult);
        if(mtResult.isSuccess()) {
            log.debug("业务报文为 {}", mtResult.getMessage());
            //组报文成功后，进行业务要素校验
            if(mtResult.isSuccess()){
                MtValidatorUtil.checkMTBusiness(mtResult);
            }
        }
        return mtResult;

    }
}
