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

import com.brilliance.swift.util.StringUtil;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.swift.objmtconvert.result.SwtExpandable;
import com.brilliance.isc.swift.objmtconvert.util.Constans;
import com.brilliance.isc.swift.objmtconvert.util.FormatUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zh
 * @create 2023-02-24-10:26
 * 适用于32，33,60，62等有借记贷记日期币种金额的域
 */
public class DcDateCurAmtTagXXX implements SwtExpandable {

    private String dcMark; //借记还是贷记  C代表贷记，D代码借记

    private String dayMonth;//D代表周期按天记，M代表周期按月记

    private String numberOfDaysMonths;//月份或者天数

    private String code;//代码

    private String date;   //日期,货币,金额

    private int number = 0; //

    private String currency;//币种

    private BigDecimal amount;//金额

    private String tagBefore;//tag的前缀，比如是62F,62M

    private String msgType;//报文类型

    private int sequenceNumber = 1;//分页序号

    private String valueString;

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

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

    public String getValueString() {
        return valueString;
    }

    public void setValueString(String valueString) {
        this.valueString = valueString;
    }

    public String getMsgType() {
        return msgType;
    }

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

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getSequenceNumber() {
        return sequenceNumber;
    }

    public void setSequenceNumber(int sequenceNumber) {
        this.sequenceNumber = sequenceNumber;
    }

    public String getDayMonth() {
        return dayMonth;
    }

    public void setDayMonth(String dayMonth) {
        this.dayMonth = dayMonth;
    }

    public String getNumberOfDaysMonths() {
        return numberOfDaysMonths;
    }

    public void setNumberOfDaysMonths(String numberOfDaysMonths) {
        this.numberOfDaysMonths = numberOfDaysMonths;
    }

    public String getCode() {
        return code;
    }

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

    public String getTagBefore() {
        return tagBefore;
    }

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

    public String getDcMark() {
        return dcMark;
    }

    public void setDcMark(String dcMark) {
        this.dcMark = dcMark;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

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

    public TagResult getTagResult(boolean isExpand) {
        TagResult tagResult = new TagResult(tagBefore);
        StringBuilder sb = new StringBuilder();
        List<String> tagDspList = tagResult.getTagDspList();
        tagDspList.clear();
        String swiftAmount = "";
        if (amount != null) {
            if (isExpand) {
                swiftAmount = getExpandAmount(isExpand);
            } else {
                swiftAmount = Utils.formatAmt4Swt(amount, currency);
            }
        }
        if (tagBefore.contains("90")) {
            if (number != 0) {
                sb.append(number);
            }
            if (StringUtil.isNotEmpty(currency)) {
                sb.append(currency);
            }
            if (StringUtil.isNotEmpty(swiftAmount)) {
                sb.append(swiftAmount);
            }
//            tagResult.setValue(sb.toString());
            tagDspList.add(sb.toString());
        }
        //处理32栏位
        if ("32|33".contains(tagBefore)) {
            if (StringUtils.isEmpty(msgType)) {
                if (StringUtils.isNotEmpty(date)) {
                    tagResult.setTagEnd(Constans.TAG_LAST_A);
                    sb.append(getExpandDate(isExpand));
                }
            } else {
                if (StringUtils.isNotEmpty(date)) {
                    tagResult.setTagEnd(Constans.TAG_LAST_D);
                    sb.append(getExpandDate(isExpand));
                }
            }
            if (StringUtils.isNotEmpty(dayMonth) || StringUtils.isNotEmpty(numberOfDaysMonths)) {
                tagResult.setTagEnd(Constans.TAG_LAST_K);
                if (StringUtils.isNotEmpty(dayMonth)) {
                    if(isExpand){
                        sb.append(dayMonth).append(" ");
                    }else{
                        sb.append(dayMonth);
                    }
                }
                if (StringUtils.isNotEmpty(numberOfDaysMonths)) {
                    if(isExpand){
                        sb.append(numberOfDaysMonths).append(" ");
                    }else{
                        sb.append(numberOfDaysMonths);
                    }
                }
            }
            if (StringUtils.isEmpty(dayMonth) && StringUtils.isEmpty(numberOfDaysMonths) && StringUtils.isEmpty(date)) {
                tagResult.setTagEnd(Constans.TAG_LAST_B);
            }
            if (StringUtils.isNotEmpty(code) && Constans.TAG_LAST_K.equals(tagResult.getTagEnd())) {
                if(isExpand){
                    sb.append(code).append(" ");
                }else{
                    sb.append(code);
                }
            }
            if (StringUtils.isNotEmpty(currency) && StringUtils.isNotEmpty(swiftAmount)) {
                sb.append(currency).append(swiftAmount);
            }
//            tagResult.setValue(sb.toString());
            tagDspList.add(sb.toString());
//            return tagResult;
        }

        //处理34栏位，34A|34B
        if ("34".contains(tagBefore)) {
            if (StringUtils.isNotEmpty(date) && StringUtils.isNotEmpty(currency) && StringUtils.isNotEmpty(swiftAmount)) {
                tagResult.setTagEnd(Constans.TAG_LAST_A);
                sb.append(getExpandDate(isExpand)).append(currency).append(swiftAmount);
            } else if (StringUtils.isNotEmpty(dcMark) && StringUtils.isNotEmpty(currency) && StringUtils.isNotEmpty(swiftAmount)) {
                tagResult.setTagEnd(Constans.TAG_LAST_F);
                sb.append(currency).append(dcMark).append(swiftAmount);
            } else if (StringUtils.isEmpty(date) && StringUtils.isNotEmpty(currency) && StringUtils.isNotEmpty(swiftAmount)) {
                tagResult.setTagEnd(Constans.TAG_LAST_B);
                sb.append(currency).append(swiftAmount);
            }
//            if(StringUtils.isNotEmpty(currency)&&StringUtils.isNotEmpty(swiftAmount)){
//                sb.append(currency).append(amount);
//            }
//            tagResult.setValue(sb.toString());
            tagDspList.add(sb.toString());
//            return tagResult;
        }

        if ("60|62|64|65".contains(tagBefore)) {
            if ("60|62".contains(tagBefore)) {
                if (1 == sequenceNumber) {
                    tagResult.setTagEnd(Constans.TAG_LAST_F);
                } else {
                    tagResult.setTagEnd(Constans.TAG_LAST_M);
                }
            }

            if (StringUtils.isNotEmpty(dcMark)) {
                sb.append(dcMark);
            }
            if (StringUtils.isNotEmpty(date)) {
                sb.append(getExpandDate(isExpand));
            }
            if (StringUtils.isNotEmpty(currency)) {
                sb.append(currency);
            }
            if (StringUtils.isNotEmpty(swiftAmount)) {
                sb.append(swiftAmount);
            }
//            tagResult.setValue(sb.toString());
            tagDspList.add(sb.toString());
        }
        //处理71栏位。
        if ("71".equals(tagBefore)) {
            if (StringUtil.isNotEmpty(currency)) {
                sb.append(currency);
            }
            if (StringUtil.isNotEmpty(swiftAmount)) {
                sb.append(swiftAmount);
            }
            tagDspList.add(sb.toString());
        }
        Map<String, Object> tagValueMap = new HashMap<>();
        //判断每个域的值是否为空，不为空则添加添加括号到stringBuilder中
        if (StringUtil.isNotEmpty(dcMark)) {
            tagValueMap.put("dcMark", dcMark);
        }
        if (StringUtil.isNotEmpty(dayMonth)) {
            tagValueMap.put("dayMonth", dayMonth);
        }
        if (StringUtil.isNotEmpty(numberOfDaysMonths)) {
            tagValueMap.put("numberOfDaysMonths", numberOfDaysMonths);
        }
        if (StringUtil.isNotEmpty(code)) {
            tagValueMap.put("code", code);
        }
        if (StringUtil.isNotEmpty(date)) {
            tagValueMap.put("date", getExpandDate(isExpand));
        }
        if (number != 0) {
            tagValueMap.put("number", number);
        }
        if (StringUtil.isNotEmpty(currency)) {
            tagValueMap.put("currency", currency);
        }
        if (StringUtil.isNotEmpty(swiftAmount)) {
            tagValueMap.put("amount", swiftAmount);
        }
        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();
        tagDspList.add(FormatUtils.fmt6NDate(param.substring(0, 6)) + param.substring(6));
        return tagResult;
    }

    public String getExpandDate(boolean expand) {
        if (expand) {
            return "  " + FormatUtils.fmt6NDate(date) + "  ";
        } else {
            return date;
        }
    }

    public String getExpandAmount(boolean expand) {
        if (expand) {
            return "  " + FormatUtils.fmtAmount(amount, currency) + "  ";
        } else {
            return amount.toPlainString();
        }
    }

}
