import Vue from 'vue';
import { StudioModelEditor } from '../studio-model-editor';
import { clone } from 'ramda';
import './model-editor-view-controller.less';
/**
 * 动态模型模态展示控制器
 *
 * @export
 * @class ModelEditorViewController
 */
export class ModelEditorViewController {
    /**
     * Creates an instance of ModelEditorViewController.
     * @memberof ModelEditorViewController
     */
    constructor() {
        /**
         * 模型是否修改
         *
         * @protected
         * @type {boolean}
         * @memberof ModelEditorViewController
         */
        this.isModelChange = false;
        this.init();
    }
    /**
     * 初始化
     *
     * @protected
     * @memberof ModelEditorViewController
     */
    init() {
        this.container = document.createElement('div');
        document.body.appendChild(this.container);
    }
    /**
     * 打开视图
     *
     * @author chitanda
     * @date 2021-03-25 20:03:41
     * @param {IContext} context
     * @param {IParams} params
     * @return {*}  {Promise<any>}
     */
    open(context, params) {
        return new Promise((resolve) => {
            this.resolve = resolve;
            this.isModelChange = false;
            if (!this.vueInstance) {
                this.vueInit(context, params);
            }
            else {
                this.vueInstance.modalContext = clone(context);
                this.vueInstance.modalParams = clone(params);
                this.vueInstance.showChange(true);
            }
        });
    }
    /**
     * vue实例初始化
     *
     * @protected
     * @param {IContext} context
     * @param {*} [params={}]
     * @returns {void}
     * @memberof ModelEditorViewController
     */
    vueInit(context, params = {}) {
        const ctrl = this;
        this.vueInstance = new Vue({
            data() {
                return {
                    isShow: false,
                    modalContext: context,
                    modalParams: params,
                };
            },
            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.StudioModelEditor && this.isShow) {
                        this.$refs.StudioModelEditor.setParam(this.modalContext, this.modalParams);
                    }
                },
                close() {
                    this.showChange(false);
                    if (ctrl.resolve) {
                        ctrl.resolve(ctrl.isModelChange);
                    }
                },
                saved() {
                    ctrl.isModelChange = true;
                },
            },
            render(h) {
                return h('div', {
                    class: 'studio-model-editor-drawer',
                    style: {
                        'margin-top': this.isShow ? '0px' : '-100vh',
                    },
                }, [
                    h('div', {
                        class: 'studio-model-editor-drawer-container',
                    }, [
                        h(StudioModelEditor, {
                            ref: 'StudioModelEditor',
                            props: {
                                isClose: !this.isShow,
                            },
                            on: {
                                close: this.close,
                                saved: this.saved,
                            },
                        }),
                    ]),
                ]);
            },
        }).$mount(this.container);
    }
}
// 导出控制器
export const modelEditorController = new ModelEditorViewController();
