package com.brilliance.isc.common.register.document.common;

import cn.hutool.core.lang.Assert;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.register.document.IDocumentRegister;
import com.brilliance.isc.common.setgll.service.SetgllService;
import com.brilliance.isc.common.setgll.vo.SetgllVo;
import com.brilliance.isc.common.trndoc.service.DoceotService;
import com.brilliance.isc.common.trndoc.service.TrndocService;
import com.brilliance.isc.common.trndoc.vo.TrndocVo;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.brilliance.mda.runtime.mda.Constants.CR;
import static com.brilliance.mda.runtime.mda.Constants.NULLSTR;

/**
 * 支持根据结算信息，出对应支付凭证、头寸电、756、400等报文
 */
@Component
public class SetglgDocumentRegister implements IDocumentRegister {

    @Autowired
    private TrndocService trndocService;

    @Autowired
    private DoceotService doceotService;

    @Autowired
    private SetgllService setgllService;

    @Override
    public void registerDocument(TrndocVo trndoc) {
        if(MdaUtils.isEmpty(trndoc.getSetglg().getSetgll())){
           return;
        }
        for (int i = 0; i < trndoc.getSetglg().getSetgll().size(); i++) {
            registerDocumentOfLine(trndoc,trndoc.getSetglg().getSetgll().get(i),i);
        }
    }

    private void registerDocumentOfLine(TrndocVo trndoc, SetgllVo setgllVo, int idx){
        // create payment messages per setgll line
        final String REC_PATH = "\\SETGLL\\REC";
        //汇款交易的oth不出报文
        String ownpath = NULLSTR;
        String id = NULLSTR;
        String rtgrol = NULLSTR;
        String title = NULLSTR;
        String single202 = NULLSTR;
        String descr = NULLSTR;
        String prf = NULLSTR;
        String flg = NULLSTR;
        String dfdflg = NULLSTR;
        String framename = NULLSTR;
        String cmtflg = Utils.getFieldValue(trndoc.getRec(),"cmtflg","");
        String swftyp = NULLSTR;
//        String prfd = NULLSTR;
        String des = NULLSTR;
        String des2 = NULLSTR;
        String dfuflg = NULLSTR;
        String ptainr = NULLSTR;
        PtsptaVo wrkPtspta = null;
        if( ! MdaUtils.isEmpty(setgllVo.getSftmt()) && MdaUtils.compareTo(setgllVo.getSftmt(),"OTH") != 0 )
        {
            id = (idx+1)+"";
            rtgrol = "";
            if( MdaUtils.isEmpty(setgllVo.getDirindflg()) )
            {
                if( MdaUtils.compareTo(setgllVo.getDsp(),"RTC") == 0 || MdaUtils.compareTo(setgllVo.getDsp(),"TGC") == 0 )
                {
                    Argument<String> rtgrolBox = new Argument<String>("",rtgrol);
                    setgllService.getRelevantRtgRol(setgllVo, setgllVo.getDsp(), rtgrolBox );

                    rtgrol = rtgrolBox.value;
                    wrkPtspta = Utils.getFieldValue(setgllVo.getSetgrp(), rtgrol.toLowerCase());
                }
                else
                {
                    if( MdaUtils.compareTo(setgllVo.getSftmt(),"400") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"756") == 0 )
                    {
                        wrkPtspta = setgllVo.getSetgrp().getSnd756();
                    }
                    else
                    {
                        wrkPtspta = setgllVo.getSetgrp().getBei();
                    }
                }
            }
            else
            {
                wrkPtspta = setgllVo.getSetgrp().getApy();
            }
            title = "";
            switch( setgllVo.getDsp() )
            {
                case "RTC":
                    // RTGS Plus message
                    title = Strings.getText("RTGSplus Payment to $2",setgllVo.getSftmt(),rtgrol);
                    break;
                case "TGC":
                    title = Strings.getText("TARGET Payment to $2",setgllVo.getSftmt(),rtgrol);
                    break;
                case "IZC":
                    title = Strings.getText("IZV Credit to $2",setgllVo.getSftmt(),setgllVo.getRol());
                    break;
                case "IZD":
                    title = Strings.getText("IZV Debit to $2",setgllVo.getSftmt(),setgllVo.getRol());
                    break;
                default:
                    single202 = Utils.getFieldValue(trndoc.getRec(),"cpdSwiftBizInfo//sin202","");
                    if( MdaUtils.compareTo(setgllVo.getSftmt(),"202") == 0 && MdaUtils.isEmpty(single202) && MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(),1,2),"CP") == 0 )
                    {
                        if( MdaUtils.compareTo(SettleContext.getTransName(),"CPTOPN") == 0 )
                        {
                            title = Strings.getText("MT 103 Payment to $1",setgllVo.getRol());
                        }
                        else
                        {
                            title = "MT202 COV";
                        }
                    }
                    else
                    {
                        //if SFTMT = "CMT" then
                        switch( setgllVo.getSftmt() )
                        {
                            case "CMT":
                                title = Strings.getText("CMT 100 Payment to $1",setgllVo.getRol());
                                break;
                            case "HVP":
                                title = Strings.getText("hvps111 Payment to $1",setgllVo.getRol());
                                break;
                            case "HVQ":
                                title = Strings.getText("hvps112 Payment to $1",setgllVo.getRol());
                                break;
                            case "CIU":
                                title = Strings.getText("cips111 Payment to $1",setgllVo.getRol());
                                break;
                            case "CIV":
                                title = Strings.getText("cips112 Payment to $1",setgllVo.getRol());
                                break;
                            case "CIW":
                                title = Strings.getText("cips113 Payment to $1",setgllVo.getRol());
                                break;
                            default:
                                if( MdaUtils.compareTo(setgllVo.getSftmt(),"100") == 0 )
                                {
                                    title = Strings.getText("境内外币支付付款指令");
                                }
                                else
                                {
                                    title = Strings.getText("MT $1 Payment to $2",setgllVo.getSftmt(),setgllVo.getRol());
                                }
                        }
                    }
            }
            // payment messages for final settlements only, payment panels are visible
            // Dspflg 一直为CG，因此去掉
            //if( MdaUtils.compareTo(ctx.absGet(Setmod.class).getDspflg(),"CG") == 0 )
            {
                // main message
                if(wrkPtspta !=null && ! MdaUtils.isEmpty(wrkPtspta.getPtainr()) )
                {
                    //根据业务员需求，400 与756默认不发
                    if( MdaUtils.compareTo(setgllVo.getSftmt(),"400") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"756") == 0 )
                    {
                        trndocService.defDocEot (trndoc, "SET", "PAY" + id, ownpath + "\\SETSUMS1", "LET", 2, wrkPtspta.getPts().getPtainr(), REC_PATH, title, "PAY", wrkPtspta.getPts().getAdrblk() );
                        // DNGDEV.000346
                        switch( setgllVo.getSftmt() )
                        {
                            case "756":
                                trndocService.setUseTag79Z (trndoc, "SET", "PAY" + id, "SWT" );
                                break;
                            default:
                                trndocService.setUseTag79Z (trndoc, "SET", "PAY" + id, "" );
                        }
                    }
                    else
                    {
                        /**
                         if SFTMT == "CMT" or SFTMT == "HVP" or SFTMT == "HVQ" then
                         **/
                        /**
                         if SFTMT == "CMT" or SFTMT == "HVP" or SFTMT == "HVQ" or SFTMT == "CIV" or SFTMT == "CIU" then
                         **/
                        if( MdaUtils.compareTo(setgllVo.getSftmt(),"CMT") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"HVP") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"HVQ") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"CIV") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"CIU") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"CIW") == 0 )
                        {
                            descr = "";
                            prf = "";
                            switch( setgllVo.getSftmt() )
                            {
                                case "CMT":
                                    descr = "CMT电汇凭证";
                                    prf = "CMT";
                                    break;
                                case "HVP":
                                    descr = "hvps111电汇凭证";
                                    prf = "HVP";
                                    break;
                                case "HVQ":
                                    descr = "hvps112电汇凭证";
                                    prf = "HVQ";
                                    break;
                                case "CIU":
                                    descr = "中国光大银行(跨境人民币结算)电汇凭证";
                                    prf = "CIU";
                                    break;
                                case "CIV":
                                    descr = "中国光大银行(跨境人民币结算)电汇凭证";
                                    prf = "CIV";
                                    break;
                                case "CIW":
                                    descr = "中国光大银行(跨境人民币结算)电汇凭证";
                                    prf = "CIW";
                                    break;
                                default:
                            }
                            /**
                             * 此处为汇款交易的Cpt/flg 需要给Rec赋值带过来
                             */
                            flg = Utils.getFieldValue(trndoc.getRec(),"cpdCipsBizInfo//flg","");
//                            Datas.getContent(setgllVo,"\\CPTP\\FLG");
                            if( MdaUtils.compareTo(flg,"X") == 0 )
                            {
                                /**
                                 if SFTMT == "CIV" or SFTMT == "CIU" then
                                 **/
                                if( MdaUtils.compareTo(setgllVo.getSftmt(),"CIV") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"CIU") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"CIVCOV") == 0 ) {
                                    if (MdaUtils.compareTo(setgllVo.getSftmt(),"CIU") == 0) {
                                        trndocService.defDocEot (trndoc, "TRN", "PAY" + id, ownpath + "\\CIPPAYL1", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, descr, "PAY", wrkPtspta.getAdrblk() );
                                        trndocService.setCORTYPList (trndoc, "TRN", "PAY" + id, "LET" );
                                    }
                                    if (MdaUtils.compareTo(setgllVo.getSftmt(),"CIV") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"CIVCOV") == 0) {
                                        trndocService.defDocEot (trndoc, "TRN", "PAY" + id, ownpath + "\\CIPPAYL2", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, descr, "PAY", wrkPtspta.getAdrblk() );
                                        trndocService.setCORTYPList (trndoc, "TRN", "PAY" + id, "LET" );
                                    }
                                }
                                else
                                {
                                    trndocService.defDocEot (trndoc, "TRN", "PAY" + id, ownpath + "\\CMTPAYL1", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, descr, "PAY", wrkPtspta.getAdrblk() );
                                    trndocService.setCORTYPList (trndoc, "TRN", "PAY" + id, "LET" );
                                }
                            }
                            else
                            {
                                //根据不同的报文类型得到不同的id号
                                //CMT、HVP报文使用同一张面函内部再改变细节
                                /**
                                 if SFTMT == "CIV" or SFTMT == "CIU" then
                                 **/
                                if( MdaUtils.compareTo(setgllVo.getSftmt(),"CIV") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"CIU") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"CIVCOV") == 0 ) {
                                    if (MdaUtils.compareTo(setgllVo.getSftmt(),"CIU") == 0) {
                                        trndocService.defDocEot (trndoc, "TRN", prf + id, ownpath + "\\CIPSPAYL1", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, title, "PAY", wrkPtspta.getAdrblk() );
                                        trndocService.setCORTYPList(trndoc, "TRN", prf + id, "LET" );
                                    }
                                    if (MdaUtils.compareTo(setgllVo.getSftmt(),"CIV") == 0) {
                                        trndocService.defDocEot (trndoc, "TRN", prf + id, ownpath + "\\CIPSPAYL2", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, title, "PAY", wrkPtspta.getAdrblk() );
                                        trndocService.setCORTYPList(trndoc, "TRN", prf + id, "LET" );
                                    }
                                    if (MdaUtils.compareTo(setgllVo.getSftmt(),"CIVCOV") == 0) {
                                        trndocService.defDocEot (trndoc, "TRN", prf + id, ownpath + "\\CIPSPAYL3", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, title, "PAY", wrkPtspta.getAdrblk() );
                                        trndocService.setCORTYPList(trndoc, "TRN", prf + id, "LET" );
                                    }
                                }
                                else
                                {
                                    trndocService.defDocEot (trndoc, "TRN", prf + id, ownpath + "\\MSGPAYL1", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, title, "PAY", wrkPtspta.getAdrblk() );
                                    trndocService.setCORTYPList (trndoc, "TRN", prf + id, "LET" );
                                }
                            }
                        }
                        else
                        {
                            if( MdaUtils.compareTo(setgllVo.getSftmt(),"100") == 0 )
                            {
                                dfdflg = Utils.getFieldValue(trndoc.getRec(),"dfdflg","");
                                if( MdaUtils.isEmpty(dfdflg) )
                                {
                                    trndocService.defDocEot (trndoc, "TRN", "PAY" + id, ownpath + "\\CHUPAYL1", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, title, "PAY", wrkPtspta.getAdrblk() );
                                    trndocService.setCORTYPList (trndoc, "TRN", "PAY" + id, "LET" );
                                }
                            }
                            else
                            {
                                if(!(MdaUtils.compareTo(setgllVo.getSftmt(),"202") == 0 && (MdaUtils.compareTo(cmtflg,"O") == 0 || MdaUtils.compareTo(cmtflg,"P") == 0))){
                                    trndocService.defDocEot (trndoc, "SET", "PAY" + id, ownpath + "\\SETSUMS1", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, title, "PAY", wrkPtspta.getAdrblk() );
                                }
                            }
                        }
                    }
                }
                // create cover message
                if( MdaUtils.isEmpty(setgllVo.getDirindflg()) )
                {
                    /**
                     \\TRNMOD\\TRNDOC.DefDocEot ( "SET", "COV" + $id, $ownpath + "\\\\SETSUMS3", "LET", 1, SETGRP\\APY\\PTA\\INR, RecGetPath( \\MTABUT\\REC ), 'LI000121', "COV", SETGRP\\APY\\PTS\\ADRBLK )
                     **/
                    //   \\TRNMOD\\TRNDOC.DefDocEot ( "SET", "COV" + $id, $ownpath + "\\\\SETSUMS3", "LET", 0, SETGRP\\APY\\PTA\\INR, RecGetPath( \\MTABUT\\REC ), 'RI000198
                    //人民币跨境结算，平202报文，出CMT100面涵或CMT100报文要素面涵（根据FLG挑勾域判断）
                    framename = SettleContext.getDirectTransName();
                    if( MdaUtils.compareTo(framename,"GITPSE") == 0||MdaUtils.compareTo(framename,"BRTSET") == 0 || MdaUtils.compareTo(framename,"BCTSET") == 0 || MdaUtils.compareTo(framename,"GITSET") == 0 || MdaUtils.compareTo(framename,"NITSET") == 0)
                    {
                        //cmtflg = Utils.getFieldValue(trndoc.getRec(),"cmtflg",""); //cmtflg = Datas.getContent(setgllVo,"\\TRNMOD\\CMTFLG");
//                        flg = Datas.getContent(setgllVo,"\\TRNMOD\\FLG");
                        //取出要出的报文类型
                        swftyp = Utils.getFieldValue(trndoc.getRec(),"swftyp",""); //Datas.getContent(setgllVo,"\\TRNMOD\\SWFTYP");
//                        prfd = "";
                        des = "";
                        des2 = "";
                        switch( swftyp )
                        {
//                            case "CMT":
//                                prfd = "CMT";
//                                des = "CMT 100";
//                                des2 = "CMT电汇凭证";
//                                break;
//                            case "HVP":
//                                prfd = "HVP";
//                                des = Strings.getText("hvps111 Payment to $1",setgllVo.getRol());
//                                des2 = "hvps111电汇凭证";
//                                break;
//                            case "HVQ":
//                                prfd = "HVQ";
//                                des = Strings.getText("hvps112 Payment to $1",setgllVo.getRol());
//                                des2 = "hvps112电汇凭证";
//                                break;
                            case "CIV":
//                                prfd = "CIV";
                                des = Strings.getText("cips112电汇凭证",setgllVo.getRol());
                                des2 = "cips112电汇凭证";
                                break;
                            default:
                        }
                        /**
                         if $cmtflg == "X" then
                         经与张磊分析，cmtflg == "X"的时候，即”人行支付系统“，已经不支持了
                         **/
                        if( MdaUtils.compareTo(cmtflg,"P") == 0 )
                        {
//                            if( MdaUtils.compareTo(flg,"X") == 0 )
//                            {
//                                trndocService.defDocEot (trndoc, "TRN", "PAY" + id, ownpath + "\\CMTPAYL1", "LET", 0, wrkPtspta.getPts().getPtainr(), REC_PATH, des2, "PAY", wrkPtspta.getPts().getAdrblk() );
//                                trndocService.setCORTYPList (trndoc, "TRN", "PAY" + id, "LET" );
//                            }
//                            else
//                            {
//                                if( MdaUtils.compareTo(cmtflg,"P") == 0 )
//                                {
                                    String prfd = "CIV";
                                    Assert.notNull(wrkPtspta);
                                    trndocService.defDocEot (trndoc, "TRN", prfd + id, ownpath + "\\CIPSPAYL2", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, "cips112电汇凭证", "PAY", wrkPtspta.getAdrblk() );
                                    trndocService.setCORTYPList (trndoc, "TRN", prfd + id, "LET" );
//                                }
//                                else
//                                {
//                                    trndocService.defDocEot (trndoc, "TRN", prfd + id, ownpath + "\\MSGPAYL1", "LET", 0, wrkPtspta.getPts().getPtainr(), REC_PATH, des, "PAY", wrkPtspta.getPts().getAdrblk() );
//                                    trndocService.setCORTYPList (trndoc, "TRN", prfd + id, "LET" );
//                                }
//                            }
                        }
                        else
                        {
                            dfuflg = Utils.getFieldValue(trndoc.getRec(),"oseflg");
                            //海外代付时默认不出
                            if( MdaUtils.compareTo(dfuflg,"X") == 0 )
                            {
                                trndocService.defDocEot (trndoc, "SET", "COV" + id, ownpath + "\\SETSUMS3", "LET", 2, setgllVo.getSetgrp().getApy().getPtainr(), REC_PATH, "MT202", "PAY", setgllVo.getSetgrp().getApy().getPts().getAdrblk() );
                            }
                            else
                            {
                                //人民币跨境结算，Other不出202    ren ming bi kuajing  jiesuan   "other" shi  bu chu 202
                                if( MdaUtils.compareTo(cmtflg,"O") != 0 )
                                {
                                    trndocService.defDocEot (trndoc, "SET", "COV" + id, ownpath + "\\SETSUMS3", "LET", 0, setgllVo.getSetgrp().getApy().getPtainr(), REC_PATH, "MT202", "PAY", setgllVo.getSetgrp().getApy().getPts().getAdrblk() );
                                }
                            }
                        }
                    }
                    else
                    {
                        trndocService.defDocEot (trndoc, "SET", "COV" + id, ownpath + "\\SETSUMS3", "LET", 0, setgllVo.getSetgrp().getApy().getPtainr(), REC_PATH, "MT202", "PAY", setgllVo.getSetgrp().getApy().getAdrblk() );
                    }
                }
            }
            // set allowed cortypes depending on disposition and message type
            switch( setgllVo.getDsp() )
            {
                case "RTC":
                    trndocService.setCORTYPList (trndoc, "SET", "PAY" + id, "RTG" );
                    if( MdaUtils.isEmpty(setgllVo.getDirindflg()) )
                    {
                        trndocService.setCORTYPList (trndoc, "SET", "COV" + id, "RTG" );
                    }
                    break;
                case "TGC":
                    trndocService.setCORTYPList (trndoc, "SET", "PAY" + id, "TAR" );
                    break;
                case "IZC":
                case  "IZD":
                    trndocService.setCORTYPList (trndoc, "SET", "PAY" + id, "IZV" );
                    break;
                default:
                    if( MdaUtils.compareTo(setgllVo.getSftmt(),"400") == 0 || MdaUtils.compareTo(setgllVo.getSftmt(),"756") == 0 )
                    {
                        ptainr = setgllVo.getSetgrp().getSnd756().getPtainr();
                    }
                    else
                    {
                        ptainr = setgllVo.getSetgrp().getBei().getPtainr();
                    }
                    if( MdaUtils.compareTo(doceotService.getPtmField ( ptainr, "SWT", "AUTFLG" ),"A") == 0 || MdaUtils.compareTo(doceotService.getPtmField ( ptainr, "SWT", "AUTFLG" ),"C") == 0 || MdaUtils.compareTo(setgllVo.getDirindflg(),"X") == 0 )
                    {
                        //   if \\TRNMOD\\TRNDOC\\DOCCUR.GetPtmField ( SESPAY\\BEIP\\PTSPTA\\PTA\\INR, "SWT", "AUTFLG" ) = "A" or DIRINDFLG = "X" then
                        trndocService.setCORTYPList (trndoc, "SET", "PAY" + id, "SWT" + CR + "LET" );
                    }
                    else
                    {
                        trndocService.setCORTYPList (trndoc, "SET", "PAY" + id, "LET" );
                        //  \\TRNMOD\\TRNDOC.SetCORTYPList ( "SET", "PAY" + $id, "SWT" + CR + "LET" )
                    }
                    if( MdaUtils.compareTo(doceotService.getPtmField ( setgllVo.getSetgrp().getApy().getPtainr(), "SWT", "AUTFLG" ),"A") == 0 )
                    {
                        trndocService.setCORTYPList (trndoc, "SET", "COV" + id, "SWT" + CR + "LET" );
                    }
                    else
                    {
                        trndocService.setCORTYPList (trndoc, "SET", "COV" + id, "LET" );
                    }
                    // Paymentmessage cannot be used for 756 and 400, if currency exchanged
                    switch( setgllVo.getSftmt() )
                    {
                        case "756":
                        case  "400":
                            if( MdaUtils.compareTo(setgllVo.getCur(),setgllVo.getAcccur()) != 0 )
                            {
                                trndocService.setCORTYPList (trndoc, "SET", "PAY" + id, "LET" );
                            }
                            break;
                        case "CMT":
                            trndocService.setCORTYPList (trndoc, "TRN", "CMT" + id, "LET" );
                            break;
                        case "100":
                            trndocService.setCORTYPList (trndoc, "TRN", "PAY" + id, "LET" );
                            break;
                    }
            }
        }
    }
}
