package org.example.Entity.System;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.example.Utils.DataTableUtil;
import org.example.Utils.DateTimeUtil;
import org.example.Utils.NativeExtensionUtils;

import java.util.*;

public class FlowStepInfo extends RowBase {
    protected Map<String, Object>[] _his;

    public FlowStepInfo(Map<String, Object> row, Map<String, Object>[] his) {
        super(row);
        this._his = his;
    }

    public int getId() {
        return NativeExtensionUtils.ToInt32(DataTableUtil.get(row, "id", 0));
    }

    @JsonProperty("StepName")
    public String getStepName() {
        String value = DataTableUtil.get(row, "stepName", "") + "";
        return value.trim().replace("\n", "");
    }

    @JsonProperty("stepName")
    public String getStepNameWithHisInfo() {
        if (getHisRec() != null) {
            Map<String, Object> hisRec = getHisRec();
            Date happenTime = NativeExtensionUtils.toDateTime(hisRec.get("happentime"));
            Date operTime = NativeExtensionUtils.toDateTime(hisRec.get("opertime"));
            String difDate = DateTimeUtil.getDateDiff(happenTime, operTime);
            String hisInfo = "(" + hisRec.get("operatorname") + ")→" +
                    (NativeExtensionUtils.isNullOrEmpty(difDate) ? "秒速通过" : difDate) + "\n" +
                    (hisRec.get("opertime") + "").replace(" ", "\t");
            return getStepName() + "\n" + hisInfo;
        }
        return getStepName();
    }

    @JsonProperty("stepCode")
    public int getStepCode() {
        return NativeExtensionUtils.ToInt32(DataTableUtil.get(row, "stepcode", 0));
    }

    private Integer _stepOver;

    @JsonProperty("stepOver")
    public int getStepOver() {
        if (_stepOver == null) {
            _stepOver = NativeExtensionUtils.ToInt32(DataTableUtil.get(row, "stepover", 0));
        }
        return _stepOver;
    }

    public void setStepOver(int value) {
        _stepOver = value;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("operUser")
    public String getOperUser() {
        String user = DataTableUtil.get(row, "operUser", "") + "";
        if (!NativeExtensionUtils.isNullOrEmpty(user)) {
            // 使用String.replaceAll()替代trim(',')，移除首尾的逗号
            return user.replaceAll("^,+,", "").replaceAll(",+$", "");
        }
        return null;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("viewUser")
    public String getViewUser() {
        return DataTableUtil.get(row, "viewUser", "") + "";
    }

    private Boolean _mulitAudit;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("multiAudit")
    public Boolean getMultiAudit() {
        if (_mulitAudit == null) {
            _mulitAudit = NativeExtensionUtils.toBoolean(DataTableUtil.get(row, "multiAudit", false));
        }
        return _mulitAudit ? _mulitAudit : null;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("stepTime")
    public Object getStepTime() {
        return DataTableUtil.get(row, "stepTime", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("AffirmBak")
    public Object getAffirmBak() {
        return DataTableUtil.get(row, "AffirmBak", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("flowcharTagId")
    public Object getFlowcharTagId() {
        return DataTableUtil.get(row, "flowcharTagId", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("flowcharWidth")
    public Object getFlowcharWidth() {
        return DataTableUtil.get(row, "flowcharWidth", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("flowcharHeght")
    public Object getFlowcharHeght() {
        return DataTableUtil.get(row, "flowcharHeght", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("FlowCharfillcolor")
    public Object getFlowCharfillcolor() {
        return DataTableUtil.get(row, "FlowCharfillcolor", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("LeftFlowCharfillcolor")
    public Object getLeftFlowCharfillcolor() {
        return DataTableUtil.get(row, "LeftFlowCharfillcolor", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("LeftFlowCharWidth")
    public Object getLeftFlowCharWidth() {
        return DataTableUtil.get(row, "LeftFlowCharWidth", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("fontSize")
    public Object getFontSize() {
        return DataTableUtil.get(row, "fontSize", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("fontColor")
    public Object getFontColor() {
        return DataTableUtil.get(row, "fontColor", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("LeftFlowCharTagId")
    public Object getLeftFlowCharTagId() {
        return DataTableUtil.get(row, "LeftFlowCharTagId", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("fontWeight")
    public Object getFontWeight() {
        return DataTableUtil.get(row, "fontWeight", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("fontFamily")
    public Object getFontFamily() {
        return DataTableUtil.get(row, "fontFamily", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("LeftFontFamily")
    public Object getLeftFontFamily() {
        return DataTableUtil.get(row, "LeftFontFamily", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("LeftFontSize")
    public Object getLeftFontSize() {
        return DataTableUtil.get(row, "LeftFontSize", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("LeftFontColor")
    public Object getLeftFontColor() {
        return DataTableUtil.get(row, "LeftFontColor", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("LeftFontWeight")
    public Object getLeftFontWeight() {
        return DataTableUtil.get(row, "LeftFontWeight", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("LeftFlowCharHeight")
    public Object getLeftFlowCharHeight() {
        return DataTableUtil.get(row, "LeftFlowCharHeight", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("FlowCharScale")
    public Object getFlowCharScale() {
        return DataTableUtil.get(row, "FlowCharScale", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("flowcharLineHeght")
    public Object getFlowcharLineHeght() {
        return DataTableUtil.get(row, "flowcharLineHeght", null);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("HisRec")
    public Map<String, Object> getHisRec() {
        if (_his == null || _his.length == 0) {
            return null;
        }

        Map<String, Object> hisTab = _his[_his.length - 1];
        if (hisTab != null && _his.length > 1) {
            List<String> operatorIds = new ArrayList<>();
            List<String> operatorNames = new ArrayList<>();
            List<String> operAdvices = new ArrayList<>();

            for (Map<String, Object> row : _his) {
                operatorIds.add(row.get("operatorid") + "");
                operatorNames.add(row.get("operatorname") + "");
                operAdvices.add(row.get("operatorname") + "：" + row.get("operAdvice"));
            }

            hisTab.put("operatorid", String.join(",", operatorIds));
            hisTab.put("operatorname", String.join(",", operatorNames));
            hisTab.put("operAdvice", String.join("</br>", operAdvices));
        }
        return hisTab;
    }

    @JsonIgnore
    public String getNextSteps() {
        return DataTableUtil.get(row, "NextStepCode", "") + "";
    }

    public void setNextSteps(String value) {
        row.put("NextStepCode", value);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("NextStepCode")
    public int[] getNextStepCode() {
        String nextSteps = getNextSteps();
        if (NativeExtensionUtils.isNullOrEmpty(nextSteps)) {
            return null;
        }

        String[] codeStrs = nextSteps.split(",");
        List<Integer> codeList = new ArrayList<>();
        for (String codeStr : codeStrs) {
            if (!NativeExtensionUtils.isNullOrEmpty(codeStr)) {
                codeList.add(NativeExtensionUtils.ToInt32(codeStr));
            }
        }

        if (codeList.isEmpty()) {
            return null;
        }

        int[] codes = new int[codeList.size()];
        for (int i = 0; i < codeList.size(); i++) {
            codes[i] = codeList.get(i);
        }
        return codes;
    }
}

// 补充必要的基础类定义（假设存在）
abstract class RowBase {
    protected Map<String, Object> row;

    public RowBase(Map<String, Object> row) {
        this.row = row;
    }

    protected Object GetVal(String key) {
        return DataTableUtil.get(row, key, null);
    }

    protected void SetVal(String key, Object value) {
        row.put(key, value);
    }
}

