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.Entity.Control.Com.SysPoPupMenuBtn;
import org.example.Utils.DataTableUtil;

import java.util.ArrayList;
import java.util.Map;


import java.util.Optional;

import static org.example.Utils.DataTableUtil.get;
import static org.example.Utils.NativeExtensionUtils.parseInt;
import static org.example.Utils.NativeExtensionUtils.toBoolean;
import static org.example.Utils.PublicUtil.EvalCond;
import static org.example.Utils.PublicUtil.SqlToCode;

public class ModuleBaseEntity extends ModuleEntity {

    public ModuleBaseEntity() {
        super();
    }

    public ModuleBaseEntity(Map<String, Object> basemodule) {
        super(basemodule);
    }

    public Boolean OperAble = true;

    private Boolean _applyFlag;

    @JsonProperty("ApplyAble")
    public Boolean getApplyAble() {
        return toBoolean(DataTableUtil.getRowVal(basemodule, "SaveApplyFlag", _applyFlag != null ? _applyFlag : true));
    }

    public void setApplyAble(Boolean value) {
        this._applyFlag = value;
    }

    private Boolean _backSelected;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("BackSelected")
    public Boolean getBackSelected() {
        _backSelected = toBoolean(basemodule.getOrDefault("BackSelected", _backSelected));
        return _backSelected != null && _backSelected ? _backSelected : null;
    }

    public void setBackSelected(Boolean value) {
        this._backSelected = value;
    }

    private Boolean _muitlAudit;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("MuitlAudit")
    public Boolean getMuitlAudit() {
        _muitlAudit = toBoolean(get(basemodule, "MuitlAudit", _muitlAudit));
        return _muitlAudit != null && _muitlAudit ? _muitlAudit : null;
    }

    public void setMuitlAudit(Boolean value) {
        this._muitlAudit = value;
    }

    private Boolean _NoFastAudit;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("NoFastAudit")
    public Boolean getNoFastAudit() {
        _NoFastAudit =  toBoolean(get(basemodule, "disAppFastAudit", _NoFastAudit,null));
        return (Boolean) _NoFastAudit ? _NoFastAudit : null;
    }

    public void setNoFastAudit(Boolean value) {
        this._NoFastAudit = value;
    }

    private String _taskSql;

    @JsonIgnore
    @JsonProperty("TaskSql")
    public String getTaskSql() {
        return (String) basemodule.getOrDefault("taskSql", _taskSql);
    }

    public void setTaskSql(String value) {
        this._taskSql = value;
    }

    private String _countSql;

    @JsonIgnore
    @JsonProperty("CountSql")
    public String getCountSql() {
        Object value = basemodule.get("countSql");
        return value != null ? (String) value : _countSql;
    }

    public void setCountSql(String value) {
        this._countSql = value;
    }

    @JsonProperty("NeedCount")
    public boolean getNeedCount() {
        return getCountSql() != null && !getCountSql().isEmpty();
    }

    @JsonIgnore
    public Boolean CondSelect = true;

    private String _overBackCond;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("OverBackCond")
    public String getOverBackCond() {
        return (String) basemodule.getOrDefault("OverBackCond", _overBackCond);
    }

    public void setOverBackCond(String value) {
        this._overBackCond = value;
    }

    private String _attatchModifyCond;

    @JsonIgnore
    @JsonProperty("AttatchModifyCond")
    public String getAttatchModifyCond() {
        return (String) basemodule.getOrDefault("attatchModifyCond", _attatchModifyCond);
    }

    public void setAttatchModifyCond(String value) {
        this._attatchModifyCond = value;
    }

    private String _overBackSql;

    @JsonIgnore
    @JsonProperty("OverBackSql")
    public String getOverBackSql() {
        return (String) get(basemodule, "OverBackSql", _overBackSql, null);
    }

    public void setOverBackSql(String value) {
        this._overBackSql = value;
    }

    private String _overBackKey;

    @JsonIgnore
    @JsonProperty("OverBackKey")
    public String getOverBackKey() {
        Object value = get(basemodule, "OverBackKey", _overBackKey);
        return value != null ? value.toString() : "";
    }

    public void setOverBackKey(String value) {
        this._overBackKey = value;
    }

    private String _overBackOper;

    @JsonIgnore
    @JsonProperty("OverBackOper")
    public String getOverBackOper() {
        return (String) basemodule.getOrDefault("OverBackOper", _overBackOper);
    }

    public void setOverBackOper(String value) {
        this._overBackOper = value;
    }

    private Boolean _importFlag;

    @JsonProperty("ImportAble")
    public Boolean getImportAble() {
        if (!OperAble) return false;
        return _importFlag != null ? _importFlag : toBoolean(basemodule.getOrDefault("ImportFlag", true));
    }

    public void setImportAble(Boolean value) {
        this._importFlag = value;
    }

    private String _importCond;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("ImportCond")
    public String getImportCond() {
        return (String) basemodule.getOrDefault("ImportCond", _importCond);
    }

    public void setImportCond(String value) {
        this._importCond = value;
    }

    private String _exportCond;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("ExportCond")
    public String getExportCond() {
        return (String) get(basemodule, "ExportCond", _exportCond, null);
    }

    public void setExportCond(String value) {
        this._exportCond = value;
    }

    private Boolean _exportFlag;

    @JsonProperty("ExportAble")
    public Boolean getExportAble() {
        if (!OperAble) return false;
        Boolean exportAble = _exportFlag != null ? _exportFlag : toBoolean(basemodule.getOrDefault("ExportFlag", true));

        if (!exportAble) return false;
        if (getExportCond() == null || getExportCond().trim().isEmpty()) {
            return exportAble;
        } else if (!getExportCond().contains("{")) {
            exportAble = toBoolean(EvalCond(SqlToCode(getExportCond()), null, true));
        }

        _exportFlag = exportAble;
        return _exportFlag;
    }

    public void setExportAble(Boolean value) {
        this._exportFlag = value;
    }


    @JsonIgnore
    @JsonProperty("ModuleName")
    public String getModuleName() {
        boolean[] isUrl = new boolean[1];
        return SystemMenu.convertToModuleName((String) basemodule.getOrDefault("dllfilename", null), 0, isUrl);
    }


    @JsonIgnore
    @JsonProperty("AddDllName")
    public String getAddDllName() {
        return (String) basemodule.getOrDefault("adddllname", null);
    }

    private String _addXtype;


    @JsonProperty("AddXtype")
    public String getAddXtype() {
        if (_addXtype == null || _addXtype.isEmpty()) {
            _addXtype = (String) basemodule.getOrDefault("adddllname", null);
            if (_addXtype != null && !_addXtype.isEmpty()) {
                _addXtype = _addXtype.trim();
                // 这里需要实现 SystemMenu.ConvertToModuleName 对应的 Java 逻辑
                boolean[] isUrl = new boolean[1];
                _addXtype = SystemMenu.convertToModuleName(_addXtype, 0, isUrl);
            }
            return _addXtype;
        }
        return _addXtype;
    }

    @JsonIgnore
    public String ContextMenuId;

    @JsonIgnore
    public SysPoPupMenuBtn PopBtn;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Integer BillType;

    private String _dirId;

    @JsonProperty("DirId")
    public String getDirId() {
//        return (String) DataTableUtil.get(basemodule, "dirid", _dirId, null);
        return Optional.ofNullable(get(basemodule, "dirid", _dirId, null))
                .map(Object::toString)
                .orElse(null);
    }

    public void setDirId(String value) {
        this._dirId = value;
    }

    private Integer _editType;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("EditType")
    public Integer getEditType() {
        return (Integer) basemodule.getOrDefault("editType", _editType);
    }

    public void setEditType(Integer value) {
        this._editType = value;
    }

    private Boolean _cellTplflag;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("CellTplflag")
    public Boolean getCellTplflag() {
        Boolean flag = toBoolean(basemodule.getOrDefault("cellTplflag", _cellTplflag));
        return flag != null && flag ? flag : null;
    }

    public void setCellTplflag(Boolean value) {
        this._cellTplflag = value;
    }

    private boolean _cardTplflag;

    //  2026.2.24新增
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("WebCardTpl")
    public Boolean getWebCardTpl() {
        boolean flag = toBoolean(basemodule.getOrDefault("WebCardflag", _cardTplflag));
        if (flag) return true;
        return false;
    }

    public void setWebCardTpl(Boolean value) {
        this._cardTplflag = value;
    }

    private String _speciesNo;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("FileSpeciesNo")
    public String getFileSpeciesNo() {
        return get(basemodule, "FileSpeciesNo", _speciesNo) != null ? (String) get(basemodule, "FileSpeciesNo", _speciesNo) : "";
    }

    public void setFileSpeciesNo(String value) {
        this._speciesNo = value;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("AttcViewType")
    public Integer AttcViewType;

    private String _ftype;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("FType")
    public String getFType() {
        _ftype = _ftype != null ? _ftype : (String) basemodule.getOrDefault("ftype", "");
        return _ftype != null && !_ftype.isEmpty() ? _ftype : null;
    }

    public void setFType(String value) {
        this._ftype = value;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("CusAddTpl")
    public String CusAddTpl;

    private String _addHint;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("AddHint")
    public String getAddHint() {
        Object value = get(basemodule, "addhint", _speciesNo, null);
        return value != null ? value.toString() : "";
    }

    public void setAddHint(String value) {
        this._addHint = value;
    }

    private Integer _rowHeight;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("RowHeight")
    public Integer getRowHeight() {
        if (_rowHeight == null || _rowHeight == 0) {
            Object h = basemodule.getOrDefault("rowHeight", _rowHeight);
            if (h != null) {
                _rowHeight = Integer.parseInt(h.toString());
            }
        }
        return _rowHeight;
    }

    public void setRowHeight(Integer value) {
        this._rowHeight = value;
    }

    private Integer _attcLeafOnly;


    @JsonProperty("AttcLeafOnly")
    public Integer getAttcLeafOnly() {
        return _attcLeafOnly != null ? _attcLeafOnly : parseInt(basemodule.getOrDefault("bmpSpecIsMJ", true).toString());
    }

    public void setAttcLeafOnly(Integer value) {
        this._attcLeafOnly = value;
    }

    private Integer _accInfoWidth;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("AccInfow")
    public Integer getAccInfow() {
        return (Integer) basemodule.getOrDefault("attInfow", _accInfoWidth);
    }

    public void setAccInfow(Integer value) {
        this._accInfoWidth = value;
    }
    @JsonProperty("defaultSearch")
    public Boolean getDefaultSearch() {
        return toBoolean(basemodule.getOrDefault("defaultSearch", true));
    }

    @JsonIgnore
    public Map<String, Object> MasterData;

    @JsonIgnore
    public ArrayList<Map<String, Object>> DetailData;


    @JsonIgnore
    @JsonProperty("NewVer")
    public Boolean getNewVer() {
        return toBoolean(basemodule.getOrDefault("newver", false));
    }


    @JsonIgnore
    @JsonProperty("NewWFVer")
    public Boolean getNewWFVer() {
        return toBoolean(basemodule.getOrDefault("newwfver", false));
    }

    private Boolean _addCopyFlag;

    @JsonIgnore
    public String getAttcField1(){
        return basemodule.getOrDefault("attcfield1",false)!=null?basemodule.getOrDefault("attcfield1",false).toString():"";
    }

    @JsonIgnore
    public String getAttcField2(){
        return basemodule.getOrDefault("attcfield2",false)!=null?basemodule.getOrDefault("attcfield2",false).toString():"";
    }

    @JsonIgnore
    public String getAttcField3(){
        return basemodule.getOrDefault("attcfield3",false)!=null?basemodule.getOrDefault("attcfield3",false).toString():"";
    }


    @JsonProperty("AddCopyAble")
    public Boolean getAddCopyAble() {
        return toBoolean(basemodule.getOrDefault("addCopy", _addCopyFlag != null ? _addCopyFlag : true));
    }

    public void setAddCopyAble(Boolean value) {
        this._addCopyFlag = value;
    }

    private String _desp;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("desp")
    public String getDesp() {
        Object value = basemodule.getOrDefault("desp", _desp);
        return value != null ? value.toString() : "";
    }

    public void setDesp(String value) {
        this._desp = value;
    }

    private String _noDataMsg;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("noDataMsg")
    public String getNoDataMsg() {
        return basemodule.getOrDefault("noDataMsg",null)!=null?basemodule.getOrDefault("noDataMsg",null).toString():"";
    }

    public void setNoDataMsg(String value) {
        this._noDataMsg = value;
    }


    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String StepApplyText;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String StepBackText;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Object rightMenus;
}