import { ViewConst } from "../ConstDefine/ViewConst";
import AppFacade from "../AppFacade";
import LayerMgr from "../Manager/LayerMgr";

import UIWindowView from "./UIWindowView";
import View from "./View";


export default class MaskView extends View{
    //栈
    private viewStack:Array<UIWindowView>=new Array()
    //退出队列
    private exitQueue:Array<UIWindowView>=new Array()

    private newTarget:cc.Node=null
    
    public getTypeName(): string {
        return ViewConst.PauseView
    }

    protected setLayer(){
        this.newTarget&&this.setParentData(this.newTarget)
    }

    protected async getChilds(){
        this.node.on(cc.Node.EventType.TOUCH_END,this.onClickMask,this)
        this.node.active=true
        this.isOpen=true
    }

    /**
     * @param target 
     * @returns 是否可以显示出来
     */
    public bindTarget(target:UIWindowView){
        if(!target) return
        let init=this.viewStack.length<=0
        this.pushStack(target)
        if(init){
            this.node&&(this.isOpen=true) 
        }
    }

    public unBindTarget(target:UIWindowView){
        this.popStack(target)
        this.tryResycle()
    }

    /**
     * 返回是否已经存入
     * @param target 
     */
    private pushStack(target:UIWindowView){
        let lastView=this.viewStack[this.viewStack.length-1]
        if(lastView==target) return
        
        let index=this.viewStack.lastIndexOf(target)
        if(index>=0){
            this.viewStack.splice(index,1)
        }
        let preView=this.viewStack[this.viewStack.length-1]

        this.viewStack.push(target)
        this.setParentData(target?.node)

        this.popExitQueue(target)
        this.popExitQueue(preView)
        if(preView){
            this.exitQueue.push(preView)
            preView.hide()
        }
    }

    private popStack(target:UIWindowView) {
        //只是临时退出
        if(this.exitQueue.includes(target)){
            return
        }
        let index=this.viewStack.lastIndexOf(target)
        if(index>=0){
            this.viewStack.splice(index,1)
        }
        let preView=this.exitQueue.shift()
        if(preView){
            preView.open()
            this.setParentData(preView.node)
        }
    }

    private popExitQueue(target:UIWindowView){
        let index=this.exitQueue.lastIndexOf(target)
        if(index>=0){
            this.exitQueue.splice(index,1)
        }
    }

    private setParentData(node:cc.Node){
        if(!node) return
        if(this.node){
            this.setParent(node.parent)
            this.setPosition(cc.Vec2.ZERO_R)
            let sibling=node.parent?.childrenCount??0
            this.node.setSiblingIndex(sibling+1)
            node.setSiblingIndex(sibling)
        }else{
            this.newTarget=node
        }
    }

    private onClickMask(){
        // if(this.viewStack.length>0){
        //     let view=this.viewStack[0]
        //     if(view?.IsCanClickMaskClose){
        //         view.close()
        //     }
        // }
        // this.tryResycle()
    }

    public closeAll(){
        let tmpStack=this.viewStack.splice(0)
        this.viewStack.length=0
        this.exitQueue.length=0
        for(let view of tmpStack){
            view?.close()
        }
        this.tryResycle()
    }

    public isInStack(view:UIWindowView):boolean{
        return this.viewStack.includes(view)
    }

    private tryResycle(){
        if(this.viewStack.length<=0){
            this.exitQueue.length=0
            AppFacade.getMgr(LayerMgr).pushPool(this.node)
            this.isOpen=false
        }
    }


}