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

import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.register.document.IDocumentRegister;
import com.brilliance.isc.common.setgll.vo.SetgllVo;
import com.brilliance.isc.common.trndoc.service.TrndocService;
import com.brilliance.isc.common.trndoc.vo.TrndocVo;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

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

    @Autowired
    private TrndocService trndocService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Override
    public void registerDocument(TrndocVo trndoc) {
        List<SetgllVo> setglls = trndoc.getSetglg().getSetgll();
        if (setglls == null || setglls.size() == 0) {
            return;
        }
        for (int i=0; i<setglls.size(); i++) {
            registerDocumentOfLine(trndoc, setglls.get(i), i);
        }
    }

    /**
     * 根据sftmt注册报文面函
     * @param trndoc
     * @param setgllVo
     * @param idx
     */
    private void registerDocumentOfLine(TrndocVo trndoc, SetgllVo setgllVo, int idx){
        String sftmt = setgllVo.getSftmt();
        if (StringUtils.isEmpty(sftmt)) {
            return;
        }
        String REC_PATH = "\\SETGLG\\SETGLL("+idx+")";
        idx += 1;
        String title = "";
        PtsptaVo wrkPtspta = null;
        switch (sftmt) {
            case "103":
            case "202":
                title = Strings.getText("MT $2 Payment to $1", setgllVo.getRol(), sftmt);
                wrkPtspta = setgllVo.getSetgrp().getApy();
                if (wrkPtspta != null && StringUtils.isNotEmpty(wrkPtspta.getPtainr())) {
                    trndocService.defDocEot(trndoc, "SET", "PAY" + idx,"\\SETSUMS1", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, title, "PAY", wrkPtspta.getAdrblk());
                    trndocService.setDocSendFormat(trndoc, "SETPAY"+idx, "SWT", "MT"+sftmt);
                }
                break;
            case "400":
            case "756":
                title = Strings.getText("MT $2 Payment to $1", setgllVo.getRol(), sftmt);
                wrkPtspta = setgllVo.getSetgrp().getSnd756();
                if (wrkPtspta != null && StringUtils.isNotEmpty(wrkPtspta.getPtainr())) {
                    trndocService.defDocEot(trndoc, "SET", "PAY" + idx, "\\SETSUMS1", "LET", 2, wrkPtspta.getPtainr(), REC_PATH, title, "PAY", wrkPtspta.getAdrblk());
                    trndocService.setDocSendFormat(trndoc, "SETPAY"+idx, "SWT", "MT"+sftmt);
                }
                break;
            case "CIV":
                //跨境人民币112支付
                title = Strings.getText("cips112 Payment to $1", setgllVo.getRol());
                wrkPtspta = setgllVo.getSetgrp().getApy();
                if (wrkPtspta != null && StringUtils.isNotEmpty(wrkPtspta.getPtainr())) {
                    //电汇凭证
                    String flg = Utils.getFieldValue(trndoc.getRec(), "cpdCipsBizInfo//flg", "");
                    if ("X".equals(flg)) {
                        trndocService.defDocEot(trndoc, "TRN", "PAY" + idx,"\\CIPPAYL2", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, "中国光大银行(跨境人民币结算)电汇凭证", "PAY", wrkPtspta.getAdrblk());
                    } else {
                        trndocService.defDocEot(trndoc, "TRN", "PAY" + idx,"\\CIPSPAYL2", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, title, "PAY", wrkPtspta.getAdrblk());
                    }
                }
                break;
            case "CIU":
                //跨境人民币111支付
                title = Strings.getText("cips111 Payment to $1", setgllVo.getRol());
                wrkPtspta = setgllVo.getSetgrp().getApy();
                if (wrkPtspta != null && StringUtils.isNotEmpty(wrkPtspta.getPtainr())) {
                    //电汇凭证
                    String flg = Utils.getFieldValue(trndoc.getRec(), "cpdCipsBizInfo//flg", "");
                    if ("X".equals(flg)) {
                        trndocService.defDocEot(trndoc, "TRN", "PAY" + idx,"\\CIPPAYL1", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, "中国光大银行(跨境人民币结算)电汇凭证", "PAY", wrkPtspta.getAdrblk());
                    } else {
                        trndocService.defDocEot(trndoc, "TRN", "PAY" + idx,"\\CIPSPAYL2", "LET", 0, wrkPtspta.getPtainr(), REC_PATH, title, "PAY", wrkPtspta.getAdrblk());
                    }
                }
                break;
            case "111":
            case "112":
                //境内外币111
                title = "FXPS.111境内跨行贷记";
                if ("112".equals(sftmt)) {
                    //境内外币112
                    title = "FXPS.112银行间资金调拨";
                }
                wrkPtspta = setgllVo.getSetgrp().getApy();
                if ("CPTOPN".equals(SettleContext.getTransName())) {
                    wrkPtspta = ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "SND");
                } else if ("DFTMSG".equals(SettleContext.getTransName())) {
                    wrkPtspta = ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "DFF");
                }
                if (wrkPtspta != null && StringUtils.isNotEmpty(wrkPtspta.getPtainr())) {
                    trndocService.defDocEot(trndoc, "TRN", "APL2", "\\ATTAPLQ2", "FMT", 0, wrkPtspta.getPtainr(), REC_PATH, title, wrkPtspta.getRol(), wrkPtspta.getAdrblk());
                }
                break;
            default:
                break;
        }
        String dirindflg = setgllVo.getDirindflg();
        if (StringUtils.isEmpty(dirindflg)) {
            //空表示indirect payment(2 messages) 参考码表DIRIND
            //会发202报文
            PtsptaVo ptspta = setgllVo.getSetgrp().getApy();
            if (ptspta != null && StringUtils.isNotEmpty(ptspta.getPtainr())) {
                trndocService.defDocEot(trndoc, "SET", "COV" + idx, "\\SETSUMS3", "LET", 0, ptspta.getPtainr(), REC_PATH, "MT202", "PAY", ptspta.getAdrblk());
                trndocService.setDocSendFormat(trndoc, "SETCOV"+idx, "SWT", "MT202");
            }
        }
    }
}
