import { Subject } from 'rxjs';
import { AppWizardPanelService } from '..';
import { MainControlBase } from './main-control-base';
export class StateWizardPanelControlBase extends MainControlBase {
    constructor() {
        super(...arguments);
        /**
         * 向导表单参数
         *
         * @type {*}
         * @memberof StateWizardPanelControlBase
         */
        this.formParam = {};
        /**
         * 执行过的表单
         *
         * @public
         * @type {Array<string>}
         * @memberof StateWizardPanelControlBase
         */
        this.historyForms = [];
        /**
         * 步骤行为集合
         *
         * @type {*}
         * @memberof StateWizardPanelControlBase
         */
        this.stepActions = {};
        /**
         * 向导表单集合
         *
         * @type {Array<any>}
         * @memberof StateWizardPanelControlBase
         */
        this.wizardForms = [];
        /**
         * 当前状态
         *
         * @memberof StateWizardPanelControlBase
         */
        this.curState = '';
        /**
         * 当前激活表单
         *
         * @type {string}
         * @memberof StateWizardPanelControlBase
         */
        this.activeForm = '';
        /**
         * 首表单
         *
         * @type {string}
         * @memberof StateWizardPanelControlBase
         */
        this.firstForm = '';
        /**
         * 状态属性
         *
         * @type {string}
         * @memberof StateWizardPanelControlBase
         */
        this.stateField = '';
        /**
         * 步骤标识集合
         *
         * @type {*}
         * @memberof StateWizardPanelControlBase
         */
        this.stepTags = {};
        /**
         * 视图状态订阅对象
         *
         * @public
         * @type {Subject<{action: string, data: any}>}
         * @memberof StateWizardPanelControlBase
         */
        this.wizardState = new Subject();
        /**
         * 步骤是否显示集合
         *
         * @type {*}
         * @memberof StateWizardPanelControlBase
         */
        this.stepVisiable = {};
        /**
         * 当前显示表单
         *
         * @type {string}
         * @memberof StateWizardPanelControlBase
         */
        this.curShow = '';
        /**
         * 抽屉状态
         *
         * @memberof StateWizardPanelControlBase
         */
        this.drawerOpenStatus = {
            isOpen: false,
            formName: '',
        };
    }
    /**
     * 部件模型数据初始化实例
     *
     * @param {*} [args]
     * @memberof StateWizardPanelControlBase
     */
    async ctrlModelInit(args) {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        await super.ctrlModelInit(args);
        this.stateField = (_b = (_a = this.controlInstance.getStatePSAppDEField()) === null || _a === void 0 ? void 0 : _a.codeName) === null || _b === void 0 ? void 0 : _b.toLowerCase();
        //TODO行为首字母大写
        this.stateField = (_d = (_c = this.controlInstance.getStatePSAppDEField()) === null || _c === void 0 ? void 0 : _c.codeName) === null || _d === void 0 ? void 0 : _d.toLowerCase();
        this.initAction = ((_f = (_e = this.controlInstance.getInitPSControlAction()) === null || _e === void 0 ? void 0 : _e.getPSAppDEMethod()) === null || _f === void 0 ? void 0 : _f.codeName) || 'Get';
        this.finishAction = ((_h = (_g = this.controlInstance.getFinishPSControlAction()) === null || _g === void 0 ? void 0 : _g.getPSAppDEMethod()) === null || _h === void 0 ? void 0 : _h.codeName) || 'Update';
        if (!(this.Environment && this.Environment.isPreviewMode)) {
            this.service = new AppWizardPanelService(this.controlInstance, this.context);
            await this.service.loaded();
        }
        this.initFirstForm();
    }
    /**
     * 部件初始化
     *
     * @memberof StateWizardPanelControlBase
     */
    ctrlInit() {
        super.ctrlInit();
        this.regFormActions();
        this.doInit();
    }
    viewStateAction(tag, action, data) {
        if (!Object.is(tag, this.name)) {
            return;
        }
        super.viewStateAction(tag, action, data);
        if (Object.is('load', action)) {
            this.doInit(data);
        }
    }
    /**
     * 注册表单步骤行为
     *
     * @memberof StateWizardPanelControlBase
     */
    regFormActions() {
        const wizard = this.controlInstance.getPSDEWizard();
        const wizardForms = (wizard === null || wizard === void 0 ? void 0 : wizard.getPSDEWizardForms()) || [];
        if (wizard && wizardForms.length > 0) {
            wizardForms.forEach((form) => {
                var _a;
                this.regFormAction(`${this.controlInstance.name}_form_${form.formTag.toLowerCase()}`, form.getStepActions() || [], this.getStepTag(wizard.getPSDEWizardSteps() || [], (_a = form === null || form === void 0 ? void 0 : form.getPSDEWizardStep()) === null || _a === void 0 ? void 0 : _a.codeName));
            });
        }
    }
    /**
     * 初始化当前激活表单
     *
     * @memberof StateWizardPanelControlBase
     */
    initFirstForm() {
        var _a;
        const wizard = this.controlInstance.getPSDEWizard();
        const wizardForms = (wizard === null || wizard === void 0 ? void 0 : wizard.getPSDEWizardForms()) || [];
        if (wizard && wizardForms.length > 0) {
            //TODO IPSDEWizard getFirstPSDEWizardForm()无返回值
            const firstForm = wizardForms.find((form) => {
                return form.firstForm;
            });
            if (firstForm) {
                this.firstForm = `${this.controlInstance.name}_form_${(_a = firstForm.formTag) === null || _a === void 0 ? void 0 : _a.toLowerCase()}`;
            }
        }
    }
    /**
     * 状态表单加载完成
     *
     * @param {*} args 表单参数
     * @param {string} name 名称
     * @memberof StateWizardPanelControlBase
     */
    wizardpanelFormload(args, name) {
        if (args) {
            Object.assign(this.formParam, args);
        }
    }
    /**
     * 向导表单保存完成
     *
     * @param {*} args 表单参数
     * @param {string} name 名称
     * @memberof StateWizardPanelControlBase
     */
    wizardpanelFormsave(args, name) {
        Object.assign(this.formParam, args);
        if (Object.is(this.curState, 'NEXT')) {
            if (this.historyForms.indexOf(name) === -1) {
                this.historyForms.push(name);
            }
            this.setPopVisiable(name, false);
            if (this.getNextForm(name)) {
                this.activeForm = this.getNextForm(name);
                this.setPopVisiable(this.activeForm, true);
                setTimeout(() => {
                    this.formLoad(this.activeForm);
                }, 1);
            }
            else {
                this.doFinish();
            }
        }
        else if (Object.is(this.curState, 'FINISH')) {
            this.doFinish();
        }
    }
    /**
     * 步骤标题点击
     *
     * @param {string} name 步骤名称
     * @return {*}
     * @memberof StateWizardPanelControlBase
     */
    stepTitleClick(name) {
        const that = this;
        let activeIndex = that.wizardForms.indexOf(that.activeForm);
        let curIndex = that.wizardForms.indexOf(name);
        if (curIndex > activeIndex) {
            setTimeout(() => {
                that.$refs[name + '_popover'].showPopper = false;
                that.curShow = '';
            }, 0);
            return;
        }
        that.stepVisiable[name] = !that.stepVisiable[name];
        if (that.stepVisiable[name]) {
            that.curShow = name;
            that.formLoad(name);
        }
        else {
            that.curShow = '';
        }
    }
    /**
     * 左右按钮点击
     *
     * @param {*} mode 左右标识
     * @return {*}
     * @memberof StateWizardPanelControlBase
     */
    handleClick(mode) {
        if (Object.is(this.curShow, '')) {
            return;
        }
        let curIndex = this.wizardForms.indexOf(this.curShow);
        if (Object.is(mode, 'PRE') && curIndex !== 0) {
            this.setPopVisiable(this.wizardForms[curIndex], false);
            setTimeout(() => {
                this.setPopVisiable(this.wizardForms[curIndex - 1], true);
                this.formLoad(this.wizardForms[curIndex - 1]);
            }, 0);
        }
        if (Object.is(mode, 'NEXT') && curIndex < this.wizardForms.length - 1 && this.historyForms.includes(this.wizardForms[curIndex + 1])) {
            this.setPopVisiable(this.wizardForms[curIndex], false);
            setTimeout(() => {
                this.setPopVisiable(this.wizardForms[curIndex + 1], true);
                this.formLoad(this.wizardForms[curIndex + 1]);
            }, 0);
        }
    }
    /**
     * 打开链接
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    handleOpen(name) {
        this.handleClose(name);
        this.drawerOpenStatus.isOpen = true;
        this.drawerOpenStatus.formName = name;
    }
    /**
     * 关闭
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    handleClose(name) {
        this.setPopVisiable(name, false);
    }
    /**
     * 上一步
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    onClickPrev(name) {
        const length = this.historyForms.length;
        if (length > 0) {
            this.curState = 'PREV';
            let curIndex = this.wizardForms.indexOf(name);
            this.setPopVisiable(name, false);
            setTimeout(() => {
                this.setPopVisiable(this.historyForms[curIndex - 1], true);
                this.formLoad(this.historyForms[curIndex - 1]);
            }, 1);
        }
    }
    /**
     * 下一步
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    onClickNext(name) {
        if (name) {
            if (this.$refs && this.$refs[name]) {
                let form = this.$refs[name].ctrl;
                if (form.formValidateStatus()) {
                    this.curState = 'NEXT';
                    this.wizardState.next({ tag: name, action: 'save', data: this.formParam });
                }
                else {
                    this.$throw(this.$t('app.commonwords.rulesexception'), 'onClickNext');
                }
            }
        }
    }
    /**
     * 完成
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    onClickFinish(name) {
        if (name) {
            if (this.$refs && this.$refs[name]) {
                let form = this.$refs[name].ctrl;
                if (form.formValidateStatus()) {
                    this.curState = 'FINISH';
                    this.wizardState.next({ tag: name, action: 'save', data: this.formParam });
                }
                else {
                    this.$throw(this.$t('app.commonwords.rulesexception'), 'onClickFinish');
                }
            }
        }
    }
    /**
     * 抽屉状态改变
     *
     * @param {*} value  值
     * @memberof StateWizardPanelControlBase
     */
    onVisibleChange(value) {
        if (!value) {
            this.drawerOpenStatus.isOpen = false;
        }
    }
    /**
     * 获取步表单
     *
     * @param {IPSDEWizardStep} step 步骤模型
     * @return {*}
     * @memberof StateWizardPanelControlBase
     */
    getStepForm(step) {
        const editForms = this.controlInstance.getPSDEEditForms() || [];
        let stepForm;
        if (editForms.length > 0) {
            stepForm = editForms.find((form) => {
                var _a;
                const wizardForm = form === null || form === void 0 ? void 0 : form.getPSDEWizardForm();
                if (wizardForm) {
                    const wizardStep = (_a = wizardForm.getPSDEWizardStep()) === null || _a === void 0 ? void 0 : _a.codeName;
                    if (wizardStep && wizardStep == step.codeName) {
                        return true;
                    }
                }
                return false;
            });
        }
        return stepForm ? stepForm.name : '';
    }
    /**
     * 设置popover是否显示
     *
     * @param {string} name 表单名称
     * @param {boolean} isVisiable 是否显示
     * @memberof StateWizardPanelControlBase
     */
    setPopVisiable(name, isVisiable) {
        this.stepVisiable[name] = isVisiable;
        const refFrom = this.$refs[name + '_popover'];
        if (refFrom) {
            refFrom.showPopper = isVisiable;
            this.curShow = isVisiable ? name : '';
        }
    }
    /**
     * 是否显示
     *
     * @param {string} name 表单名称
     * @param {string} type 类型
     * @return {*}
     * @memberof StateWizardPanelControlBase
     */
    isVisiable(name, type) {
        const actions = this.stepActions[name];
        if (actions && actions.indexOf(type) !== -1 && Object.is(name, this.activeForm)) {
            return true;
        }
        else {
            return false;
        }
    }
    /**
     * 部件事件
     * @param ctrl 部件
     * @param action  行为
     * @param data 数据
     *
     * @memberof StateWizardPanelControlBase
     */
    onCtrlEvent(controlname, action, data) {
        if (Object.is(action, 'save')) {
            this.wizardpanelFormsave(data, controlname);
        }
        else if (Object.is(action, 'load')) {
            this.wizardpanelFormload(data, controlname);
        }
    }
    /**
     * 获取下一步向导表单
     *
     * @memberof StateWizardPanelControlBase
     */
    getNextForm(name) {
        let index = this.wizardForms.indexOf(name);
        if (index >= 0) {
            if (this.wizardForms[index + 1]) {
                return this.wizardForms[index + 1];
            }
        }
        return undefined;
    }
    /**
     * 完成行为
     *
     * @memberof StateWizardPanelControlBase
     */
    doFinish() {
        let arg = {};
        Object.assign(arg, this.formParam);
        Object.assign(arg, { viewparams: this.viewparams });
        let tempContext = JSON.parse(JSON.stringify(this.context));
        this.onControlRequset('doFinish', tempContext, arg);
        const post = this.service.finish(this.finishAction, tempContext, arg, this.showBusyIndicator);
        post
            .then((response) => {
            this.onControlResponse('doFinish', response);
            if (response && response.status === 200) {
                const data = response.data;
                this.ctrlEvent({
                    controlname: this.controlInstance.name,
                    action: 'finish',
                    data: data,
                });
            }
        })
            .catch((response) => {
            this.onControlResponse('doFinish', response);
            this.$throw(response, 'doFinish');
        });
    }
    /**
     * 获取步骤标识
     *
     * @param {Array<any>} wizardSteps 步骤数组
     * @param {string} tag 标识
     * @return {*}
     * @memberof StateWizardPanelControlBase
     */
    getStepTag(wizardSteps, tag) {
        if (wizardSteps && wizardSteps.length > 0 && tag) {
            let curStep = wizardSteps.find((step) => {
                return step.title === tag;
            });
            return curStep.stepTag || tag;
        }
        else {
            return tag;
        }
    }
    /**
     * 注册表单
     *
     * @param {string} name 名称
     * @param {Array<string>} actions 步骤集合
     * @param {*} stepTag 步骤标识
     * @memberof StateWizardPanelControlBase
     */
    regFormAction(name, actions, stepTag) {
        this.stepActions[name] = actions;
        this.stepTags[name] = stepTag;
        this.stepVisiable[name] = false;
        this.wizardForms.push(name);
    }
    /**
     * 计算激活表单
     *
     * @param {*} data 数据
     * @memberof StateWizardPanelControlBase
     */
    computedActiveForm(data) {
        if (data[this.stateField]) {
            if (Object.keys(this.stepTags).length > 0) {
                Object.keys(this.stepTags).forEach((name) => {
                    if (this.stepTags[name] === data[this.stateField]) {
                        this.activeForm = name;
                    }
                });
            }
            if (!this.activeForm) {
                this.activeForm = this.firstForm;
            }
        }
        else {
            this.activeForm = this.firstForm;
        }
        if (this.activeForm) {
            let index = this.wizardForms.indexOf(this.activeForm);
            this.wizardForms.forEach((item, inx) => {
                if (inx <= index) {
                    this.historyForms.push(item);
                }
            });
        }
    }
    /**
     * 初始化
     *
     * @param {*} [opt={}] 额外参数
     * @memberof StateWizardPanelControlBase
     */
    doInit(opt = {}) {
        const arg = Object.assign({}, opt);
        Object.assign(arg, { viewparams: this.viewparams });
        let tempContext = JSON.parse(JSON.stringify(this.context));
        this.onControlRequset('doInit', tempContext, arg);
        const post = this.service.init(this.initAction, tempContext, arg, this.showBusyIndicator);
        post
            .then((response) => {
            this.onControlResponse('doInit', response);
            if (response && response.status === 200) {
                this.formParam = response.data;
                if (response.data[this.appDeCodeName.toLowerCase()]) {
                    Object.assign(this.context, { [this.appDeCodeName.toLowerCase()]: response.data[this.appDeCodeName.toLowerCase()] });
                }
                this.computedActiveForm(this.formParam);
            }
        })
            .catch((response) => {
            this.onControlResponse('doInit', response);
            this.$throw(response, 'doInit');
        });
    }
    /**
     * 表单加载
     *
     * @param {string} name 表单名称
     * @memberof StateWizardPanelControlBase
     */
    formLoad(name) {
        if (name) {
            this.wizardState.next({ tag: name, action: 'load', data: this.formParam });
        }
    }
    ctrlDestroyed() {
        super.ctrlDestroyed();
        this.wizardState.complete();
    }
}
