import Vue from 'vue';
import { Util, AppServiceBase } from '@ibizstudio/runtime';
import { ModelClipboardImportView } from '../model-clipboard-import-view';
import './model-clipboard-import-view-controller.less';
/**
 * 动态模型模态展示控制器
 *
 * @export
 * @class ModelClipboardImportViewController
 */
export class ModelClipboardImportViewController {
    /**
     * Creates an instance of ModelClipboardImportViewController.
     * @memberof ModelClipboardImportViewController
     */
    constructor() {
        /**
         * 模型是否修改
         *
         * @protected
         * @type {boolean}
         * @memberof ModelClipboardImportViewController
         */
        this.isModelChange = false;
        /**
         * @description 层级
         * @private
         * @type {number}
         * @memberof ModelClipboardImportViewController
         */
        this.zIndex = 100;
        this.init();
    }
    /**
     * 初始化
     *
     * @protected
     * @memberof ModelClipboardImportViewController
     */
    init() {
        this.container = document.createElement('div');
        document.body.appendChild(this.container);
        this.store = AppServiceBase.getInstance().getAppStore();
    }
    /**
     * 打开视图
     *
     * @param {OpenParams} params
     * @returns {*}
     * @memberof ModelClipboardImportViewController
     */
    open(params) {
        this.handleZIndex();
        return new Promise((resolve) => {
            this.resolve = resolve;
            this.isModelChange = false;
            if (!this.vueInstance) {
                this.vueInit(params);
            }
            else {
                this.vueInstance.modalParams = Util.deepCopy(params);
                this.vueInstance.zIndex = this.zIndex;
                this.vueInstance.showChange(true);
            }
        });
    }
    /**
     * @description 处理z-index
     * @memberof ModelClipboardImportViewController
     */
    handleZIndex() {
        if (!this.store) {
            this.store = AppServiceBase.getInstance().getAppStore();
        }
        this.store.commit('upZIndex');
        this.zIndex = this.store.getters.getZIndex();
    }
    /**
     * vue实例初始化
     *
     * @protected
     * @param {*} [params={}]
     * @returns {void}
     * @memberof ModelClipboardImportViewController
     */
    vueInit(params = {}) {
        const ctrl = this;
        this.vueInstance = new Vue({
            data() {
                return {
                    isShow: false,
                    modalParams: params,
                    zIndex: ctrl.zIndex,
                };
            },
            created() {
            },
            mounted() {
                setTimeout(() => {
                    this.showChange();
                }, 100);
                this.updateModelParam();
            },
            updated() {
                this.updateModelParam();
            },
            methods: {
                showChange(is) {
                    if (is !== undefined) {
                        this.isShow = is;
                    }
                    else {
                        this.isShow = !this.isShow;
                    }
                },
                updateModelParam() {
                    if (this.$refs.ModelClipboardImportView && this.isShow) {
                        this.$refs.ModelClipboardImportView.setParam(this.modalParams);
                    }
                },
                close() {
                    this.showChange(false);
                    if (this.zIndex) {
                        ctrl.store.commit('downZIndex');
                    }
                    if (ctrl.resolve) {
                        ctrl.resolve(ctrl.isModelChange);
                    }
                },
                saved() {
                    ctrl.isModelChange = true;
                }
            },
            render(h) {
                return h('div', {
                    class: 'model-clipboard-import-view-drawer',
                    style: {
                        'margin-top': this.isShow ? '0px' : '-100vh',
                        'z-index': this.zIndex,
                    }
                }, [
                    h('div', {
                        class: 'model-clipboard-import-view-drawer-container'
                    }, [
                        h(ModelClipboardImportView, {
                            ref: 'ModelClipboardImportView',
                            props: {
                                isClose: !this.isShow
                            },
                            on: {
                                'close': this.close,
                                'saved': this.saved
                            }
                        })
                    ])
                ]);
            },
        }).$mount(this.container);
    }
}
// 导出控制器
export const modelClipboardImportViewController = new ModelClipboardImportViewController();
