import { ModuleTypes } from "../types/sceneTypes";
import Panel from "../components/Panel";
import Loading from "../components/Loading";
export default class PanelCtrl extends egret.EventDispatcher {
    private _parent: egret.Sprite;
    private _mask: egret.Sprite;
    static _instance: PanelCtrl;
    static get instance() {
        return PanelCtrl._instance || (PanelCtrl._instance = new PanelCtrl())
    }

    init(parent: egret.Sprite) {
        this._parent = parent;
    }

    show(type: ModuleTypes, data?) {
        if (this._current) {
            this.closeCurrent();
        }
        const cls = this._panelClassMap[type];
        const panel: Panel = new cls(data);
        panel.type = type;
        this.addMask();
        if (!window['development']) {
            Loading.instace.show();
            const key = panel.skinKey.toLowerCase();
            const p = Promise.all([RES.getResAsync(`${key}_json`), RES.getResAsync(`${key}_png`)]);
            p.then(() => {
                Loading.instace.hide();
                this.showAfterLoadAtlas(panel, data);
            })
        } else {
            this.showAfterLoadAtlas(panel, data);
        }
        return panel;
    }

    showAfterLoadAtlas(panel: Panel, data) {
        panel.loadSkin();
        this._current = panel;
        panel.start(data);
        this.add(panel);
        egret.Tween.get(panel).set({ y: -200 }).to({ y: 0 }, 500, egret.Ease.quartOut);
    }

    addMask() {
        if (!this._mask) {
            this._mask = new egret.Sprite();
            this._mask.touchEnabled = true;
            this._mask.graphics.beginFill(0, .7);
            this._mask.graphics.drawRect(0, 0, 750, 1624);
            this._mask.graphics.endFill();
        }
        this._mask.alpha = 1;
        this._parent.addChild(this._mask);
    }

    removeMask(useTween) {
        if (this._mask) {
            if (useTween)
                egret.Tween.get(this._mask).to({ alpha: 0 }, 300, egret.Ease.quartIn).call(
                    () => this._parent.removeChild(this._mask), this);
            else
                this._parent.removeChild(this._mask)
        }
    }

    private add(panel: Panel) {
        this._parent.addChild(panel);
        panel.once('onCloseBtnClicked', this.onPanelHide, this);
    }

    private remove(panel: Panel, useTween = true, closeType = '') {
        if (!useTween) {
            this._parent.removeChild(panel);
            this.removeMask(useTween);
            this.dispatchEvent(new egret.Event('onPanelRemoved', false, false, { panel: panel, closeType: closeType }));
        } else {
            const tw = egret.Tween.get(panel);
            tw.to({ y: panel.stage.stageHeight }, 300, egret.Ease.quartIn)
                .call(() => {
                    this._parent.removeChild(panel);
                    this.removeMask(useTween);
                    this.dispatchEvent(new egret.Event('onPanelRemoved', false, false, { panel: panel, closeType: closeType }))
                }, this);
        }
        this._current = null;
    }

    private onPanelHide(e: egret.Event) {
        const panel = e.target as Panel;
        this.remove(panel, true, 'closeByClick');
    }

    private _panelClassMap: any;
    registerPanel(name, config) {
        this._panelClassMap = this._panelClassMap || {};
        this._panelClassMap[name] = config.cls;
    }


    private _current: Panel;

    closeCurrent() {
        if (this._current) {
            this._current.removeEventListener('onCloseBtnClicked', this.onPanelHide, this);
            this._current.destroy();
            this.remove(this._current, false);
        }
    }
}