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

import com.brilliance.isc.swift.objmtconvert.model.AbsMXCreator;
import com.brilliance.isc.swift.objmtconvert.model.mx.camt05200108.MXCamt05200108Creator;
import com.brilliance.isc.swift.objmtconvert.model.mx.camt05300108.MXCamt05300108Creator;
import com.brilliance.isc.swift.objmtconvert.model.mx.camt05400108.MXCamt05400108Creator;
import com.brilliance.isc.swift.objmtconvert.model.mx.camt05600108.MXCamt05600108Creator;
import com.brilliance.isc.swift.objmtconvert.model.mx.camt05700106.MXCamt05700106Creator;
import com.brilliance.isc.swift.objmtconvert.model.mx.pacs00200110.MXPacs00200110Creator;
import com.brilliance.isc.swift.objmtconvert.model.mx.pacs00400109.MXPacs00400109Creator;
import com.brilliance.isc.swift.objmtconvert.model.mx.pacs00800108.MXPacs00800108Creator;
import com.brilliance.isc.swift.objmtconvert.model.mx.pacs01000103.MXPacs01000103Creator;
import com.brilliance.isc.swift.objmtconvert.model.mx.pain00100109.MXPain00100109Creator;
import com.brilliance.isc.swift.objmtconvert.model.mx.pain00200110.MXPain00200110Creator;
import com.brilliance.isc.swift.objmtconvert.result.MxResult;
import com.brilliance.isc.swift.objmtconvert.util.MXUtil;
import com.google.gson.Gson;
import com.prowidesoftware.swift.model.mx.AbstractMX;

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

    private static AbsMXCreator getMXCreator(String jsonData) {
        String identifier = MXUtil.getSwiftType(jsonData);
        AbsMXCreator absMXCreator = null;
        try {
            if ("pacs.008.001.08".equals(identifier)) {
                absMXCreator = new MXPacs00800108Creator(identifier);
            } else if ("camt.053.001.08".equals(identifier)) {
                absMXCreator = new MXCamt05300108Creator(identifier);
            } else if ("camt.052.001.08".equals(identifier)) {
                absMXCreator = new MXCamt05200108Creator(identifier);
            } else if ("camt.054.001.08".equals(identifier)) {
                absMXCreator = new MXCamt05400108Creator(identifier);
            } else if ("camt.056.001.08".equals(identifier)) {
                absMXCreator = new MXCamt05600108Creator(identifier);
            } else if ("camt.057.001.06".equals(identifier)) {
                absMXCreator = new MXCamt05700106Creator(identifier);
            } else if ("pacs.009.001.08".equals(identifier)) {
                absMXCreator = new MXPacs00800108Creator(identifier);
            } else if ("pacs.004.001.09".equals(identifier)) {
                absMXCreator = new MXPacs00400109Creator(identifier);
            } else if ("pacs.002.001.10".equals(identifier)) {
                absMXCreator = new MXPacs00200110Creator(identifier);
            } else if ("camt.029.001.09".equals(identifier)) {
                absMXCreator = new MXPacs00200110Creator(identifier);
            } else if("pacs.010.001.03".equals(identifier)) {
                absMXCreator = new MXPacs01000103Creator(identifier);
            } else if ("pain.001.001.09".equals(identifier)){
                absMXCreator = new MXPain00100109Creator(identifier);
            } else if("pain.002.001.10".equals(identifier)) {
                absMXCreator = new MXPain00200110Creator(identifier);
            } else {
                return null;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return absMXCreator;
    }

    public static MxResult pack(AbsMXCreator absMXCreator) {
        String json = new Gson().toJson(absMXCreator);
        return pack(json);
    }


    public static MxResult pack(String json) {
        MxResult result = new MxResult();
        AbsMXCreator absMXCreator = getMXCreator(json);
        try {
            result.setIdentifier(absMXCreator.getIdentifier());
            String swiftGsonStr = MxTransfer.transfer(json);
            AbstractMX abstractMX = AbstractMX.fromJson(swiftGsonStr);
            String message = abstractMX.message();
            result.setMessage(message);
            result.setAbstractMX(abstractMX);
            MXUtil.validateMx(message);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setErrorInf(e.getMessage());
        }
        return result;
    }

}
