package org.example.Entity.Control.Com;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.example.Entity.Control.Base.RowComponent;
import org.example.Utils.DataTableUtil;
import org.example.Utils.IPublicUtil;
import org.example.Utils.NativeExtensionUtils;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * ==============================================================================
 * 功能描述：SysPoPupMenu  右键菜单/单据工具表其他参数，
 * 注意：其他参数中 以‘@’开头的，表示sql语句，以‘!’开头的表示存储过程，以#开头的表示多返回值得sql一般使用在拓展插件里面 所有以‘{}’包裹的，都要替换为对应的值
 * 备注：actiontype ：0表示存储过程 需要其他参数作为参数，一一对应，1表示sql 不需要其他参数，但需要替换对应行数据
 * ==============================================================================
 */
public class SysPoPupMenuBtn extends Button {


    public SysPoPupMenuBtn() {
        super();
    }

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

    public SysPoPupMenuBtn(Map<String, Object> row, IPublicUtil util) {
        super(row, util);
        this._row = row;
        this.setUtil(util);
    }

    @JsonIgnore
    @Override
    public String getXtype() {
        return null;
    }

    @Override
    public void setXtype(String xtype) {
        super.setXtype(xtype);
    }

    @JsonIgnore
    public int MenuBtnId;

    @JsonIgnore
    public String dllname;

    private String __dllname;

    public String get_dllname() {
        if (dllname == null && getStoreName() != null && !getStoreName().isEmpty()) {
            return getStoreName().toLowerCase();
        }
        if (__dllname == null) {
            if (dllname == null) return "";
            String[] names = dllname.toLowerCase().split("\\.");
            __dllname = names.length > 1 ? names[1] : "";

            if (names.length >= 3) {
                String[] specialNames = {"tplbatchadd", "tpladd", "wordadd", "batchadd", "tableadd", "tabadd"};
                for (String specialName : specialNames) {
                    if (specialName.equals(names[2]) || dllname.toLowerCase().contains(".machine")) {
                        __dllname = names[2];
                        break;
                    }
                }
            }
        }
        return __dllname;
    }


    public String action;

    protected String getStoreName() {
        if (actiontype == 1 && action != null && !action.trim().isEmpty()) {
            return action.trim().split(" ")[0];
        }
        return "";
    }

    public int actiontype;


    @JsonIgnore
    public Map<String, Object> Record;

    /// <summary>
    /// 其他参数1
    /// </summary>
    @JsonIgnore
    public String dllpar1;
    /// <summary>
    /// 其他参数2
    /// </summary>
    @JsonIgnore
    public String dllpar2;
    /// <summary>
    /// 其他参数3
    /// </summary>
    @JsonIgnore
    public String dllpar3;
    /// <summary>
    /// 其他参数4
    /// </summary>
    @JsonIgnore
    public String dllpar4;
    /// <summary>
    /// 其他参数5
    /// </summary>
    @JsonIgnore
    public String dllpar5;
    /// <summary>
    /// 其他参数6
    /// </summary>
    @JsonIgnore
    public String dllpar6;
    /// <summary>
    /// 其他参数7
    /// </summary>
    @JsonIgnore
    public String dllpar7;
    /// <summary>
    /// 其他参数8，单据为主表sql
    /// </summary>
    @JsonIgnore
    public String dllpar8;
    /// <summary>
    /// 其他参数9,，单据为明细sql
    /// </summary>
    @JsonIgnore
    public String dllpar9;
    /// <summary>
    /// 其他参数10，单据为单据编号
    /// </summary>
    @JsonIgnore
    public String dllpar10;
    @JsonIgnore
    public String comfirm;
    /// <summary>
    /// 特殊参数是否选择人员或者步骤，用于提交或者审批
    /// </summary>
    @JsonIgnore
    public String selectConfirmFlag;
    /// <summary>
    /// 特殊参数是否选择人员或者步骤，用于提交或者审批
    /// </summary>
    @JsonIgnore
    public String nextSelectStepCode;
    /// <summary>
    /// 特殊参数是否选择人员或者步骤，用于提交或者审批
    /// </summary>
    @JsonIgnore
    public String nextSelectStepOper;
    /// <summary>
    /// 转发需要人员
    /// </summary>
    @JsonIgnore
    public String comfirmOpers;
    /// <summary>
    /// 转发备注
    /// </summary>
    @JsonIgnore
    public String remark;
    @JsonIgnore
    public boolean maxWindow;
    /// <summary>
    /// 右键打开方式 0：窗口 1：tab,2:内嵌
    /// </summary>
    @JsonIgnore
    public int showMode;

    /// <summary>
    /// 1：是否在表格上展示 2：在工具栏上显示（未实现）
    /// </summary>
    protected int toBar;
    /// <summary>
    /// 批量执行
    /// </summary>
    protected boolean multi;

    private String _moduleId;

    @JsonIgnore
    // ModuleId属性
    @JsonProperty("ModuleId")
    public String getModuleId() {
        switch (get_dllname()) {
            case "pubadd":
            case "pubadd2":
            case "pubadd3":
            case "tplbatchadd":
            case "tpladd":
            case "wordadd":
            case "batchadd":
            case "tableadd":
            case "tabadd":
            case "machine":
            case "machine2":
            case "machine3":
            case "pubpagedetail":
            case "pubbillinfo":
            case "pubchart":
            case "pubmoduletd":
            default:
                return NativeExtensionUtils.isNullOrEmpty(dllpar1) ? _moduleId : dllpar1;
            case "pubmodule":
            case "report":
            case "baseinfo":
            case "pubbill":
            case "pubmoduledetailinfo":
            case "pubmoduledetail":
                return NativeExtensionUtils.isNullOrEmpty(dllpar2) ? dllpar1 : dllpar2;
            case "photoview":
                return dllpar4;
            case "pubattachment":
                return dllpar3;
            case "pubaccraditation":
            case "baseaccraditation":
                return dllpar2;
        }
    }

    public void setModuleId(String moduleId) {
        this._moduleId = moduleId;
    }

    private int _serverId;

    @JsonIgnore
    @JsonProperty("ServerId")
    public int getServerId() {
        switch (get_dllname()) {
            case "pubadd":
            case "pubadd2":
            case "pubadd3":
            case "tplbatchadd":
            case "tpladd":
            case "wordadd":
            case "batchadd":
            case "tableadd":
            case "machine":
            case "machine2":
            case "machine3":
            case "pubpagedetail":
            case "pubbillinfo":
            case "pubchart":
            case "pubmoduletd":
                return DataTableUtil.getIntValue(dllpar7, 0);
            case "pubmodule":
            case "report":
            case "baseinfo":
            case "pubbill":
            case "pubmoduledetailinfo":
            case "pubmoduledetail":
                return DataTableUtil.getIntValue(dllpar3, 0);
            case "pubaccraditation":
            case "baseaccraditation":
                return DataTableUtil.getIntValue(dllpar1, 0);
            default:
                return 0;
        }
    }

    public void setServerId(int serverId) {
        this._serverId = serverId;
    }

    @JsonProperty("Updateable")
    protected boolean isUpdateable() {
        switch (get_dllname()) {
            case "pubadd":
            case "pubadd2":
            case "pubadd3":
            case "tplbatchadd":
            case "tpladd":
            case "wordadd":
            case "batchadd":
            case "tableadd":
            case "tabadd":
            case "machine":
            case "machine2":
            case "machine3":
            case "photoview":
                return !DataTableUtil.getBooleanValue(dllpar2, false);
            case "baseinfo":
                return !DataTableUtil.getBooleanValue(dllpar1, false);
            default:
                return true;
        }
    }

    @JsonIgnore
    // IdValue属性
    @JsonProperty("IdValue")
    public String getIdValue() {
        switch (get_dllname()) {
            case "pubadd":
            case "pubadd2":
            case "pubadd3":
            case "tplbatchadd":
            case "tpladd":
            case "wordadd":
            case "batchadd":
            case "tableadd":
            case "tabadd":
            case "machine":
            case "machine2":
            case "machine3":
                return dllpar3;
            case "pubbillinfo":
                return dllpar2;
            case "pubbill":
                return dllpar10;
            case "photoview":
                return dllpar1;
            case "windowwordcard":
            case "windowcard":
            case "windowcuscard":
            case "pubaccraditation":
            case "baseaccraditation":
                return dllpar4;
            case "baseinfo":
                return dllpar3;
            default:
                return dllpar2;
        }
    }


    @JsonIgnore
    // UnionCond属性
    @JsonProperty("UnionCond")
    public String getUnionCond() {
        switch (get_dllname()) {
            case "pubpagedetail":
            case "pubmodule":
            case "pubmoduledetailinfo":
                return dllpar7;
            default:
                return "";
        }
    }

    @JsonIgnore
    // UnionValue属性
    @JsonProperty("UnionValue")
    public String getUnionValue() {
        switch (get_dllname()) {
            case "pubpagedetail":
                return dllpar2;
            case "pubbillinfo":
                return dllpar2;
            case "photoview":
            case "pubattachment":
                return dllpar1;
            case "baseinfo":
                return dllpar3;
            case "pubmodule":
            case "pubadd":
            case "pubadd2":
            case "pubadd3":
            case "tplbatchadd":
            case "tpladd":
            case "wordadd":
            case "batchadd":
            case "tableadd":
            case "tabadd":
            case "pubmoduledetailinfo":
                return dllpar5;
            default:
                return "";
        }
    }

    @JsonIgnore
    // UnionField属性
    @JsonProperty("UnionField")
    public String getUnionField() {
        switch (get_dllname()) {
            case "pubmodule":
            case "pubadd":
            case "pubadd2":
            case "pubadd3":
            case "tplbatchadd":
            case "tpladd":
            case "wordadd":
            case "batchadd":
            case "tableadd":
            case "tabadd":
            case "pubmoduledetailinfo":
                return dllpar4;
            default:
                return "";
        }
    }

    @JsonIgnore
    // PmStr属性
    @JsonProperty("PmStr")
    public String getPmStr() {
        if ("pubmoduledetailinfo".equals(get_dllname())) {
            return dllpar3;
        }
        if ("tplbatchadd".equals(get_dllname())) return dllpar6;
        if (getIdValue() != null && !getIdValue().isEmpty()) return "";
        return dllpar6;
    }

    // Pms属性
    @JsonIgnore
    @JsonProperty("Pms")
    public Map<String, Object> Pms;

    // PmOffset属性
    @JsonProperty("PmOffset")
    public int getPmOffset() {
        if (get_dllname() == null || get_dllname().isEmpty()) return 0;
        switch (get_dllname()) {
            case "report":
                return 3;
            case "pubbill":
                return 2;
            default:
                return 3;
        }
    }

    private Map<String, Object> _popPms;

    @JsonIgnore
    // PopPms属性
    @JsonProperty("PopPms")
    public Map<String, Object> getPopPms() {
        if (_popPms != null) return _popPms;

        Map<String, Object> _retTab = new HashMap<>();
        if (Pms != null) {
            _retTab.putAll(Pms);
        }
        _retTab.put("p_1", dllpar1);
        _retTab.put("p_2", dllpar2);
        _retTab.put("p_3", dllpar3);
        _retTab.put("p_4", dllpar4);
        _retTab.put("p_5", dllpar5);
        _retTab.put("p_6", dllpar6);
        _retTab.put("p_7", dllpar7);
        _retTab.put("p_8", dllpar8);
        _retTab.put("p_9", dllpar9);
        _retTab.put("p_10", dllpar10);

        Map<String, Object> retTab = new HashMap<>();
        for (Map.Entry<String, Object> entry : _retTab.entrySet()) {
            if (entry.getValue() != null && !entry.getValue().toString().isEmpty()) {
                try {
                    int i = Integer.parseInt(entry.getKey().substring(2));
                    if (i == 6) {
                        switch (get_dllname()) {
                            case "pubadd":
                            case "pubadd2":
                            case "pubadd3":
                            case "tplbatchadd":
                            case "tpladd":
                                break;
                            default:
                                retTab.put("p_" + (i - getPmOffset()), entry.getValue());
                                break;
                        }
                    } else {
                        retTab.put("p_" + (i - getPmOffset()), entry.getValue());
                    }
                } catch (NumberFormatException e) {
                    // 忽略非数字键
                }
            }
        }

        _popPms = retTab;
        return retTab;
    }
}