package com.brilliance.isc.swift.objmtconvert.model.commontag;

import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.bo.Adr;
import com.brilliance.isc.bo.Pta;
import com.brilliance.isc.common.cache.pta.PtaCacheService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.context.SpringEnvContext;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.AdrMapper;
import com.brilliance.isc.mda.dao.PtaMapper;
import com.brilliance.isc.swift.objmtconvert.result.SwtExpandable;
import com.brilliance.isc.swift.objmtconvert.util.Constans;
import com.brilliance.swift.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 适用于MT报文的50 51 52 53 54 55 56 57 58 59域的处理
 *
 * @author gechengyang
 * @date 2023/2/20
 */


public class ActnoBicAdrTagXXX implements SwtExpandable {

    private String info;//  /1!a信息
    private String acctno;//账号
    private String bic;//银行BIC CODE
    private String code;


    private String partyIdentifier;//参与方标识

    private String location;//分行位置
    private String address;//名称地址
    private String narrative;//名称地址
    private String type;
    private String msgType;


    private String tagBefore;//tag的前缀，比如是50A、51B、52C，则tagBefore=50或者tagBefore=51或者tagBefore=52


    public ActnoBicAdrTagXXX(String tagBefore) {
        this.tagBefore = tagBefore;
    }

    public ActnoBicAdrTagXXX(String tagBefore, String msgType) {
        this.tagBefore = tagBefore;
        this.msgType = msgType;
    }

    /**
     * 判断地址信息是否为数字开头，如果是，则赋值为50F或者59F
     *
     * @param adr
     * @return
     */
    public static boolean aDrIsTagOfFLAG_F(String adr) {
        if (adr != null) {
            String[] adrs = adr.split(Constans.CRLF);
            for (int i = 0; i < adrs.length; i++) {
                String str = adrs[i];
                if (!str.matches("[0-9]{1}/([\\s\\S]*)")) {
                    return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }

    public String getNarrative() {
        return narrative;
    }

    public void setNarrative(String narrative) {
        this.narrative = narrative;
    }

    public String getMsgType() {
        return msgType;
    }

    public void setMsgType(String msgtType) {
        this.msgType = msgtType;
    }

    public String getPartyIdentifier() {
        return partyIdentifier;
    }

    public void setPartyIdentifier(String partyIdentifier) {
        this.partyIdentifier = partyIdentifier;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getTagBefore() {
        return tagBefore;
    }

    public void setTagBefore(String tagBefore) {
        this.tagBefore = tagBefore;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getAcctno() {
        return acctno;
    }

    public void setAcctno(String acctno) {
        this.acctno = acctno;
    }

    public String getBic() {
        return bic;
    }

    public void setBic(String bic) {
        this.bic = bic;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public TagResult getTagResult(boolean isExpand) {

        TagResult tagResult = new TagResult(tagBefore);
        List<String> tagDspList = tagResult.getTagDspList();
        tagDspList.clear();
        String partyIdentifer = getPartyIdentifier();

        if ("700".equals(msgType) && StringUtils.isNotEmpty(msgType) && "50".equals(tagBefore)) {
            tagResult.setTagEnd("");
            tagDspList.add(address);
            // tagResult.toResult();
            //return tagResult;
        }

        //处理24G
        if ("24".contains(tagBefore)) {
            //sb.append(code+Constans.CRLF+address);
            tagDspList.add(code);
            tagDspList.add(address);
            tagResult.setTagEnd(Constans.TAG_LAST_G);
            // tagResult.toResult();
            //return tagResult;
        }

        //处理25栏位，包括25 25P
        if ("25".equals(tagBefore)) {
            if (StringUtils.isNotEmpty(bic)) {
                tagResult.setTagEnd(Constans.TAG_LAST_P);
                //sb.append(acctno+Constans.CRLF+bic);
                tagDspList.add(acctno);
                tagDspList.add(bic);
            } else if (StringUtils.isNotEmpty(acctno)) {
                //sb.append(acctno);
                tagDspList.add(acctno);
            }
            // tagResult.setValue(sb.toString());
            //  tagResult.toResult();
            //return tagResult;
        }

        //处理40B栏位
        if ("40".equals(tagBefore)) {
            if (StringUtils.isNotEmpty(code)) {
                if (StringUtils.isNotEmpty(type))
                    tagResult.setTagEnd(Constans.TAG_LAST_B);
                tagDspList.add(type);
                tagDspList.add(code);
                //sb.append(type+Constans.CRLF+code);
            }
            //  tagResult.setValue(sb.toString());
            //  tagResult.toResult();
            //return tagResult;
        }

        //处理41栏位，包括A D F G
        if ("41".equals(tagBefore)) {
            if (StringUtils.isNotEmpty(code)) {
                if (StringUtils.isNotEmpty(bic)) {
                    tagResult.setTagEnd(Constans.TAG_LAST_A);
                    //sb.append(bic+Constans.CRLF+code);
                    tagDspList.add(bic);
                    if (!isExpand) {
                        //报文预览的时候code会放到address里面，这里不需要重复添加了
                        tagDspList.add(code);
                    }
                } else if (StringUtils.isNotEmpty(address)) {
                    tagResult.setTagEnd(Constans.TAG_LAST_D);
                    //sb.append(address+Constans.CRLF+code);
                    tagDspList.add(address);
                    tagDspList.add(code);
                }
            } else {
                if (StringUtils.isNotEmpty(bic)) {
                    tagResult.setTagEnd(Constans.TAG_LAST_F);
                    //sb.append(bic);
                    tagDspList.add(bic);
                } else if (StringUtils.isNotEmpty(address)) {
                    tagResult.setTagEnd(Constans.TAG_LAST_G);
                    //sb.append(address);
                    tagDspList.add(address);
                }
            }
            // tagResult.toResult();
            //return tagResult;
        }


        //先处理F栏位
        if ("50|59".contains(tagBefore) && !"210".equals(msgType) && !"101".equals(getMsgType())) {
            if (aDrIsTagOfFLAG_F(address)) {
                if ("50".equals(tagBefore)) {
                    if (StringUtils.isNotEmpty(partyIdentifer)) {
                        tagResult.setTagEnd(Constans.TAG_LAST_F);
                        //sb.append(partyIdentifer);
                        // sb.append(Constans.CRLF);
                        tagDspList.add(partyIdentifer);
                    }
                }
                if ("59".equals(tagBefore)) {
                    tagResult.setTagEnd(Constans.TAG_LAST_F);
                    if (StringUtils.isNotEmpty(acctno)) {
                        // sb.append("/" + acctno);
                        //  sb.append(Constans.CRLF);
                        tagDspList.add("/" + acctno);
                    }
                }
                // sb.append(address);
                tagDspList.add(address);
                //tagResult.setValue(sb.toString());
                //  tagResult.toResult();
                //return tagResult;
            }
        }
        //处理50K栏位
        if ("50".equals(tagBefore) && !"101".equals(getMsgType())) {
            //处理50K栏位
            if (StringUtils.isNotEmpty(address)) {
                tagResult.setTagEnd(Constans.TAG_LAST_K);
                if (StringUtils.isNotEmpty(acctno)) {
                    //sb.append("/" + acctno);
                    // sb.append(Constans.CRLF);
                    tagDspList.add("/" + acctno);
                }
                tagDspList.add(address);
                //sb.append(address);
                // tagResult.toResult();
                //tagResult.setValue(sb.toString());
                //return tagResult;
            }
        }


        if ("59".contains(tagBefore) && StringUtils.isEmpty(tagResult.getTagEnd())) {
            if (!StringUtils.isEmpty(acctno)) {
                tagDspList.add("/" + acctno);
            }
        } else {
            if (StringUtil.isNotEmpty(info) && StringUtil.isNotEmpty(partyIdentifer)) {
                tagDspList.add("/" + info + "/" + partyIdentifer);
            } else {
                if (!StringUtils.isEmpty(info)) {
                    //sb.append("/" + info);
                    tagDspList.add("/" + info);
                }
                if (!StringUtils.isEmpty(partyIdentifer)) {
                    // 不再判断是否展开，在预览时所有账号的前面都加上/符号
                    tagDspList.add("/" + partyIdentifer);
                }
            }
        }

//        if (StringUtils.isNotEmpty(sb.toString())) {
//            sb.append(Constans.CRLF);
//        }


        if ("50|51|52|53|54|55|56|57|58|59|42".contains(tagBefore)) {
            //bic不为空 XX A
            if (StringUtils.isNotEmpty(bic) && !"210".equals(msgType)) {
                tagResult.setTagEnd(Constans.TAG_LAST_A);
                tagDspList.add(bic);
            }
            //XX B
            else if (StringUtils.isNotEmpty(location)) {
                tagResult.setTagEnd(Constans.TAG_LAST_B);
                tagDspList.add(location);
            }
            //XX D  特注：50没有D
            else if (!"50".equals(tagBefore) && StringUtils.isNotEmpty(address) && !"103_STP".equals(msgType) && !"110".equals(msgType) && !"101".equals(msgType)) {
                if (!"59".equals(tagBefore)) {
                    tagResult.setTagEnd(Constans.TAG_LAST_D);
                }
                //59F在上面已经赋值了
                if (!Constans.TAG_LAST_F.equals(tagResult.getTagEnd())) {
                    tagDspList.add(address);
                }
            } else if (StringUtils.isNotEmpty(partyIdentifer) && "56|57".contains(tagBefore)) {
                tagResult.setTagEnd(Constans.TAG_LAST_C);
                tagDspList.add(partyIdentifer);
            }
            //tagResult.setValue(sb.toString());
            //tagResult.toResult();
        }

        if ("71|72|73|77".contains(tagBefore)) {
            //code存在   输出N
            if (StringUtils.isNotEmpty(code)) {
                tagResult.setTagEnd(Constans.TAG_LAST_N);
                tagDspList.add(code);
            }
            // narrative 存在就直接加后面
            else if (StringUtils.isNotEmpty(narrative)) {
                tagDspList.add(narrative);
            }
            if ("71".equals(tagBefore) && MdaUtils.isEmpty(code)) {
                tagResult.setTagEnd(Constans.TAG_LAST_D);
            }
            if ("72".equals(tagBefore)) {
                tagResult.setTagEnd(Constans.TAG_LAST_Z);
            }
            if ("73".equals(tagBefore)) {
                tagResult.setTagEnd(Constans.TAG_LAST_A);
            }
            if ("77".equals(tagBefore)) {
                tagResult.setTagEnd(Constans.TAG_LAST_J);
            }
            // tagResult.setValue(sb.toString());
            //tagResult.toResult();
        }

        //处理210的50a栏位
        if ("210".equals(msgType) && StringUtils.isNotEmpty(msgType) && "50".equals(tagBefore)) {
            if (StringUtils.isNotEmpty(address)) {
                if (StringUtils.isNotEmpty(partyIdentifer)) {
                    tagResult.setTagEnd(Constans.TAG_LAST_F);
                    tagDspList.add(address);
                } else {
                    tagResult.setTagEnd("");
                    tagDspList.add(address);
                }
            } else {
                tagResult.setTagEnd(Constans.TAG_LAST_C);
                tagDspList.add(bic);
            }
        }

        //处理101的50a栏位,该报文有两个50a Tag栏位
        if ("50".contains(tagBefore) && "101".equals(getMsgType())) {
            if (StringUtils.isNotEmpty(bic)) {
                if (StringUtils.isEmpty(acctno)) {
                    tagResult.setTagEnd(Constans.TAG_LAST_C);
                    //sb.append(bic);
//                    tagDspList.add(bic);
                } else {
                    tagResult.setTagEnd(Constans.TAG_LAST_G);
                    // sb.append(acctno);
                    // sb.append(Constans.CRLF);
                    // sb.append(bic);
//                    tagDspList.add(acctno);
//                    tagDspList.add(bic);
                }
            } else if (aDrIsTagOfFLAG_F(address)) {
                if (StringUtils.isNotEmpty(partyIdentifer)) {
                    tagResult.setTagEnd(Constans.TAG_LAST_F);
                    //sb.append(partyIdentifer);
                    //sb.append(Constans.CRLF);
                    // sb.append(address);
                    tagDspList.add(partyIdentifer);
                    tagDspList.add(address);
                }
            } else if (StringUtils.isEmpty(address)) {
                tagResult.setTagEnd(Constans.TAG_LAST_L);
                //sb.append(partyIdentifer);
                tagDspList.add(partyIdentifer);
            } else if (StringUtils.isNotEmpty(acctno) && StringUtils.isNotEmpty(address) && StringUtils.isEmpty(bic) && StringUtils.isEmpty(partyIdentifer)) {
                tagResult.setTagEnd(Constans.TAG_LAST_H);
                //sb.append(acctno);
                //sb.append(Constans.CRLF);
                // sb.append(address);
//                tagDspList.add(acctno);
                tagDspList.add(address);
            }
            // tagResult.setValue(sb.toString());
            // tagResult.toResult();
            // return tagResult;
        }
        //处理50A,50F,50K，59,59A,59F的问题
        if (StringUtil.isNotEmpty(msgType) && "103_STP|110|910|205COV".contains(msgType)) {
            tagDspList.clear();
            tagResult.setTagEnd("");
            if (StringUtil.isNotEmpty(acctno)) {
                tagDspList.add("/" + acctno);
            }
            if (StringUtil.isNotEmpty(partyIdentifer)) {
                tagDspList.add(partyIdentifer);
            }
            if ("50|59".contains(tagBefore)) {
                if (StringUtil.isNotEmpty(bic)) {
                    tagDspList.add(bic);
                    tagResult.setTagEnd(Constans.TAG_LAST_A);
                }
                if (StringUtil.isNotEmpty(address)) {
                    if (aDrIsTagOfFLAG_F(address)) {
                        tagResult.setTagEnd(Constans.TAG_LAST_F);
                    } else {
                        if ("50".contains(tagBefore)) {
                            tagResult.setTagEnd(Constans.TAG_LAST_K);
                        }
                    }
                    tagDspList.add(address);
                }
            }
        }
        String bicDesp = " BIC identified as:\r\n";
        if (StringUtil.isNotEmpty(bic) && isExpand) {
            //展开模式
            int lastIndex = tagDspList.size() - 1;
            if (StringUtil.isNotEmpty(address)) {
                tagDspList.set(lastIndex, tagDspList.get(lastIndex).trim() + bicDesp + this.address);
            } else if (StringUtil.isNotEmpty(narrative)) {
                tagDspList.set(lastIndex, tagDspList.get(lastIndex).trim() + bicDesp + narrative);
            }
        }

        Map<String, Object> tagValueMap = new HashMap<>();
        if (StringUtil.isNotEmpty(info)) {
            tagValueMap.put("info", info);
        }
        if (StringUtil.isNotEmpty(acctno)) {
            tagValueMap.put("acctno", acctno);
        }
        if (StringUtil.isNotEmpty(bic)) {
            tagValueMap.put("bic", bic);
        }
        if (StringUtil.isNotEmpty(code)) {
            tagValueMap.put("code", code);
        }
        if (StringUtil.isNotEmpty(partyIdentifier)) {
            tagValueMap.put("partyIdentifier", partyIdentifier);
        }
        if (StringUtil.isNotEmpty(location)) {
            tagValueMap.put("location", location);
        }
        if (StringUtil.isNotEmpty(address)) {
            tagValueMap.put("address", address);
        }
        if (StringUtil.isNotEmpty(narrative)) {
            tagValueMap.put("narrative", narrative);
        }
        if (StringUtil.isNotEmpty(type)) {
            tagValueMap.put("type", type);
        }
        tagResult.setTagValueMap(tagValueMap);
        return tagResult;
    }

    @Override
    public TagResult getExpandResult() {
        return getTagResult(true);
    }

    /**
     * 读取快照文件时报文展开方法。
     *
     * @param param 报文原值
     * @return TagResult
     */
    @Override
    public TagResult getExpandResult(String param) {
        TagResult tagResult = new TagResult(tagBefore);
        List<String> tagDspList = tagResult.getTagDspList();
        //针对已经是名址的，或不是以A结尾的，都不用继续后续BIC转名址
        if (param.length() > 11 || !tagBefore.endsWith("A")) {
            tagDspList.add(param);
            return tagResult;
        }
        PtaCacheService ptaCacheService = SpringEnvContext.getBean(PtaCacheService.class);
        Pta pta = ptaCacheService.selectByPtyextkeyAndUsg(param, "MAA");
        if (pta != null) {
            PtsptaToolComponent ptsptaToolComponent = SpringEnvContext.getBean(PtsptaToolComponent.class);
            PtsptaVo ptspta = ptsptaToolComponent.getPtsptaFromPtaINR(pta.getInr());
            tagDspList.add(param + " BIC identified as:\r\n" + ptspta.getAdrblk());
        } else {
            tagDspList.add(param);

        }
        return tagResult;
    }

    public TagResult getTagResult() {
        return getTagResult(false);
    }

    private String handleAdr(String adr) {
        StringBuffer sb = new StringBuffer();
        if (adr != null) {
            String[] adrs = adr.split(Constans.CRLF);
            for (int i = 0; i < adrs.length; i++) {
                sb.append((i + 1) + "/" + adrs[i] + Constans.CRLF);
            }
        }
        return sb.toString();
    }
}
