import { HomeWindow } from "./HomeWindow";
import { InGameWindow } from "./InGameWindow";
import { SettlementWindow } from "./SettlementWindow";
import { ReviveWindow } from "./ReviveWindow";
import { ShopWindow } from "./ShopWindow";
import { QueueWindow } from "./QueueWindow";
import { SettingWindow } from "./SettingWindow";
import { RankWindow } from "./RankWindow";
import { SignWindow } from "./SignWindow";
import ADPlayer from "../WXSDK/ADPlayer";
import { RecommendWindow } from "./RecommendWindow";
import { TrialWindow } from "./TrialWindow";
import { OffLineWindow } from "./OffLineWindow";

import { FriendPlyaingWindow } from "./FriendPlayingWindow";
import { MoreFunWindow } from "./MoreFunWindow";

//新增FriendPlayingWindow,MoreFunWindow,OffLineWindow ----2019.09.21 赵成阳
export enum WindowType{HomeWindow,InGameWindow,SettlementWindow,ReviveWindow,ShopWindow,QueueWindow,SettingWindow,RankWindow,SignWindow,TaskWindow,RecommendWindow,TrialWindow,FriendPlayingWindow,MoreFunWindow,OffLineWindow}

export class WindowManager 
{
    private static WindowManagerInstance:WindowManager;

    constructor()
    {
        WindowManager.WindowManagerInstance = this;

        this.Init();
    }
    public static get Instance()
    {
        if(WindowManager.WindowManagerInstance)
        {
            return WindowManager.WindowManagerInstance;
        }
        else
        {
            return new WindowManager();
        }
    }

    public curOpenedView:any;
    private ViewDic:[any][any] = [];
    private popViewDic:[any][any] = [];
    private viewCont:Laya.Sprite = new Laya.Sprite;
    private popCont:Laya.Sprite = new Laya.Sprite;
    

    Init()
    {
        this.ViewDic[WindowType.HomeWindow] = HomeWindow;
        this.ViewDic[WindowType.InGameWindow] = InGameWindow;
        this.ViewDic[WindowType.SettlementWindow] = SettlementWindow;
        this.ViewDic[WindowType.ReviveWindow] = ReviveWindow;
        this.ViewDic[WindowType.ShopWindow] = ShopWindow;
        this.ViewDic[WindowType.QueueWindow] = QueueWindow;
        this.ViewDic[WindowType.SettingWindow] = SettingWindow;
        this.ViewDic[WindowType.RankWindow] = RankWindow;
        this.ViewDic[WindowType.SignWindow] = SignWindow;
        this.ViewDic[WindowType.RecommendWindow] = RecommendWindow;
        this.ViewDic[WindowType.TrialWindow] = TrialWindow;
        //新增FriendPlayingWindow  MoreFunWindow OffLineWindow-----2019.09.21 赵成阳
        this.ViewDic[WindowType.FriendPlayingWindow] = FriendPlyaingWindow;
        this.ViewDic[WindowType.MoreFunWindow] = MoreFunWindow;
        this.ViewDic[WindowType.OffLineWindow] = OffLineWindow;

        Laya.stage.addChild(this.viewCont);
        Laya.stage.addChild(this.popCont);
    }

    public ShowWindow(_type:WindowType)
    {
        
        if(this.curOpenedView != null)
        {
            this.curOpenedView.OnHide();
            this.curOpenedView.destroy();
            this.curOpenedView.removeSelf();
        }

        ADPlayer.ClearBanner();

        this.ClearPopViews();
        this.curOpenedView = new this.ViewDic[_type];
        this.curOpenedView.name = WindowType[_type];
        this.viewCont.addChild(this.curOpenedView);
        this.curOpenedView.OnOpen();
        
    }

    public ShowWindowWithData(_type:WindowType,_data:any)
    {
        if(this.curOpenedView != null)
        {
            this.curOpenedView.OnHide();
            this.curOpenedView.destroy();
            this.curOpenedView.removeSelf();
        }

        ADPlayer.ClearBanner();

        this.ClearPopViews();

        this.curOpenedView = new this.ViewDic[_type];
        this.curOpenedView.name = WindowType[_type];
        this.viewCont.addChild(this.curOpenedView);
        this.curOpenedView.OnOpen(_data);
    }

    public OpenPopView(viewType:WindowType,data:any = null)
    {
        ADPlayer.ClearBanner();
        var popView;
        if(this.popViewDic[viewType])
        {
            popView = this.popViewDic[viewType];

            this.popCont.setChildIndex(popView,this.popCont.numChildren - 1);
            popView.visible = true;
        }
        else
        {
            popView = new this.ViewDic[viewType];
            this.popCont.addChild(popView);
            this.popViewDic[viewType] = popView;
        }

        popView.OnOpen(data);
    }

    public ClosePopView(viewType:WindowType)
    {
        var popView = this.popViewDic[viewType];
        if(popView == null){
            return;
        }
        ADPlayer.ClearBanner();
        popView.OnHide();
        popView.visible = false;
       
    }

    CloseCurrentWindow()
    {
        this.curOpenedView.OnHide();
        this.curOpenedView.destroy();
        this.curOpenedView.removeSelf();
    }

    ClearPopViews()
    {
        ADPlayer.ClearBanner();
        this.ClosePopView(WindowType.RecommendWindow);
        this.ClosePopView(WindowType.OffLineWindow);
        //this.ClosePopView(ViewType.NewRewardView);
    }
}