import Dictionary from "../Util/Dictionary";
import LoadView from "../View/LoadView";
import MainView from "../View/MainView";
import ShadeView from "../View/ShadeView";
import NetManager from "../game_module/NetManager";



// import { SettingUi } from "../Window/SettingUi";


import CompleteView from "../View/CompleteView";
import InGameView from "../View/InGameView";


import MouseEvent from "../GameLogic/MouseEvent";
import { PoolType, GamePool } from "../GameLogic/GamePool";
import IntensifyView from "../View/IntensifyView";
import AttributeUpView from "../View/AttributeUpView";
import { SKinShopView } from "../View/SKinShopView";
import TrialView from "../View/TrialView";
import GuideView from "../View/GuideView";

export enum ViewType {
    InGameView,
    ShadeView,
    CompleteView,
    LoadView,
    MainView,
    SettingUi,
    SKinShopView,
    TrialView,
    IntensifyView,
    AttributeUpView,
    //GuideView,
}
export default class ViewManager {
    private static instance: ViewManager;
    public static get Instance(): ViewManager {
        return this.instance || (this.instance = new ViewManager());
    }
    constructor() {
        this.init();
    }
    private viewDic = [];
    private curView: any;
    private addViewDic = new Dictionary();
    public uiNode: Laya.Sprite;
    public popNode: Laya.Sprite;
    init() {
        this._maskCnt = 0;
        this.viewDic[ViewType.InGameView] = InGameView;
        this.viewDic[ViewType.LoadView] = LoadView;
        this.viewDic[ViewType.MainView] = MainView;
        this.viewDic[ViewType.ShadeView] = ShadeView;
   
        this.viewDic[ViewType.IntensifyView] = IntensifyView;
        this.viewDic[ViewType.CompleteView] = CompleteView;
        this.viewDic[ViewType.SKinShopView] = SKinShopView;
        this.viewDic[ViewType.TrialView] = TrialView;

        this.viewDic[ViewType.AttributeUpView] = AttributeUpView;
        //this.viewDic[ViewType.GuideView] = GuideView;
        this.uiNode = new Laya.Sprite();
        this.popNode = new Laya.Sprite();
        Laya.stage.addChild(this.uiNode);
        Laya.stage.addChild(this.popNode);
        this.uiNode.zOrder = 3;
        this.popNode.zOrder = 4;
    }
    public OpenView(view: ViewType, data?: any) {
        if (this.curView != null) {
            this.curView.OnClose();
            this.uiNode.removeChild(this.curView);
            this.curView.destroy(true);
            this.curView = null;
        }
        this.curView = new this.viewDic[view];



        this.uiNode.addChild(this.curView);
        this.curView.OnShow(data);
    }
    public get getCurView(): any {
        return this.curView;
    }
    public AddView(view: ViewType, data?: any) {
        if (this.addViewDic.has(view)) return;
        let t_view = new this.viewDic[view];
        this.addViewDic.set(view, t_view);
        this.uiNode.addChild(t_view);
        t_view.OnShow(data);
        MouseEvent.Instance.CloseInput();
    }
    public RemoveView() {
        if (this.curView != null) {
            this.curView.OnClose();
            this.uiNode.removeChild(this.curView);
            this.curView.destroy(true);
            this.curView = null;
        }
    }
    public HaveView(view: ViewType) {
        return this.addViewDic.has(view);
    }
    public CloseView(view: ViewType) {
        if (!this.addViewDic.has(view)) return;
        let t_view = this.addViewDic.get(view);
        t_view.OnClose();
        this.uiNode.removeChild(t_view);
        t_view.destroy(true);
        this.addViewDic.delete(view);
        MouseEvent.Instance.OpenInput();
    }
    private curMask: any;
    private _maskCnt = 0;
    public OpenMask() {
        this._maskCnt++;
        if (this._maskCnt > 1) return;
        if (this.curMask != null) {
            this.curMask.OnClose();
            this.popNode.removeChild(this.curView);
            this.curMask.destroy();
            this.curMask = null;
            return;
        }
        this.curMask = new this.viewDic[ViewType.ShadeView];
        if (!this.curMask) return;
        console.log("打开遮罩");
        this.popNode.addChild(this.curMask);
        this.curMask.OnShow();
    }
    public CloseMask() {
        this._maskCnt--;
        if (this._maskCnt > 0) return;
        if (this.curMask != null) {
            this.curMask.OnClose();
            this.popNode.removeChild(this.curMask);
            this.curMask.destroy(true);
            console.log("关闭遮罩");
            this.curMask = null;
        }
    }

    public ShowTips(value, type = 1) {//1:重上往下掉 2:再屏幕下方
        var warn = GamePool.Instance.SetActiveForUi(PoolType.WarnTips) as Laya.Text;
        if (!warn) return;
        // console.log(value);
        if (type == 1) {
            warn.y = 0;
            warn.text = value;
            warn.x = Laya.stage.width >> 1;
            this.popNode.addChild(warn);
            let _end = (Laya.stage.height >> 1) - 300;
            var tw = Laya.Tween.to(warn, { y: _end }, 1000, Laya.Ease.bounceOut, new Laya.Handler(this, () => {
                tw.clear();
                GamePool.Instance.SetDisableForUi(PoolType.WarnTips, warn);
            }));
        } else if (type == 2) {
            warn.y = Laya.stage.height - 400;
            warn.text = value;
            warn.x = Laya.stage.width >> 1;
            this.popNode.addChild(warn);
            //   let _end = (Laya.stage.height >> 1) - 300;
            var tw = Laya.Tween.to(warn, { alpha: 0 }, 1000, Laya.Ease.bounceOut, new Laya.Handler(this, () => {
                tw.clear();
                GamePool.Instance.SetDisableForUi(PoolType.WarnTips, warn);
                warn.alpha = 1;
            }));
        }
    }

    // /**
    //  * 暂停提示
    //  */
    // private toptips: TopTipUi;
    // public ShowTopTip() {
    //     if (!this.toptips) {
    //         this.toptips = new TopTipUi();
    //         this.popNode.addChild(this.toptips);
    //     }
    //     this.toptips.OnShow();
    // }
    // public CloseTopTip() {
    //     if (this.toptips) {
    //         this.toptips.OnClose();
    //     }
    // }
/**
     * 暂停提示
     */
    private GuideView: GuideView;
    public ShowGuideView() {
        if (!this.GuideView) {
            this.GuideView = new GuideView();
            this.popNode.addChild(this.GuideView);
        }
        this.GuideView.OnShow();
    }
    public CloseGuideView() {
        if (this.GuideView) {
            this.GuideView.OnClose();
            this.GuideView.removeSelf();
            this.GuideView.destroy();
        }
    }
}