package com.bohuikeji.frame.module.admin.base;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.bohuikeji.frame.module.admin.annotation.TableShowField;
import com.bohuikeji.frame.module.admin.annotation.TableNewField;
import com.bohuikeji.frame.module.admin.entity.configure.BaseInputAn;
import com.bohuikeji.frame.module.admin.entity.configure.FieldConf;
import com.bohuikeji.frame.module.admin.entity.configure.PageConf;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * Html组件父类
 * 【注意】请勿修改此类，新增组件请继承
 * @author 10552
 */
@Slf4j
@Data
public abstract class HtmlBlockBase<T> extends SuperBase{
    public HtmlBlockBase(){
        log.warn("组件初始化默认构造方法被调用");
    }
    /**
     * 自增序列
     * 用于保证同一页面创建的组件id不会重复
     */
    private static int index=0;
    /**
     * 组件id
     */
    protected String id;
    /**
     * 组件name属性
     */
    private String name;
    /**
     * 组件数据库对应列名称
     */
    private String columnName;
    /**
     * 组件中文标题
     */
    protected String title;
    /**
     * 图标
     */
    protected String icon;
    /**
     * 组件类型，用于识别子类对象
     */
    protected String blockType;

    /**
     * 设置禁用
     */
    protected String disabled="";
    /**
     * 设置显示
     */
    protected String show="inline";

    /**
     * 是否必填
     */
    protected boolean required;
    /**
     * 摆放排序
     */
    protected double sort=1d;

    /*private HtmlBlockBase(){
    }*/

    /**
     * 初始化组件默认属性
     * @param field
     */
    public HtmlBlockBase(Field field){
        if (field==null){
            this.blockType=this.getClass().getSimpleName();
            this.disabled="";
            /*log.warn("【警告】组件初始化错误，Field为空，部分属性无法完成初始化，位于：[{}]，调用者：[{}]",this.getClass(),this);*/
            return;
        }
        //一页最多9999个组件，超过重新计数，为了保证StringBuilder性能，控制自增长度发生避免扩容
        if (index>10000){
            index=0;
        }
        StringBuilder idStr=new StringBuilder(20);
        idStr.append("id_");
        idStr.append(index++);
        idStr.append("_");
        idStr.append(field.getName());
        this.id=idStr.toString();
        this.name=field.getName();
        TableShowField tableEntityField=field.getAnnotation(TableShowField.class);
        TableNewField tableNewField=field.getAnnotation(TableNewField.class);
        if (tableEntityField==null){
            log.error("【错误】无法初始化HTML组件[{}]，位于[{}]，请为该字段配置注解[{}]",this.getClass(),field.getDeclaringClass()+"."+field.getName(), TableShowField.class);
            throw new RuntimeException("配置错误，Html组件初始化失败，字段：["+field.getName()+"]缺少注解：@TableShowField");
        }
        if (tableEntityField.id()){
            this.columnName=field.getAnnotation(TableId.class).value();
        }
        else {
            Annotation tableField=field.getAnnotation(TableField.class);
            if (tableField==null){
                this.columnName=StringUtils.toUnderline(field.getName());
            }else {
                this.columnName=field.getAnnotation(TableField.class).value();
            }
        }
        this.title=field.getAnnotation(TableShowField.class).value();
        this.blockType=this.getClass().getSimpleName();
        this.disabled="";
        if (tableNewField==null){
            return;
        }
        if (!tableNewField.edit()){
            this.disabled="disabled";
        }
        if (!tableNewField.show()){
            this.show="none";
        }
        required=tableNewField.required();
    }
    public HtmlBlockBase(Field field,EntityBase entity){
        this(field);
    }

    public HtmlBlockBase(FieldConf fieldConf, BaseInputAn inputAn){
        Class blockClass;
        try {
            blockClass=Class.forName(inputAn.getBlock());
        } catch (ClassNotFoundException e) {
            log.error("组件创建失败：位于：[{}]-[{}]",fieldConf.getTableName());
            throw new RuntimeException("组件创建失败！");
        }
        if (index>10000){
            index=0;
        }
        StringBuilder idStr=new StringBuilder(20);
        idStr.append("id_");
        idStr.append(index++);
        idStr.append("_");
        idStr.append(fieldConf.getFieldName());
        this.id=idStr.toString();
        this.name=fieldConf.getFieldName();
        this.columnName=fieldConf.getColumnName();
        this.title=fieldConf.getFileTitle();
        this.blockType=blockClass.getSimpleName();
        this.disabled=inputAn.isDisable()?"":"disabled";
        this.show=inputAn.isShow()?"inline":"none";
        this.required=inputAn.isRequired();
        this.sort=inputAn.getSort();

    }

    /**
     * 使用对象设置属性默认值默认值
     * @return
     */
    //public abstract T updateBlock(EntityBase entity);
    //public abstract T construct(Field field, EntityBase model);

    /*public static HtmlBlockBase getEmpty(){
        return HtmlBlockBase();
    }*/

    public String getTitle() {
        return title;
    }

    public T setTitle(String title) {
        this.title = title;
        return (T)this;
    }

    public String getIcon() {
        return icon;
    }

    public T setIcon(String icon) {
        this.icon = icon;
        return (T)this;
    }

    public String getBlockType() {
        return blockType;
    }

    public T setBlockType(String blockType) {
        this.blockType = blockType;
        return (T)this;
    }

    public String getId() {
        return id;
    }

    public T setId(String id) {
        this.id = id;
        return (T)this;
    }

    public String getName() {
        return name;
    }

    public T setName(String name) {
        this.name = name;
        return (T)this;
    }

    public String getColumnName() {
        return columnName;
    }

    public T setColumnName(String columnName) {
        this.columnName = columnName;
        return (T)this;
    }

    public String getDisabled() {
        return disabled;
    }

    public T setDisabled(String disabled) {
        this.disabled = disabled;
        return (T)this;
    }

    public String getShow() {
        return show;
    }

    public HtmlBlockBase<T> setShow(String show) {
        this.show = show;
        return this;
    }

    public boolean isRequired() {
        return required;
    }

    public HtmlBlockBase<T> setRequired(boolean required) {
        this.required = required;
        return this;
    }
}
