package com.bohuikeji.frame.module.admin.entity.configure;

import com.baomidou.mybatisplus.annotation.TableId;
import com.bohuikeji.frame.module.admin.annotation.TableKeyField;
import com.bohuikeji.frame.module.admin.annotation.TableNewField;
import com.bohuikeji.frame.module.admin.annotation.TableQueryField;
import com.bohuikeji.frame.module.admin.annotation.TableShowField;
import com.bohuikeji.frame.module.admin.base.*;
import com.bohuikeji.frame.module.admin.comparator.ConfComponent;
import com.bohuikeji.frame.module.admin.entity.web.TableColumn;
import com.bohuikeji.frame.module.admin.entity.web.TableInit;
import com.bohuikeji.frame.module.admin.entity.web.TableModel;
import com.bohuikeji.frame.module.admin.entity.web.TableSearsh;
import com.bohuikeji.frame.module.admin.entity.web.block.ButtonBlock;
import com.bohuikeji.frame.module.admin.entity.web.template.TableTemplate;
import com.bohuikeji.frame.module.admin.enums.ColumnTypeEnum;
import com.bohuikeji.frame.module.admin.utils.ReflectUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 页面配置
 * @author 10552
 */
@Data
@Slf4j
public class PageConf extends SuperBase {
    /**
     * 字段集合
     */
    List<FieldConf> fieldConfs;
    /**
     * 数据库表名称
     */
    String tableName;
    /**
     * 对应实体类名称
     */
    String className;

    /**
     * 生成配置文件方法
     * @param cls
     * @return
     */
    public static PageConf getPageConf(Class<? extends EntityBase> cls){
        PageConf page=new PageConf();
        Field[] fields=cls.getDeclaredFields();
        List<FieldConf> fieldConfList=new ArrayList<>(fields.length);
        for(Field field:fields){
            TableShowField showField=field.getAnnotation(TableShowField.class);
            TableNewField newField=field.getAnnotation(TableNewField.class);
            TableQueryField queryField=field.getAnnotation(TableQueryField.class);
            TableKeyField keyField=field.getAnnotation(TableKeyField.class);
            TableId tableId=field.getAnnotation(TableId.class);
            if(showField==null){
                continue;
            }
            FieldConf conf=FieldConf.getInstance(showField);
            conf.setFieldName(field.getName());
            conf.setColumnName(ReflectUtil.getColumnName(field));
            conf.setFieldType(field.getType().getName());
            conf.setTableName(ReflectUtil.getTableName(cls));
            EditAn editAn=EditAn.getInstance(newField);
            NewAn newAn=NewAn.getInstance(newField);
            QueryAn queryAn=QueryAn.getInstance(queryField);
            KeyAn keyAn=KeyAn.getInstance(keyField);
            TableAn tableAn=TableAn.getInstance(showField);
            conf.setEdit(editAn);
            conf.setNewed(newAn);
            conf.setQuery(queryAn);
            conf.setTable(tableAn);
            conf.setKey(keyAn);
            if(tableId!=null){
                conf.setId(true);
                conf.setColumnName(tableId.value());
            }
            fieldConfList.add(conf);
        }
        page.setFieldConfs(fieldConfList);
        page.setClassName(cls.getName());
        page.setTableName(ReflectUtil.getTableName(cls));
        return page;
    }

    /**
     * 创建页面模板对象
     * @return
     */
    public PageTemplateBase createTemplate(){
        //创建页面模板对象
        TableTemplate page=new TableTemplate();
        //创建默认按钮组
        page.setBatchButton(ButtonBlock.createButtons());
        //表头
        List<String> heads=new ArrayList<>(this.fieldConfs.size());
        List<HtmlBlockFormBase>queryBlocks=new ArrayList<>(fieldConfs.size());
        List<HtmlBlockFormBase>newBlocks=new ArrayList<>(fieldConfs.size());
        List<HtmlBlockFormBase>editBlocks=new ArrayList<>(fieldConfs.size());
        List<TableSearsh> searshList=new ArrayList<>(fieldConfs.size());
        for (FieldConf field:fieldConfs){
            TableAn tableAn=field.getTable();
            if (tableAn==null){
                continue;
            }
            TableColumn column = TableColumn.getDefalut(field.fieldName,field.getFileTitle());
            ColumnTypeEnum columnType=field.getTable().getDataType();
            //自动识别特殊格式
            if (columnType== ColumnTypeEnum.AUTO){
                switch (field.getFieldType()){
                    case "com.bohuikeji.frame.module.admin.entity.vo.AddressVo":
                        column.setDataFormatter("formatter_address");break;
                    case "com.bohuikeji.frame.module.admin.entity.vo.FileVo":
                        column.setDataFormatter("formatter_file");break;
                    default:break;
                }
                if(field.getKey()!=null){
                    column.setDataFormatter("formatter_key");
                }
            }
            else{
                column.setDataFormatter("formatter_"+columnType);
            }
            if(tableAn.isShow()){
                heads.add(column.forParamStr());
            }
            if(!field.isId()){
                if(field.getQuery()!=null){
                    if(field.getQuery().isSearch()){
                        searshList.add(new TableSearsh(field.getColumnName(),field.getFileTitle()));
                    }
                    if(field.getQuery().isShow()){
                        queryBlocks.add(field.getHtmlBlock(field.getQuery()));
                    }
                }
                if(field.getNewed()!=null){
                    newBlocks.add(field.getHtmlBlock(field.getNewed()));
                }
                if(field.getEdit()!=null){
                    editBlocks.add(field.getHtmlBlock(field.getEdit()));
                }
            }
            else {
                editBlocks.add(field.getHtmlBlock(field.getEdit()));
            }
        }
        //组件位置排序
        ConfComponent component=new ConfComponent();
        queryBlocks.sort(component);
        newBlocks.sort(component);
        editBlocks.sort(component);
        //填充组件
        page.setTableHeads(heads);
        TableInit tableInit=TableInit.getDefaultStyle();
        tableInit.setDataIdField("id");
        page.setTableInit(tableInit.forParamStr());
        page.setTableQuery(queryBlocks);
        TableModel tableModel = new TableModel();
        tableModel.setAddBlocks(newBlocks);
        tableModel.setInputBlocks(editBlocks);
        page.setTableModel(tableModel);
        page.setTableSearch(searshList);
        return page;
    }

}
