import { IPSAppDEChartExplorerView, IPSChartExpBar } from '@ibizstudio-ex/runtime';
import { ChartExpViewEngine, ModelTool } from '@ibizstudio-ex/runtime';
import { ExpViewBase } from './expview-base';

export class ChartExpViewBase extends ExpViewBase {

    /**
     * 图表导航视图实例对象
     *
     * @type {IPSAppDEChartExplorerView}
     * @memberof ChartExpViewBase
     */
    declare viewInstance: IPSAppDEChartExplorerView;

    /**
     * 图表导航栏实例对象
     *
     * @type {IPSChartExpBar}
     * @memberof ChartExpViewBase
     */
    declare expBarInstance: IPSChartExpBar;

    /**
     * 图表导航视图引擎对象
     *
     * @type {ChartExpViewEngine}
     * @memberof ChartExpViewBase
     */
    engine: ChartExpViewEngine = new ChartExpViewEngine();

    /**
     * 视图模型初始化
     *
     * @memberof ChartExpViewBase
     */
    async viewModelInit() {
        await super.viewModelInit();
        this.viewInstance = (this.staticProps?.modeldata) as IPSAppDEChartExplorerView;
        this.expBarInstance = ModelTool.findPSControlByType('CHARTEXPBAR', this.viewInstance?.getPSControls?.() || []);
    }

    /**
     * 引擎初始化
     *
     * @memberof ChartExpViewBase
     */
    engineInit() {
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        let engineOpts = ({
          view: this,
          parentContainer: this.$parent,
          p2k: '0',
          chartexpbar:(this.$refs[this.expBarInstance.name] as any).ctrl,
          keyPSDEField: this.appDeCodeName.toLowerCase(),
          majorPSDEField: this.appDeMajorFieldName.toLowerCase(),
          isLoadDefault: this.viewInstance.loadDefault,
        });
        this.engine.init(engineOpts);
    }

    /**
     * 计算图表导航栏部件参数
     *
     * @memberof CalendarExpViewBase
     */
    computeTargetCtrlData(controlInstance:any) {
        const { targetCtrlName, targetCtrlParam, targetCtrlEvent } = super.computeTargetCtrlData(controlInstance);
        Object.assign(targetCtrlParam.staticProps,{
            sideBarLayout:this.viewInstance.sideBarLayout
        })
        return { targetCtrlName: targetCtrlName, targetCtrlParam: targetCtrlParam, targetCtrlEvent: targetCtrlEvent };
    }
}