package org.example.Entity.Control.Base;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.example.Utils.IPublicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * ==============================================================================
 * 功能描述：Component 所有Ext组件的基类
 * ==============================================================================
 */
@org.springframework.stereotype.Component
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Component extends Base {
    @JsonIgnore
    private IPublicUtil _util;

    // 自动注入JdbcTemplate（由Spring容器提供，已关联DataSource）
    @JsonIgnore
    @Autowired
    protected JdbcTemplate jdbcTemplate;

    @JsonIgnore
    protected IPublicUtil getUtil() {
        if (_util == null) {
            // 若IPublicUtil需要JdbcTemplate，通过构造函数传入已注入的实例
            _util = new IPublicUtil(jdbcTemplate);
        }
        return _util;
    }

    public void setUtil(IPublicUtil _util) {
        this._util = _util;
    }

    private JdbcTemplate _dbOperator;

    @JsonIgnore
    protected JdbcTemplate getDbOperator() {
        if (_dbOperator == null) {
            if (getUtil() != null) {
                _dbOperator = _util.GetOperater();
            }
        }
        return _dbOperator;
    }

    public void setDbOperator(JdbcTemplate value) {
        this._dbOperator = value;
    }

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

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


    protected Boolean hidden;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Boolean getHidden() {
        return this.hidden;
    }

    public void setHidden(Boolean hidden) {
        this.hidden = hidden;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Boolean disabled;


    protected Integer top;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Integer getTop() {
        return top;
    }

    public void setTop(Integer top) {
        this.top = top;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Integer getLeft() {
        return left;
    }

    public void setLeft(Integer left) {
        this.left = left;
    }

    protected Integer left;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public List<Component> items;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Object defaults;

    // 添加子组件
    public void Add(Component com) {
        if (this.items == null) {
            this.items = new ArrayList<Component>();
        }
        this.items.add(com);
    }

    // 比较组件位置（替代C#的操作符重载）
    public static boolean isGreater(Component left, Component right) {
        final int err = 5; // +-5px误差
        // 处理left.top或right.top为null的情况（例如默认值设为0）
        int leftTop = (left.getTop() != null) ? left.getTop() : 0;
        int rightTop = (right.getTop() != null) ? right.getTop() : 0;

        if (leftTop < rightTop - err) {
            return true;
        } else if (leftTop >= rightTop - err && leftTop <= rightTop + err) {
            // 若top接近，比较left属性（同样处理null）
            int leftLeft = (left.getLeft() != null) ? left.getLeft() : 0;
            int rightLeft = (right.getLeft() != null) ? right.getLeft() : 0;
            return leftLeft < rightLeft;
        } else {
            return false;
        }
    }

    public static boolean isLess(Component left, Component right) {
        return !isGreater(left, right);
    }
}
