import { ModuleTypes } from "../types/sceneTypes";
import Panel from "../views/Panel";
import Msg from "../alert/Msg";
export default class PanelCtrl {
    private _parent: eui.UILayer;
    private _mask: egret.Sprite;
    static _instance: PanelCtrl;
    /**
     * 实例
     */
    static get instance() {
        return PanelCtrl._instance || (PanelCtrl._instance = new PanelCtrl())
    }

    /**
     * 初始化
     * @param parent 赋值父级 
     */
    init(parent: eui.UILayer) {
        this._parent = parent;
    }

    /**
     * 显示
     * @param type 类型
     * @param data 数据
     */
    show(type: ModuleTypes, data?) {
        this._parent.visible = true;
        if(this._current){
            this._parent.removeChild(this._current)
        }else{

        }
        this.addMask();
        const cls = this._panelClassMap[type];
        const panel: Panel = new cls(data);
        
        this._current = panel;
        panel.start(data);
        this.add(panel);
        egret.Tween.get(panel).set({ y: -200 }).to({ y: 0 }, 500, egret.Ease.getBackOut(2));
        return panel;
    }

    /**
     * 加入黑色蒙层
     */
    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() {
        if (this._mask) {
            egret.Tween.get(this._mask).to({ alpha: 0 }, 200).call(
                () => this._parent.removeChild(this._mask), this);
        }
    }


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

    /**
     * 动画后移除Panel
     * @param panel 
     */
    private remove(panel: Panel) {
        const tw = egret.Tween.get(panel);
        tw.to({ y: 1624 }, 400, egret.Ease.getBackIn(2))
            .call(() => {
                egret.Tween.removeAllTweens();
                this._parent.removeChild(panel);
                this._parent.visible =false;
                this._current = null;
                this.removeMask();
            }, this);
    }

    /**
     * 立即移除Panel
     * @param panel 
     */
    private removeImmediately(panel: Panel) {
        this._parent.removeChild(panel);
        this._parent.visible =false;
        this._current = null;
        this.removeMask();
    }


    /**
     * 隐藏Panel
     * @param e 
     */
    private onPanelHide(e: egret.Event) {
        let panel = e.target as Panel;

        panel.removeEventListener('onDestroy', this.onPanelHide, this);
        this.removeImmediately(panel);
    }

    private _panelClassMap: any;

    /**
     * 注册Panel
     * @param name Panel名字
     * @param definition 定义 
     */
    registerClass(name, definition) {
        this._panelClassMap = this._panelClassMap || {};
        this._panelClassMap[name] = definition;
    }


    private _current: Panel;

    /**
     * 关闭当前 有动画
     */
    closeCurrent() {
        if (this._current)
            this.remove(this._current);
    }

    /**
     * 关闭当前 无动画
     */
    closeCurrentImmediately() {
        if (this._current)
            this.removeImmediately(this._current);
    }
}