import UIWindowView from "../BaseView/UIWindowView";
import { EventConst } from "../ConstDefine/EventConst";
import AppFacade from "../AppFacade";
import LoopMgr from "./LoopMgr";
import Manager from "./Manager";
import View from "../BaseView/View";

/*
管理窗口类型的view
*/
class ViewInfo{
    public view:View
    public timer:number=0
    public isRecycle:boolean=false
}

export default class ViewMgr extends Manager{

    static get TypeName(): string{return "ViewMgr"}
    private intervalId:number=-1
    private viewDic:Map<string,ViewInfo>
    private allVies:Array<View>


    async start(){
        this.allVies=new Array()
        this.viewDic=new Map()
        cc.systemEvent.on(EventConst.CloseAllWindow,this.closeAllWindowView,this)
        this.intervalId=AppFacade.getMgr(LoopMgr).setInterval(this.onInterVal.bind(this),1)
        console.log("ViewMgr onStart")
    }

    public release(){
        console.log("ViewMgr onRelease")
        AppFacade.getMgr(LoopMgr).clearInterval(this.intervalId)
        this.intervalId=-1
        cc.systemEvent.off(EventConst.CloseAllWindow,this.closeAllWindowView,this)
        this.releaseViews()
    }
    
    private onInterVal(){
        for(let [name,info] of this.viewDic.entries()){
            if(!info.isRecycle) continue
            info.timer-=1
            if(info.timer<=0){
                let view=info?.view
                if(view&&!view.IsOpen){
                    info.view=null
                    this.viewDic.delete(name)
                    view.release()
                }
            }
        }
    }


    /**
     @param 释放所有windows 释放
    */
    public releaseViews():void{
        for(let i=this.allVies.length-1;i>=0;--i){
            let view=this.allVies[i] as View
            view?.release()
        }
        this.allVies.length=0
        UIWindowView.maskView?.release()
        UIWindowView.maskView=null
    }

    private closeAllWindowView():void{
        UIWindowView.maskView?.closeAll()
    }

    /**
     * 界面是否开启
     * @param viewNames  TODO
     */
    public isViewOpen(...viewNames:string[]):boolean{
        if(!viewNames) return false
        let res=false
        for(let name of viewNames){
            let view=this.allVies?.find((view)=>view.getTypeName()==name)
            if(view instanceof UIWindowView){
                res=UIWindowView?.maskView.isInStack(view)
            }else if(view instanceof View){
                res=view.IsOpen
            }
            if(!res) break
        }
        return res
    }

    
    public openView<T extends View>(viewName:string,type:typeof View):T{
        let info=this.viewDic.get(viewName)
        if(!info){
            info=new ViewInfo()
        }
        let view=info?.view
        if(!view){
            view=new type()
            info.view=view
            this.viewDic.set(viewName,info)
        }
        info.isRecycle=false
        return <T>view
    }

    public async updateView(viewName:string,data?:Object){
        let info=this.viewDic.get(viewName)
        await info?.view?.updateData(data)
    }

    public async closeView(viewName:string){
        let info=this.viewDic.get(viewName)
        info?.view?.close()
    }

    public releaseView(viewName:string,releaseTime:number){
        let info=this.viewDic.get(viewName)
        if(!info) return
        info.isRecycle=true
        info.timer=releaseTime
    }

    public getView<T extends View>(viewName:string):T{
        let info=this.viewDic.get(viewName)
        return info?.view as T
    }

}