package org.example.Entity.System;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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.JSON;

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

import static org.example.Utils.DataTableUtil.get;
import static org.example.Utils.NativeExtensionUtils.toBoolean;

public class ModuleEntity {
    private static final Logger log = LoggerFactory.getLogger(ModuleEntity.class);


    @JsonIgnore
    public Map<String, Object> basemodule = new HashMap<>();

    public ModuleEntity() {

    }

    public ModuleEntity(Map<String, Object> basemodule) {
        this.basemodule = basemodule;
    }

    protected Map<String, Object> getBasemodule() {
        return basemodule;
    }

    private String MasterTable; // 私有字段

    @JsonProperty("MasterTable")
    public String getMasterTable() {
        return MasterTable;
    }

    public void setMasterTable(String value) {
        this.MasterTable = value;
    }

    private String _fromkey;

    @JsonIgnore
    @JsonProperty("Fromkey")
    public String getFromkey() {
        Object _fk = basemodule.getOrDefault("fromkey", _fromkey);
        if (_fk != null && !(_fk.toString().isEmpty())) {
            return (_fromkey = _fk.toString());
        }
        return null;
    }

    public void setFromkey(String value) {
        this._fromkey = value;
    }

    private String _menuName;

    @JsonInclude(JsonInclude.Include.NON_NULL)

    @JsonProperty("MenuName")
    public String getMenuName() {
        // 先尝试获取 "menuname"，如果不存在则获取 "menucaption"，最后使用 _menuName 作为默认值
        Object value = basemodule.getOrDefault("menuname",
                basemodule.getOrDefault("menucaption", _menuName));

        if (value != null && !value.toString().isEmpty()) {
            return (_menuName = value.toString());
        }
        return null;
    }

    public void setMenuName(String value) {
        this._menuName = value;
    }

    private String _toolsName;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("ToolsName")
    public String getToolsName() {
        if (_toolsName == null) {
            // 2. 替换为和 C# 一致的取值对象（_row 等效对象，比如 basemodule 是 Map 的话）
            Object toolsNameObj = basemodule.getOrDefault("ToolsName", _toolsName); // 替代 C# 的 _row.Get(...)
            // 3. 安全转字符串（避免 NPE，对齐 C# 的 as string）
            _toolsName = toolsNameObj != null ? toolsNameObj.toString() : null;
        }

        // 4. 对齐 C# 的相等判断逻辑
        if (Objects.equals(_toolsName, getMenuName())) {
            return null;
        }

        // 5. 返回最终值
        return _toolsName;
    }

    private String _moduleId;

    @JsonProperty("ModuleId")
    public String getModuleId() {
        Object value = get(basemodule, "MenuCode", _moduleId);
        if (value != null && !value.toString().isEmpty()) {
            return (_moduleId = value.toString());
        }
        return null;
    }

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

    private String _menuPrefix;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("MenuPrefix")
    public String getMenuPrefix() {
        String prefix = (String) get(basemodule, "MenuPrefix", _menuPrefix, null);
        if (prefix != null && !prefix.isEmpty()) {
            _menuPrefix = prefix.toLowerCase();
        }

        return _menuPrefix == null ? "" : _menuPrefix;
    }

    public void setMenuPrefix(String value) {
        this._menuPrefix = value;
    }

    private String _menuId;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty("MenuId")
    public String getMenuId() {
        Object value = basemodule.getOrDefault("MenuId", _menuId);
        if (value != null && !value.toString().isEmpty()) {
            return (_menuId = value.toString());
        }
        return null;
    }

    public void setMenuId(String value) {
        this._menuId = value;
    }

    private String IdField;

    @JsonProperty("IdField")
    public String getIdField() {
        return IdField;
    }

    public void setIdField(String idField) {
        IdField = idField;
    }

    @JsonIgnore
    public String IdValue;

    @JsonIgnore
    public String IdentityId;

    @JsonIgnore
    public Map<String, Object> Updrow;

    private Map<String, Object> _leftRecord;

    @JsonIgnore
    @JsonProperty("LeftRecord")
    public Map<String, Object> getLeftRecord() {
        if (_leftRecord == null && SLeftRecord != null && !SLeftRecord.isEmpty()) {
            try {
                _leftRecord = (Hashtable) JSON.Decode(SLeftRecord);
            } catch (Exception e) {
                // 处理 JSON 解析异常
                log.error("Exception caught", e);
            }
        }
        return _leftRecord;
    }

    public void setLeftRecord(Map<String, Object> value) {
        this._leftRecord = value;
    }

    @JsonIgnore
    public Map<String, Object> PopPms;

    @JsonIgnore
    public String SLeftRecord;

    @JsonIgnore
    public ModuleEntity ParentModule;

    @JsonIgnore
    public BaseDetailModule DetailModule;

    @JsonIgnore
    private Boolean IsAdd;

    @JsonIgnore
    @JsonProperty("IsAdd")
    public boolean getIsAdd() {
        return IdValue == null || IdValue.trim().isEmpty();
    }

    private Boolean _noGridLine = null;

    @JsonIgnore
    public Boolean NoGridLine;

    @JsonIgnore
    @JsonProperty("NoGridLine")
    public Boolean getNoGridLine() {
        if (_noGridLine == null) {
            _noGridLine = toBoolean(get(basemodule, "noGridLine", null));
        }
        return _noGridLine != null && _noGridLine ? Boolean.TRUE : null;
    }

    public void setNoGridLine(Boolean value) {
        this._noGridLine = value;
    }

    private Boolean _hideColumnHeader = null;

    @JsonIgnore
    public Boolean HideColumnHeader;

    @JsonIgnore
    @JsonProperty("HideColumnHeader")
    public Boolean getHideColumnHeader() {
        if (_hideColumnHeader == null) {
            _hideColumnHeader = toBoolean(get(basemodule, "hideColumnHeader", null));
        }
        return _hideColumnHeader != null && _hideColumnHeader ? Boolean.TRUE : null;
    }

    public void setHideColumnHeader(Boolean value) {
        this._hideColumnHeader = value;
    }

    private Boolean _noRownumber = null;

    @JsonIgnore
    @JsonProperty("NoRownumber")
    public Boolean getNoRownumber() {
        if (_noRownumber == null) {
            _noRownumber = toBoolean(get(basemodule, "noRownumber", null));
        }
        return _noRownumber != null && _noRownumber ? true : null;
    }

    public void setNoRownumber(Boolean value) {
        this._noRownumber = value;
    }
}
