package com.brilliance.isc.swift.fremsg.service.impl;

import com.brilliance.isc.bo.Ecm;
import com.brilliance.isc.bo.Pts;
import com.brilliance.isc.bo.Spt;
import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.doc.TextResultVo;
import com.brilliance.isc.common.trndia.service.TrndiaService;
import com.brilliance.isc.common.trndia.vo.TrndiaVo;
import com.brilliance.isc.common.trndoc.service.TrndocService;
import com.brilliance.isc.common.trndoc.vo.DoceotVo;
import com.brilliance.isc.common.trndoc.vo.TrndocVo;
import com.brilliance.isc.common.trnmod.service.TrnmodService;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.common.vo.FremsgVo;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.PtsMapper;
import com.brilliance.isc.swift.fremsg.document.FreMessageDoc;
import com.brilliance.isc.swift.fremsg.service.FreMessageService;
import com.brilliance.isc.swift.objmtconvert.result.MtResult;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.brilliance.mda.runtime.mda.Constants.DiaToWrite;


@Service
public class FreMessageServiceImpl implements FreMessageService{
    public static final String CR = "\n";
    @Resource
    private PtsptaToolComponent ptsptaToolComponent;
    @Resource
    private TrndocService trndocService;
    @Resource
    private PtsMapper ptsMapper;
    @Resource
    private TrndiaService trndiaService;
    @Resource
    private XrtmodService xrtmodService;
    @Resource
    private FreMessageDoc freMessageDoc;
    @Resource
    private TrnmodService trnmodService;

    private static final String TEXT = "text";

    @Override
    public void regDocFRE(BizInfoVo bizInfoVo, TrndocVo trndoc) {
        try {
            Class<?> clazz = bizInfoVo.getClass();
            Field sndmtField = clazz.getDeclaredField("sndmt");
            Field fulmtField = clazz.getDeclaredField("fulmt");
            Field relmtField = clazz.getDeclaredField("relmt");
            Field msgrolField = clazz.getDeclaredField("msgrol");
            Field ccrolField = clazz.getDeclaredField("ccrol");
            sndmtField.setAccessible(true);
            fulmtField.setAccessible(true);
            relmtField.setAccessible(true);
            msgrolField.setAccessible(true);
            ccrolField.setAccessible(true);
            String mttyp1 = getOutMT((String)sndmtField.get(bizInfoVo),(String)fulmtField.get(bizInfoVo),(String)relmtField.get(bizInfoVo),bizInfoVo.getObjtyp().toUpperCase().substring(0,2));
            String mttyp2 = getOutMT((String)relmtField.get(bizInfoVo),bizInfoVo.getObjtyp().toUpperCase().substring(0,2));
            if("99|95|96|92|30|SUS|59".contains((String)sndmtField.get(bizInfoVo))){
                String bustyp = bizInfoVo.getObjtyp().substring(0,2);
                String mod = "\\" + bustyp + "DGRP" + "\\" + "rec";
                String namFre1 = "\\"  + "XXTFREL1";
                String namFre2 = "\\"  + "XXTFREL2";
                if (ptsptaToolComponent.ptsIsRolSet(trndoc.getPtsptas(), "OTHMSG") && (!MdaUtils.isEmpty(ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHMSG").getAdrblk()))) {
                    String othmsgPtaInr = ptsptaToolComponent.getPtaInrByRol(trndoc.getPtsptas(), "OTHMSG");
                    trndocService.defDocEot(trndoc, "TRN", "FRE1", namFre1, "LET001", 0, othmsgPtaInr, mod, "Original", ((String)msgrolField.get(bizInfoVo)), ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHMSG").getAdrblk());
                    trndocService.setDocSendFormat(trndoc, "TRNFRE1", "SWT", "MT"+ mttyp1);
                    if (ptsptaToolComponent.ptsIsRolSet(trndoc.getPtsptas(), "OTHCC") && !MdaUtils.isEmpty(ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHCC").getAdrblk())) {
                        String othccPtaInr = ptsptaToolComponent.getPtaInrByRol(trndoc.getPtsptas(), "OTHCC");
                        trndocService.defDocEot(trndoc, "TRN", "FRE2", namFre2, "LET001", 0, othccPtaInr, mod, "Copy", ((String)ccrolField.get(bizInfoVo)), ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHCC").getAdrblk());
                        trndocService.setDocSendFormat(trndoc, "TRNFRE2", "SWT", "MT"+ mttyp2);

                    }
                }
            }
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public void regDocFRE1(BizInfoVo bizInfoVo, TrndocVo trndoc,String fre1,String fre2) {
        try {
            Class<?> clazz = bizInfoVo.getClass();
            Field sndmtField = clazz.getDeclaredField("sndmt");
            Field fulmtField = clazz.getDeclaredField("fulmt");
            Field relmtField = clazz.getDeclaredField("relmt");
            Field msgrolField = clazz.getDeclaredField("msgrol");
            Field ccrolField = clazz.getDeclaredField("ccrol");
            sndmtField.setAccessible(true);
            fulmtField.setAccessible(true);
            relmtField.setAccessible(true);
            msgrolField.setAccessible(true);
            ccrolField.setAccessible(true);
            String mttyp1 = getOutMT((String)sndmtField.get(bizInfoVo),(String)fulmtField.get(bizInfoVo),(String)relmtField.get(bizInfoVo),bizInfoVo.getObjtyp().toUpperCase().substring(0,2));
            String mttyp2 = getOutMT((String)relmtField.get(bizInfoVo),bizInfoVo.getObjtyp().toUpperCase().substring(0,2));
            if("99|95|96|92|30|SUS|59".contains((String)sndmtField.get(bizInfoVo))){
                String bustyp = bizInfoVo.getObjtyp().substring(0,2);
                String mod = "\\" + bustyp + "DGRP" + "\\" + "rec";
                String namFre1 = "\\"  + fre1;
                String namFre2 = "\\"  + fre2;
                if (ptsptaToolComponent.ptsIsRolSet(trndoc.getPtsptas(), "OTHMSG") && (!MdaUtils.isEmpty(ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHMSG").getAdrblk()))) {
                    String othmsgPtaInr = ptsptaToolComponent.getPtaInrByRol(trndoc.getPtsptas(), "OTHMSG");
                    trndocService.defDocEot(trndoc, "TRN", "FRE1", namFre1, "LET001", 0, othmsgPtaInr, mod, "Original", ((String)msgrolField.get(bizInfoVo)), ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHMSG").getAdrblk());
                    trndocService.setDocSendFormat(trndoc, "TRNFRE1", "SWT", "MT"+ mttyp1);
                    if (ptsptaToolComponent.ptsIsRolSet(trndoc.getPtsptas(), "OTHCC") && !MdaUtils.isEmpty(ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHCC").getAdrblk())) {
                        String othccPtaInr = ptsptaToolComponent.getPtaInrByRol(trndoc.getPtsptas(), "OTHCC");
                        trndocService.defDocEot(trndoc, "TRN", "FRE2", namFre2, "LET001", 0, othccPtaInr, mod, "Copy", ((String)ccrolField.get(bizInfoVo)), ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHCC").getAdrblk());
                        trndocService.setDocSendFormat(trndoc, "TRNFRE2", "SWT", "MT"+ mttyp2);
                    }

                }
            }
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public Set<String> buildMsgrolList(String objinr, String objtyp) {
        Set<String> msgrolSet = new HashSet<>();
        List<Pts> ptsList = ptsMapper.getPtsByObjInrTyp(objinr, objtyp);
        if (ptsList != null) {
            ptsList.forEach(pts -> msgrolSet.add(pts.getRol()));
        }
        msgrolSet.add("OTH");
        return msgrolSet;
    }

    @Override
    public void freDiaSet(BizInfoVo bizInfoVo,TrndiaVo trndiaVo,String inifrm) {
        Class clazz = bizInfoVo.getClass();
        String ownref = bizInfoVo.getOwnref();
        try {
            Field reldirField = clazz.getDeclaredField("reldir");
            Field sndmtField = clazz.getDeclaredField("sndmt");
            reldirField.setAccessible(true);
            sndmtField.setAccessible(true);
            String reldir = (String)reldirField.get(bizInfoVo);
            String sndmt = (String)sndmtField.get(bizInfoVo);

            if (MdaUtils.compareTo(reldir, "S") == 0) {
                if (MdaUtils.compareTo(sndmt, "92") == 0 || MdaUtils.compareTo(sndmt, "95") == 0) {
                    trndiaService.trnDiaSet(trndiaVo, "FOLQUE", Dates.dateAddW(Dates.today(), 5, xrtmodService.calendar()), "FOL", Strings.getText("Reply to Query pending", ownref), inifrm, DiaToWrite);
                }
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public TextResultVo xxxfres1(FremsgVo fremsg,PtsptaVo ptsptaVo,String ownRef, DoceotVo doceotVo) {
        MtResult mtResult = null;
        fremsg.setTag79txt999(fremsg.getPrttitle() + CR + ("30".equals(fremsg.getSndmt()) ? doceotVo.getAddstr() : fremsg.getStrinf() + CR + doceotVo.getAddstr()));
        doceotVo.setRole("OTHMSG");
        String relmt = fremsg.getRelmt();
        ptsptaVo = fremsg.getOthmsg();
        if (fremsg.getSndmt().equals("95")) {
            String firstCharacter = relmt.substring(0, 1);
            switch (firstCharacter) {
                case "1":
                    mtResult = freMessageDoc.buildMt195(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "2":
                    mtResult = freMessageDoc.buildMt295(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "4":
                    mtResult = freMessageDoc.buildMt495(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "7":
                    mtResult = freMessageDoc.buildMt795(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "9":
                    mtResult = freMessageDoc.buildMt995(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
            }
        }
        if (fremsg.getSndmt().equals("96")) {
            String firstCharacter = relmt.substring(0, 1);
            switch (firstCharacter) {
                case "1":
                    mtResult = freMessageDoc.buildMt196(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "2":
                    mtResult = freMessageDoc.buildMt296(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "4":
                    mtResult = freMessageDoc.buildMt496(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "7":
                    mtResult = freMessageDoc.buildMt796(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "9":
                    mtResult = freMessageDoc.buildMt996(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
            }
        }
        if (fremsg.getSndmt().equals("30")) {
                mtResult = freMessageDoc.buildMt730(ownRef, fremsg, doceotVo);
        }
        if (fremsg.getSndmt().equals("99") || fremsg.getSndmt().equals("59")) {
            switch (fremsg.getFulmt()) {
                case "199":
                    mtResult = freMessageDoc.buildMt199(ownRef, fremsg, doceotVo);
                    break;
                case "299":
                    mtResult = freMessageDoc.buildMt299(ownRef, fremsg, doceotVo);
                    break;
                case "499":
                    mtResult = freMessageDoc.buildMt499(ownRef, fremsg, doceotVo);
                    break;
                case "759":
                    mtResult = freMessageDoc.swtPrtMT759(doceotVo, ownRef, fremsg);
                    break;
                case "999":
                    mtResult = freMessageDoc.buildMt999(ownRef,fremsg,doceotVo);
                    break;
                default:
                    mtResult = freMessageDoc.buildMt799(ownRef, fremsg, doceotVo);
                    break;
            }
        }
        if (fremsg.getSndmt().equals("92")) {
            String firstCharacter = relmt.substring(0, 1);
            switch (firstCharacter) {
                case "1":
                    mtResult = freMessageDoc.buildMt192(ownRef, fremsg, doceotVo);
                    break;
                case "2":
                    mtResult = freMessageDoc.buildMt292(ownRef, fremsg, doceotVo);
                    break;
                case "7":
                    mtResult = freMessageDoc.buildMt792(ownRef, fremsg, doceotVo);
                    break;
                case "9":
                    mtResult = freMessageDoc.buildMt992(ownRef, fremsg, doceotVo);
                    break;
            }
        }
        if (mtResult != null) {
            TextResultVo textResultVo = new TextResultVo();
            textResultVo.setData(mtResult.getMessage());
            textResultVo.setFormat(TEXT);
            textResultVo.setExtension("sf2");
            textResultVo.setBinary(mtResult.getMessage().getBytes());
            textResultVo.setAttach(mtResult.getAbsMTCreator());
            return textResultVo;
        }
        return null;
    }

    @Override
    public  TextResultVo xxxfres1(FremsgVo fremsg,PtsptaVo ptsptaVo,String ownRef, DoceotVo doceotVo,String objTyp) {
        MtResult mtResult = null;
        String mt = this.getOutMT(fremsg,objTyp);
        if(MdaUtils.equals("430",mt)){
            mt = "410";
        }
        fremsg.setTag79txt999(fremsg.getPrttitle() + CR +  ("30".equals(fremsg.getSndmt()) ? doceotVo.getAddstr() : fremsg.getStrinf() + CR + doceotVo.getAddstr()));
        doceotVo.setRole("OTHMSG");
        ptsptaVo = fremsg.getOthmsg();
        switch (mt){
            case "195":
                mtResult = freMessageDoc.buildMt195(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "295":
                mtResult = freMessageDoc.buildMt295(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "495":
                mtResult = freMessageDoc.buildMt495(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "795":
                mtResult = freMessageDoc.buildMt795(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "995":
                mtResult = freMessageDoc.buildMt995(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "196":
                mtResult = freMessageDoc.buildMt196(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "496":
                mtResult = freMessageDoc.buildMt496(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "296":
                mtResult = freMessageDoc.buildMt296(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "796":
                mtResult = freMessageDoc.buildMt796(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "996":
                mtResult = freMessageDoc.buildMt996(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
//            case "410":
//                mtResult = freMessageDoc.buildMt410(ownRef, fremsg, doceotVo);
//                break;
            case "730":
                mtResult = freMessageDoc.buildMt730(ownRef, fremsg, doceotVo);
                break;
            case "799":
                mtResult = freMessageDoc.buildMt799(ownRef, fremsg, doceotVo);
                break;
            case "199":
                mtResult = freMessageDoc.buildMt199(ownRef, fremsg, doceotVo);
                break;
            case "299":
                mtResult = freMessageDoc.buildMt299(ownRef, fremsg, doceotVo);
                break;
            case "499":
                mtResult = freMessageDoc.buildMt499(ownRef, fremsg, doceotVo);
                break;
            case "759":
                mtResult = freMessageDoc.swtPrtMT759(doceotVo, ownRef, fremsg);
                break;
            case "999":
                mtResult = freMessageDoc.buildMt999(ownRef,fremsg,doceotVo);
                break;
            case "192":
                mtResult = freMessageDoc.buildMt192(ownRef, fremsg, doceotVo);
                break;
            case "292":
                mtResult = freMessageDoc.buildMt292(ownRef, fremsg, doceotVo);
                break;
            case "792":
                mtResult = freMessageDoc.buildMt792(ownRef, fremsg, doceotVo);
                break;
            case "992":
                mtResult = freMessageDoc.buildMt992(ownRef, fremsg, doceotVo);
                break;
        }
        if (mtResult != null) {
            TextResultVo textResultVo = new TextResultVo();
            textResultVo.setData(mtResult.getMessage());
            textResultVo.setFormat(TEXT);
            textResultVo.setExtension("sf2");
            textResultVo.setBinary(mtResult.getMessage().getBytes());
            textResultVo.setAttach(mtResult.getAbsMTCreator());
            return textResultVo;
        }
        return null;
    }

    @Override
    public <T> TextResultVo xxxfres1(FremsgVo fremsg,PtsptaVo ptsptaVo,T transactionVo,String ownRef, DoceotVo doceotVo,String objTyp) {
        MtResult mtResult = null;
        String mt = this.getOutMT(fremsg,objTyp);
        if(MdaUtils.equals("430",mt)){
            mt = "410";
        }
        fremsg.setTag79txt999(fremsg.getPrttitle() + CR +  ("30".equals(fremsg.getSndmt()) ? doceotVo.getAddstr() : fremsg.getStrinf() + CR + doceotVo.getAddstr()));
        doceotVo.setRole("OTHMSG");
        ptsptaVo = fremsg.getOthmsg();
        switch (mt){
            case "195":
                mtResult = freMessageDoc.buildMt195(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "295":
                mtResult = freMessageDoc.buildMt295(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "495":
                mtResult = freMessageDoc.buildMt495(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "795":
                mtResult = freMessageDoc.buildMt795(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "995":
                mtResult = freMessageDoc.buildMt995(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "196":
                mtResult = freMessageDoc.buildMt196(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "296":
                mtResult = freMessageDoc.buildMt296(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "496":
                mtResult = freMessageDoc.buildMt496(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "796":
                mtResult = freMessageDoc.buildMt796(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "996":
                mtResult = freMessageDoc.buildMt996(ptsptaVo, ownRef, fremsg, doceotVo);
                break;
            case "410":
                mtResult = freMessageDoc.buildMt410(ownRef, fremsg, doceotVo,transactionVo,objTyp);
                break;
            case "730":
                mtResult = freMessageDoc.buildMt730(ownRef, fremsg, doceotVo);
                break;
            case "799":
                mtResult = freMessageDoc.buildMt799(ownRef, fremsg, doceotVo);
                break;
            case "199":
                mtResult = freMessageDoc.buildMt199(ownRef, fremsg, doceotVo);
                break;
            case "299":
                mtResult = freMessageDoc.buildMt299(ownRef, fremsg, doceotVo);
                break;
            case "499":
                mtResult = freMessageDoc.buildMt499(ownRef, fremsg, doceotVo);
                break;
            case "759":
                mtResult = freMessageDoc.swtPrtMT759(doceotVo, ownRef, fremsg);
                break;
            case "999":
                mtResult = freMessageDoc.buildMt999(ownRef,fremsg,doceotVo);
                break;
            case "192":
                mtResult = freMessageDoc.buildMt192(ownRef, fremsg, doceotVo);
                break;
            case "292":
                mtResult = freMessageDoc.buildMt292(ownRef, fremsg, doceotVo);
                break;
            case "792":
                mtResult = freMessageDoc.buildMt792(ownRef, fremsg, doceotVo);
                break;
            case "992":
                mtResult = freMessageDoc.buildMt992(ownRef, fremsg, doceotVo);
                break;
        }
        if (mtResult != null) {
            TextResultVo textResultVo = new TextResultVo();
            textResultVo.setData(mtResult.getMessage());
            textResultVo.setFormat(TEXT);
            textResultVo.setExtension("sf2");
            textResultVo.setBinary(mtResult.getMessage().getBytes());
            textResultVo.setAttach(mtResult.getAbsMTCreator());
            return textResultVo;
        }
        return null;
    }

    @Override
    public TextResultVo xxxfres2(FremsgVo fremsg, PtsptaVo ptsptaVo, String ownRef, DoceotVo doceotVo) {
        MtResult mtResult = null;
        fremsg.setTag79txt999(fremsg.getPrttitle() + CR + fremsg.getStrinf());
        doceotVo.setRole("OTHCC");
        String relmt = fremsg.getRelmt();
        ptsptaVo = fremsg.getOthmsg();
        if (fremsg.getSndmt().equals("95")) {
            String firstCharacter = relmt.substring(0, 1);
            switch (firstCharacter) {
                case "1":
                    mtResult = freMessageDoc.buildMt195(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "2":
                    mtResult = freMessageDoc.buildMt295(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "4":
                    mtResult = freMessageDoc.buildMt495(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "7":
                    mtResult = freMessageDoc.buildMt795(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "9":
                    mtResult = freMessageDoc.buildMt995(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
            }
        }
        if (fremsg.getSndmt().equals("96")) {
            String firstCharacter = relmt.substring(0, 1);
            switch (firstCharacter) {
                case "1":
                    mtResult = freMessageDoc.buildMt196(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "2":
                    mtResult = freMessageDoc.buildMt296(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "7":
                    mtResult = freMessageDoc.buildMt796(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
                case "9":
                    mtResult = freMessageDoc.buildMt996(ptsptaVo, ownRef, fremsg, doceotVo);
                    break;
            }
        }
        if (fremsg.getSndmt().equals("30")) {
            mtResult = freMessageDoc.buildMt730(ownRef, fremsg, doceotVo);
        }
        if (fremsg.getSndmt().equals("99") || fremsg.getSndmt().equals("59")) {
            switch (fremsg.getFulmt()) {
                case "199":
                    mtResult = freMessageDoc.buildMt199(ownRef, fremsg, doceotVo);
                    break;
                case "299":
                    mtResult = freMessageDoc.buildMt299(ownRef, fremsg, doceotVo);
                    break;
                case "499":
                    mtResult = freMessageDoc.buildMt499(ownRef, fremsg, doceotVo);
                    break;
                case "759":
                    mtResult = freMessageDoc.swtPrtMT759(doceotVo, ownRef, fremsg);
                    break;
                case "999":
                    mtResult = freMessageDoc.buildMt999(ownRef,fremsg,doceotVo);
                    break;
                default:
                    mtResult = freMessageDoc.buildMt799(ownRef, fremsg, doceotVo);
                    break;
            }
        }
        if (fremsg.getSndmt().equals("92")) {
            String firstCharacter = relmt.substring(0, 1);
            switch (firstCharacter) {
                case "1":
                    mtResult = freMessageDoc.buildMt192(ownRef, fremsg, doceotVo);
                    break;
                case "2":
                    mtResult = freMessageDoc.buildMt292(ownRef, fremsg, doceotVo);
                    break;
                case "7":
                    mtResult = freMessageDoc.buildMt792(ownRef, fremsg, doceotVo);
                    break;
                case "9":
                    mtResult = freMessageDoc.buildMt992(ownRef, fremsg, doceotVo);
                    break;
            }
        }
        if (mtResult != null) {
            TextResultVo textResultVo = new TextResultVo();
            textResultVo.setData(mtResult.getMessage());
            textResultVo.setFormat(TEXT);
            textResultVo.setExtension("sf2");
            textResultVo.setBinary(mtResult.getMessage().getBytes());
            textResultVo.setAttach(mtResult.getAbsMTCreator());
            return textResultVo;
        }
        return null;
    }

    @Override
    public TextResultVo xxxfres2(FremsgVo fremsg, PtsptaVo ptsptaVo, String ownRef, DoceotVo doceotVo, String objTyp, Spt spt) {
        MtResult mtResult = null;
        doceotVo.setRole("OTHCC");
        String relmt = fremsg.getRelmt();
        String end = "99";
        String fst = "";
        switch (objTyp){
            case "CP":
                fst = "9";
                break;
            case "BO":
            case "BC":
            case "CC":
                fst = "4";
                break;
            case "LI":
            case "BR":
            case "LE":
            case "BE":
            case "LT":
            case "BT":
            case "GI":
            case "GC":
            case "RM":
            case "RC":
            case "BP":
            case "TR":
            case "FS":
            case "FP":
            case "PA":
            case "LD":
            case "MC":
            case "SD":
            case "GE":
                fst = "7";
                break;
            default:
                fst = "9";
                break;
        }
        String Mt = fst + end;
        if(!MdaUtils.isEmpty(relmt)){
            Mt = relmt;
        }
        if(MdaUtils.equals("759",Mt)){
            Mt = "799";
        }
        String copyCc = "This message is a copy of original message sent to " + fremsg.getOthmsg().getPts().getExtkey().split("-")[0];
        String fretxt = trndocService.getFretxt(doceotVo,fremsg,spt);
        if(!MdaUtils.isEmpty(doceotVo.getAddstr())){
            fretxt += doceotVo.getAddstr();
        }
        fremsg.setTag79txt999(copyCc + CR + fretxt + CR + doceotVo.getAddstr());
        switch (Mt){
            case "199":
                mtResult = freMessageDoc.buildMt199(ownRef, fremsg, doceotVo);
                break;
            case "299":
                mtResult = freMessageDoc.buildMt299(ownRef, fremsg, doceotVo);
                break;
            case "499":
                mtResult = freMessageDoc.buildMt499(ownRef, fremsg, doceotVo);
                break;
            case "799":
                mtResult = freMessageDoc.buildMt799(ownRef, fremsg, doceotVo);
                break;
            case "999":
                mtResult = freMessageDoc.buildMt999(ownRef,fremsg,doceotVo);
                break;
            default:
                mtResult = freMessageDoc.buildMt799(ownRef, fremsg, doceotVo);
                break;

        }
        if (mtResult != null) {
            TextResultVo textResultVo = new TextResultVo();
            textResultVo.setData(mtResult.getMessage());
            textResultVo.setFormat(TEXT);
            textResultVo.setExtension("sf2");
            textResultVo.setBinary(mtResult.getMessage().getBytes());
            textResultVo.setAttach(mtResult.getAbsMTCreator());
            return textResultVo;
        }
        return null;
    }


    @Override
    public <T> TextResultVo xxxfres2(FremsgVo fremsg, PtsptaVo ptsptaVo, T transactionVo,String ownRef, DoceotVo doceotVo, String objTyp, Spt spt) {
        MtResult mtResult = null;
        doceotVo.setRole("OTHCC");
        String relmt = fremsg.getRelmt();
        String end = "99";
        String fst = "";
        switch (objTyp){
            case "CP":
                fst = "9";
                break;
            case "BO":
            case "BC":
            case "CC":
                fst = "4";
                break;
            case "LI":
            case "BR":
            case "LE":
            case "BE":
            case "LT":
            case "BT":
            case "GI":
            case "GC":
            case "RM":
            case "RC":
            case "BP":
            case "TR":
            case "FS":
            case "FP":
            case "PA":
            case "LD":
            case "MC":
            case "SD":
            case "GE":
                fst = "7";
                break;
            default:
                fst = "9";
                break;
        }
        String Mt = fst + end;
        if(!MdaUtils.isEmpty(relmt)){
            Mt = relmt;
        }
        if(MdaUtils.equals("759",Mt)){
            Mt = "799";
        }
        String copyCc = "This message is a copy of original message sent to " + fremsg.getOthmsg().getPts().getExtkey().split("-")[0];
        String fretxt = trndocService.getFretxt(doceotVo,fremsg,spt);
        if(!MdaUtils.isEmpty(doceotVo.getAddstr())){
            fretxt += doceotVo.getAddstr();
        }
        fremsg.setTag79txt999(copyCc + CR + fretxt + CR + doceotVo.getAddstr());
        switch (Mt){
            case "199":
                mtResult = freMessageDoc.buildMt199(ownRef, fremsg, doceotVo);
                break;
            case "299":
                mtResult = freMessageDoc.buildMt299(ownRef, fremsg, doceotVo);
                break;
            case "499":
                mtResult = freMessageDoc.buildMt499(ownRef, fremsg, doceotVo);
                break;
            case "799":
                mtResult = freMessageDoc.buildMt799(ownRef, fremsg, doceotVo);
                break;
            case "999":
                mtResult = freMessageDoc.buildMt999(ownRef,fremsg,doceotVo);
                break;
            default:
                mtResult = freMessageDoc.buildMt799(ownRef, fremsg, doceotVo);
                break;

        }
        if (mtResult != null) {
            TextResultVo textResultVo = new TextResultVo();
            textResultVo.setData(mtResult.getMessage());
            textResultVo.setFormat(TEXT);
            textResultVo.setExtension("sf2");
            textResultVo.setBinary(mtResult.getMessage().getBytes());
            textResultVo.setAttach(mtResult.getAbsMTCreator());
            return textResultVo;
        }
        return null;
    }

    @Override
    public <T> void xmlPrtBodyOriginal(Map<String, List<String[]>> dataMap, T transactionVo, DoceotVo doceotVo) {
        FremsgVo fremsg = (FremsgVo) Utils.getFieldValue(transactionVo,"fremsg");
        if(!MdaUtils.equals(fremsg.getSndmt(),"30")){
            String freTxt = trndocService.getFretxt(doceotVo,transactionVo); //GetFRETXT
            trndocService.xmlPrtTxtBlk(dataMap,"fretxt",freTxt);
        }

    }

    @Override
    public <T> void xmlPrtBodyCopy(Map<String, List<String[]>> dataMap, T transactionVo, DoceotVo doceotVo) {
        FremsgVo fremsg = Utils.getFieldValue(transactionVo,"fremsg");
//                print using introCopy, MSGNAM2
        trndocService.xmlPrtTxtBlk(dataMap,"introCopy",Strings.getText("$1 $2","We herewith send you for your information a copy of our message to the a.m. transaction, originally sent to ",fremsg.getOthmsg().getPts().getExtkey().split("-")[0]));
//        print using \trnmod\trndoc\quoteline
        //GetFRETXT
        String freTxt = trndocService.getFretxt(doceotVo,transactionVo);
        trndocService.xmlPrtTxtBlk(dataMap,"fretxt",freTxt);
//        print using \trnmod\trndoc\\unquoteline
        trndocService.xmlPrtTxtBlk(dataMap,"addtxt",doceotVo.getAddtxt());
    }

    @Override
    public TextResultVo Ecmswt103(Ecm ecm) {
        return freMessageDoc.buildMt103(ecm);
    }

    public String getOutMT(FremsgVo fremsgVo,String objTyp) {
        //! Get MT that is really used within the outgoing Swift message (original).
        // check if SNDMT is consists of letters only. In this case use 99
        int len = Strings.len(fremsgVo.getSndmt());
        String sndmt = fremsgVo.getSndmt();
        for (int i = 1; i <= len; i++) {
            if (Strings.pos("0123456789", Strings.mid(sndmt, i, 1)) == 0) {
                // No numeric character found, so use "99"
                // only letters found so use "99"
                sndmt = "99";
                break;
            }
        }
        // dngdev.002002 Add SD
        String mt = "";
        switch (objTyp) {
            case "CP":
                mt = "999";
                break;
            case "BO":
            case "BC":
            case "CH":
                mt = "4" + sndmt;
                break;
            case "CC":
                mt = "499";
                break;
            case "LE":
            case "LI":
            case "BR":
            case "BE":
            case "GI":
            case "GC":
            case "LT":
            case "BT":
            case "RM":
            case "RC":
                mt = "7" + sndmt;
                break;
            default:
                mt = "999";
        }
        // check 1st digit
        Argument<String> mtBox = new Argument<>("", mt);
        this.getCommonGroupMt(mtBox,objTyp);

        mt = mtBox.value;

        // set MT for Free Correspondence if "MT to Send" is changed by user manually
        if (!MdaUtils.isEmpty(fremsgVo.getFulmt())) {
            // set MT ($MT) if SNDMT=99 to value of FULMT
            // set to MT 199/ 299/ 499 or 799
            mt = fremsgVo.getFulmt();
        }
        // set 1st digit of "Send MT" ($conmt) depending on "Related MT" if available
        String v1stOfRelMT = "";
        String endOfSndMT = "";
        if (!MdaUtils.isEmpty(fremsgVo.getRelmt())) {
            v1stOfRelMT = Strings.mid(fremsgVo.getRelmt(), 1, 1);
            endOfSndMT = Strings.mid(mt, 2, 2);
            mt = v1stOfRelMT + endOfSndMT;
        }
        return mt;

    }
    public String getOutMT(String relmt,String objTyp){
        String end = "99";
        String fst = "";
        switch (objTyp){
            case "CP":
                fst = "9";
                break;
            case "BO":
            case "BC":
            case "CC":
                fst = "4";
                break;
            case "LI":
            case "BR":
            case "LE":
            case "BE":
            case "LT":
            case "BT":
            case "GI":
            case "GC":
            case "RM":
            case "RC":
            case "BP":
            case "TR":
            case "FS":
            case "FP":
            case "PA":
            case "LD":
            case "MC":
            case "SD":
            case "GE":
                fst = "7";
                break;
            default:
                fst = "9";
                break;
        }
        String Mt = fst + end;
        if(!MdaUtils.isEmpty(relmt)){
            Mt = relmt;
        }
        if(MdaUtils.equals("759",Mt)){
            Mt = "799";
        }
        return Mt;
    }
    public String getOutMT(String sndmt,String fulmt,String relmt,String objTyp) {
        //! Get MT that is really used within the outgoing Swift message (original).
        // check if SNDMT is consists of letters only. In this case use 99
        int len = Strings.len(sndmt);
        for (int i = 1; i <= len; i++) {
            if (Strings.pos("0123456789", Strings.mid(sndmt, i, 1)) == 0) {
                // No numeric character found, so use "99"
                // only letters found so use "99"
                sndmt = "99";
                break;
            }
        }
        // dngdev.002002 Add SD
        String mt = "";
        switch (objTyp) {
            case "CP":
                mt = "999";
                break;
            case "BO":
            case "BC":
            case "CH":
                mt = "4" + sndmt;
                break;
            case "CC":
                mt = "499";
                break;
            case "LE":
            case "LI":
            case "BR":
            case "BE":
            case "GI":
            case "GC":
            case "LT":
            case "BT":
            case "RM":
            case "RC":
                mt = "7" + sndmt;
                break;
            default:
                mt = "999";
        }
        // check 1st digit
        Argument<String> mtBox = new Argument<>("", mt);
        this.getCommonGroupMt(mtBox,objTyp);

        mt = mtBox.value;

        // set MT for Free Correspondence if "MT to Send" is changed by user manually
        if (!MdaUtils.isEmpty(fulmt)) {
            // set MT ($MT) if SNDMT=99 to value of FULMT
            // set to MT 199/ 299/ 499 or 799
            mt = fulmt;
        }
        // set 1st digit of "Send MT" ($conmt) depending on "Related MT" if available
        String v1stOfRelMT = "";
        String endOfSndMT = "";
        if (!MdaUtils.isEmpty(relmt)) {
            v1stOfRelMT = Strings.mid(relmt, 1, 1);
            endOfSndMT = Strings.mid(mt, 2, 2);
            mt = v1stOfRelMT + endOfSndMT;
        }
        if(MdaUtils.equals("430",mt)){
            mt = "410";
        }
        return mt;

    }

    public void getCommonGroupMt(Argument<String> argMT,String objTyp) {
        //! Check passed message type.
        //! If passed message type is empty, 1st digit will be returned only.
        // ArgMT     = message type
        // ArgCORTYP = medium, because TCO can get other MTs
        // get 1st digit of message type
        String v1st = Strings.mid(argMT.value, 1, 1);
        // get remaining digits
        String end = Strings.mid(argMT.value, 2);
        switch (objTyp) {
            // Clean Payment
            case "CP":
                /**
                 $1st = "1"
                 **/
                v1st = "9";
                // Import/Export Collection
                break;
            case "BC":
            case "BO":
            case "CC":
                v1st = "4";
                // Import: L/C, L/C Documents
                // Export: L/C, L/C Documents, L/C Transfer, L/C Transfer Document
                // Guarantee, Guarantee Claim
                // Reimbursement, Reimbursement Claim
                // DNGDEV.002002 Syndication Deal
                break;
            case "LI":
            case "BR":
            case "LE":
            case "BE":
            case "LT":
            case "BT":
            case "GI":
            case "GC":
            case "RM":
            case "RC":
            case "BP":
            case "TR":
            case "FS":
            case "FP":
            case "PA":
            case "LD":
            case "MC":
            case "SD":
            case "GE":
                v1st = "7";
                // all other messages are using "9"
                break;
            default:
                v1st = "9";
        }
        argMT.value = v1st + end;
    }
}
