import { IPSAppIndexView,IPSAppMenu } from '@ibizstudio-ex/runtime';
import { IndexViewInterface, ModelTool, ViewTool } from '@ibizstudio-ex/runtime';
import { AppFuncService } from '../app-service';
import { ViewBase } from "./view-base";

/**
 * 首页视图基类
 *
 * @export
 * @class IndexViewBase
 * @extends {ViewBase}
 * @implements {IndexViewInterface}
 */
export class IndexViewBase extends ViewBase implements IndexViewInterface {

    /**
     * 是否支持应用切换
     *
     * @type {boolean}
     * @memberof IndexViewBase
     */
    isEnableAppSwitch: boolean = false;

    /**
     * 是否为应用起始页面
     *
     * @type {boolean}
     * @memberof IndexViewBase
     */
    isDefaultPage: boolean = false;

    /**
     * 是否为空白视图模式
     *
     * @type {boolean}
     * @memberof IndexViewBase
     */
    isBlankMode: boolean = false;

    /**
     * 菜单收缩变化
     *
     * @type {boolean}
     * @memberof IndexViewBase
     */
    collapseChange: boolean = false;

    /**
     * 菜单实例
     *
     * @type {*}
     * @memberof IndexViewBase
     */
    menuInstance!: IPSAppMenu;

    /**
     * 初始化应用首页视图实例
     *
     * @memberof IndexViewBase
     */
    async viewModelInit() {
        this.viewInstance = (this.staticProps?.modeldata) as IPSAppIndexView;
        await super.viewModelInit();
        this.menuInstance = (this.viewInstance.findPSControl(ModelTool.findPSControlByType("APPMENU", this.viewInstance.getPSControls()))) as IPSAppMenu;
    }

    /**
     * 视图初始化
     *
     * @memberof IndexViewBase
     */
    viewInit() {
        super.viewInit();
        AppFuncService.getInstance().init(this);
    }

    /**
     *  视图挂载
     *
     * @memberof IndexViewBase
     */
    viewMounted() {
        super.viewMounted();
        this.viewState.next({ tag: this.menuInstance?.name, action: 'load', data: {} });
        ViewTool.setIndexParameters([{ pathName: this.viewInstance?.codeName, parameterName: this.viewInstance?.codeName }]);
        ViewTool.setIndexViewParam(this.context);
        this.appLoadingDestroyed();
    }

    /**
     *  应用loading销毁
     *
     * @memberof IndexViewBase
     */
    appLoadingDestroyed() {
        setTimeout(() => {
            const el = document.getElementById('app-loading-x');
            if (el) {
                el.style.display = 'none';
            }
        }, 300);
    }

    /**
     * 视图销毁
     *
     * @memberof IndexViewBase
     */
    viewDestroyed() {
        super.viewDestroyed()
    }


    /**
     * 渲染视图主题内容
     *
     * @memberof IndexViewBase
     */
    renderMainContent() {
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent }: { targetCtrlName: string, targetCtrlParam: any, targetCtrlEvent: any } = this.computeTargetCtrlData(this.menuInstance);
        return this.$createElement(targetCtrlName, { props: targetCtrlParam, ref: this.menuInstance?.name, on: targetCtrlEvent });
    }

    /**
     * 计算目标部件所需参数
     *
     * @param {string} [controlType]
     * @returns
     * @memberof IndexViewBase
     */
    computeTargetCtrlData(controlInstance: any) {
        const { targetCtrlName, targetCtrlParam, targetCtrlEvent } = super.computeTargetCtrlData(controlInstance);
        Object.assign(targetCtrlParam.dynamicProps, {
            collapseChange: this.collapseChange
        })
        let counterdata: any = null;
        if (this.counterServiceArray?.length > 0) {
            counterdata = this.counterServiceArray[0].service?.counterData;
        }
        Object.assign(targetCtrlParam.staticProps, {
            mode: this.viewInstance?.mainMenuAlign,
            isDefaultPage: (this.viewInstance as IPSAppIndexView)?.defaultPage,
            isBlankMode: (this.viewInstance as IPSAppIndexView)?.blankMode,
            defPSAppView: (this.viewInstance as IPSAppIndexView)?.getDefPSAppView(),
            counterdata: counterdata,
        })
        return { targetCtrlName, targetCtrlParam, targetCtrlEvent }
    }

}
