import { IMidwayApplication, ObjectContext } from "@midwayjs/core";
import { ButtonElementComponent, ComoFormParams, ComoTableColumn, ElementFormColumnGender, ElementTableColumnGender, ElementTagComponent } from "como-admin-web";
import { ComoTsUtils } from "como-ts-utils";
import { Model, Repository } from "sequelize-typescript";
import { AdminUserTokenInfo, CommonFormType, CommonPageGetFormType, GlobalOjectType, WebFunction, WebPageTableConfig } from "../interface";
import { COMO_TABLE_COLUMN_DECORATOR, GetPropertDecorator } from "./AppDecorators";

export abstract class BaseAppVue {
    /**
     * 项目信息
     */
    protected App:IMidwayApplication
    /**
     * 管理员信息
     */
    protected AdminUser:AdminUserTokenInfo
    /**
     * 当前使用的模型
     */
     protected _useModel:Repository<Model>;
    /**
     * 表单字段列实例
     */
    get FormFieldIns() {
        return ElementFormColumnGender.Ins;
    }
    /**
     * 生成按钮实例
     */
    get ButtonIns() {
        return ButtonElementComponent.Ins;
    }
    /**
     * 生成tag实例
     */
    get TagIns() {
        return ElementTagComponent.Ins;
    }
    /**
     * 生成表格列实例
     */
    get ColumnIns() {
        return ElementTableColumnGender.Ins;
    }
    /**
     * 构造函数
     * @param app
     */
    constructor(app:IMidwayApplication,UserInfo?:AdminUserTokenInfo) {
        this.App = app;
        this.AdminUser = UserInfo;
    }
    /**
     * 获取注入函数
     * @param fn
     */
    GetFunction<T extends GlobalOjectType>(fn:WebFunction<T>,data?:GlobalOjectType) {
        data = data ?? {};
        let msg = fn.toString();
        const regex = /({{)(.*?)(}})/g;
        let content = msg;
        const tmpArr = msg.match(regex);
        if(tmpArr && tmpArr.length > 0) {
            for(let i = 0;i < tmpArr.length;i++) {
                const item = tmpArr[i];
                const field = item.replace("{{","").replace("}}","");
                let value = data[field] ?? '';
                if(field.includes('-')) {
                    value = ComoTsUtils.Ins.ComoDecode(data[field.replace('-','')]);
                }
                content = content.replace(item,value || '');
            }
        }
        return content;
    }
    /**
     * 动态获取服务
     * @param identifier 
     * @param args 
     * @param objectContext 
     * @returns 
     */
    GetService<T>(identifier: new (...args: any[]) => T, args?: any[], objectContext?: ObjectContext) {
        return this.App.getApplicationContext().get(identifier);
    }
    /**
    * 获取列表配置
    */
    abstract GetTableConfig():Promise<WebPageTableConfig>;
    /**
     * 生成表单配置
     * @param data 
     */
    abstract GetFormConfig(data: CommonPageGetFormType): Promise<CommonFormType | ComoFormParams>;
    /**
    * 自动生成列
    */
    AutoColumns() {
        const CurClass = this['constructor'];
        const decorators = GetPropertDecorator(CurClass,this,COMO_TABLE_COLUMN_DECORATOR) as {
            key:string
            value:{
                column:any
                sort:number
            }
        }[];
        return decorators.sort((item,nextItem) => {
            return (item.value.sort - nextItem.value.sort);
        }).map(item => {
            const tmpValue = item.value.column as ComoTableColumn;
            if(!tmpValue.prop) tmpValue.prop = item.key;
            return tmpValue;
        });
    }
}



