import { IPSAppDETreeView, IPSDETree } from '@ibizstudio-ex/runtime';
import { ModelTool, PickupTreeViewEngine, PickupTreeViewInterface } from '@ibizstudio-ex/runtime';
import { MDViewBase } from './mdview-base';


/**
 * 选择树视图基类
 *
 * @export
 * @class PickupTreeViewBase
 * @extends {MDViewBase}
 * @implements {PickupTreeViewInterface}
 */
export class PickupTreeViewBase extends MDViewBase implements PickupTreeViewInterface {

    /**
     * 选中数据字符串
     *
     * @type {string}
     * @memberof PickupTreeViewBase
     */
    selectedData?: string;

    /**
     * 视图实例对象
     *
     * @type {IPSAppDETreeView}
     * @memberof PickupTreeViewBase
     */
    declare viewInstance: IPSAppDETreeView;

    /**
     * 树视图实例
     *
     * @public
     * @type {IPSDETree}
     * @memberof PickupTreeViewBase
     */
    treeInstance !: IPSDETree;

    /**
     * 视图引擎
     *
     * @public
     * @type {Engine}
     * @memberof PickupTreeViewBase
     */
    engine: PickupTreeViewEngine = new PickupTreeViewEngine();

    /**
     * 监听视图动态参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof PickupTreeViewBase
     */
    onDynamicPropsChange(newVal: any, oldVal: any) {
        super.onDynamicPropsChange(newVal, oldVal);
        if(newVal?.selectedData && newVal.selectedData != oldVal?.selectedData){
            this.selectedData = newVal.selectedData;
        }
    }

    /**
     * 监听参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof PickupTreeViewBase
     */
    onStaticPropsChange(newVal: any, oldVal: any) {
        this.isSingleSelect = newVal.isSingleSelect;
        super.onStaticPropsChange(newVal, oldVal)
    }

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


    /**
     * 计算目标部件所需参数
     *
     * @param {string} [controlType]
     * @returns
     * @memberof PickupGridViewBase
     */
    computeTargetCtrlData(controlInstance:any) {
        const { targetCtrlName, targetCtrlParam, targetCtrlEvent } = super.computeTargetCtrlData(controlInstance);
        Object.assign(targetCtrlParam.dynamicProps,{
            selectedData: this.selectedData,
        })
        Object.assign(targetCtrlParam.staticProps,{
            isSingleSelect: this.isSingleSelect,
            isSelectFirstDefault: true
        })
        return { targetCtrlName, targetCtrlParam, targetCtrlEvent };
    }

    /**
     * 初始化选择树视图实例
     *
     * @memberof PickupTreeViewBase
     */
    async viewModelInit() {
        this.viewInstance = (this.staticProps?.modeldata) as IPSAppDETreeView;
        await super.viewModelInit();
        this.treeInstance = ModelTool.findPSControlByType("TREEVIEW",this.viewInstance.getPSControls());
    }

    /**
     * 渲染视图主体内容区
     *
     * @memberof PickupTreeViewBase
     */
    renderMainContent() {
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent } = this.computeTargetCtrlData(this.treeInstance);
        Object.assign(targetCtrlParam.staticProps, { isSelectFirstDefault: false });
        return this.$createElement(targetCtrlName, { props: targetCtrlParam, ref: this.treeInstance.name, on: targetCtrlEvent });
    }

    /**
     * 快速搜索
     *
     * @returns {void}
     * @memberof PickupTreeViewBase
     */
    onSearch(): void {
        if (!this.treeInstance || !this.viewState) {
            return;
        }
        this.viewState.next({ tag: this.treeInstance.name, action: 'filter', data: { srfnodefilter: this.query } });
    }
}