import View from "./View";
import AppFacade from "../AppFacade";
import LayerMgr from "../Manager/LayerMgr";
import MaskView from "./MaskView";
import LoopMgr from "../Manager/LoopMgr";
import ViewMgr from "../Manager/ViewMgr";
import { ResConst } from "../ConstDefine/ResConst";
import { ViewConst, ViewType } from "../ConstDefine/ViewConst";

/**
@param UI窗体
*/
const TWEEN_TIME=0.3
export default abstract class UIWindowView extends View{

    //静态变量只有加载模块的时候调用
    public static maskView:MaskView=null

    private enterTween:cc.Tween=null
    private exitTween:cc.Tween=null
    private enterPos:cc.Vec3=cc.Vec3.ZERO
    private exitPos:cc.Vec3=cc.v3(0,cc.view.getDesignResolutionSize().height)
    protected btnClose:cc.Button=null
    //允许点击mask关闭
    public get IsCanClickMaskClose():boolean{return this.isCanClickMaskClose}
    protected isCanClickMaskClose:boolean=true

    private closeTimeoutId:number=-1

    public abstract getTypeName():string

    public getViewType():ViewType{return ViewType.UIWindow}

    protected setLayer():void{
        let layer=AppFacade.getMgr(LayerMgr)
        layer.setLayer(this.node,layer.WindowLayer)
        this.enterTween=cc.tween(this.node).to(0,{position:this.exitPos}).to(TWEEN_TIME,{position:this.enterPos},{easing:cc.easing.circInOut})
        this.exitTween=cc.tween(this.node).to(0,{position:this.enterPos}).to(TWEEN_TIME,{position:this.exitPos},{easing:cc.easing.expoIn})
    }

    protected async getChilds(){
        if(!UIWindowView.maskView){
            UIWindowView.maskView=new MaskView()
            UIWindowView.maskView.loadViewAsync(ResConst.UiABName,"Common/MaskView")
        }
        this.btnClose=this.getChildComp("BtnClose",cc.Button)
        this.btnClose?.node.on("click",this.close,this)
        
        await this.getWindowChilds()
    }

    /** 子类重写这个方法 
     *  会忘记调用 await this.getChilds()
    */
    protected abstract getWindowChilds()
    protected onOpenEnter(){}

    /** window不能限制，影响窗口再弹 */
    public open(){
        super.open()
        this.clearCloseTimeout()
        UIWindowView.maskView?.bindTarget(this)
        this.playAnimal(true)
    }

    public close():void{
        if(this.isOpen==false) return
        this.clearCloseTimeout()
        UIWindowView.maskView?.unBindTarget(this)
        this.playAnimal(false)
        AppFacade.getMgr(ViewMgr).releaseView(this.getTypeName(),ViewConst.ReleaseTime.UI)
        super.close()
        // this.closeTimeoutId=AppFacade.getMgr(LoopMgr).setTimeout(()=>{
        //     AppFacade.getMgr(ViewMgr).releaseView(this.getTypeName(),ViewConst.ReleaseTime.UI)
        //     super.close()
        // },TWEEN_TIME)
    }

    public hide(){
        this.playAnimal(false)
    }

    public playAnimal(isEnter:boolean){
        if(isEnter){
            // this.exitTween?.stop()
            // this.enterTween?.start()
            // AppFacade.getMgr(LoopMgr).setTimeout(this.onOpenEnter.bind(this),TWEEN_TIME)
            this.onOpenEnter()
        }else{
            // this.enterTween?.stop()
            // this.exitTween?.start()
        }
        // AppFacade.getMgr(SoundMgr).playEffect(SoundConst.Window)
    }

    private clearCloseTimeout(){
        AppFacade.getMgr(LoopMgr).clearTimeout(this.closeTimeoutId)
        this.closeTimeoutId=-1
    }
}