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

import com.brilliance.isc.swift.objmtconvert.model.AbsMTCreator;
import com.brilliance.isc.swift.objmtconvert.util.Constans;
import com.prowidesoftware.swift.model.mt.AbstractMT;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author gechengyang
 * @date 2023/2/15
 */
public class MtResult extends Result {

    private boolean isSuccess = true;//报文数据校验是否成功

    private boolean continueFlg = true;//是否继续进行业务校验

    private String msgType = "";//mt报文的类型

    private String message = "";//存储mt报文信息

    private List dspList = new LinkedList();//业务校验生成的错误信息

    private AbstractMT abstractMT = null;

    private AbsMTCreator absMTCreator;

    public AbsMTCreator getAbsMTCreator() {
        return absMTCreator;
    }

    public void setAbsMTCreator(AbsMTCreator absMTCreator) {
        this.absMTCreator = absMTCreator;
    }

    public AbstractMT getAbstractMT() {
        return abstractMT;
    }

    public void setAbstractMT(AbstractMT abstractMT) {
        this.abstractMT = abstractMT;
    }

    private List getDspList() {
        return dspList;
    }

    public void setDspList(List dspList) {
        this.dspList = dspList;
    }

    public boolean isSuccess() {
        if (dspList.size() > 0)
            return false;
        return isSuccess;
    }

    public void setSuccess(boolean success) {
        isSuccess = success;
    }


    public String getMsgType() {
        return msgType;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        // 匹配整行只有冒号开头和结束的字符串，并且这两个冒号之间只包含除了"15"以外的任意数字或大写字母字符
        String pattern = "(?m)^:(?!15)([0-9]{2}[A-Z]?):$";

        // 创建 Pattern 对象
        Pattern regex = Pattern.compile(pattern);

        // 创建 Matcher 对象
        Matcher matcher = regex.matcher(message);

        // 替换匹配的部分为空
        this.message = matcher.replaceAll("").replaceAll("(?m)^[ \t]*\r?\n", "").toUpperCase();
    }


    public boolean isContinueFlg() {
        return continueFlg;
    }

    public void setContinueFlg(boolean continueFlg) {
        this.continueFlg = continueFlg;
    }


    /**
     * @return
     */

    public String getDescription() {

        String separator = Constans.CRLF;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < dspList.size(); i++) {
            sb.append(dspList.get(i)).append(separator);
        }
        return dspList.isEmpty() ? "" : sb.toString().substring(0, sb.toString().length() - 2);
    }

    public void addDescription(String dsp) {
        dspList.add(dsp);
    }

}
