import { _decorator, Component, Node } from 'cc';
import { GUIManager } from './GUIManager';
const { ccclass, property } = _decorator;

@ccclass('GUIMediator')
export class GUIMediator extends Component {
    
    uiKey:number;
    data:any;

    OnShow(data?:any):void{
        this.data=data;
    }

    OnHide():void{

    }

    /**
     * 关闭自己
     */
    HideSelf():void{
        GUIManager.single.Hide(this.uiKey);
    }

    update(dt:number):void{
        this.enterFrame();
    }

    get LayerIndex(){
        return 3;
    }


    /**
     * 下一帧需要回调的函数
     */
    private nextFrameCallHandler:Function[]=[];

    /**
     * 进入这一帧
     */
    enterFrame():void{
        this.nextFrameCallHandler.forEach(element => {
            element();
        });
        this.nextFrameCallHandler.length=0;
    }

    /**
     * 
     * @param value 下一帧调用
     */
    CallNextFrame(value:Function):void{
        if(this.nextFrameCallHandler.indexOf(value)>=0){
            return;
        }
        this.nextFrameCallHandler.push(value);
    }
}
