// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { GUIManager } from "./GUIManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GUIMediator{

    _view:fgui.GComponent;
    uiKey:number;
    data:any;

    /**
     * 内部接口
     * @param data 
     */
    Show(data?:any):void{
        this.data=data;
        this.AddEvents();
        this.OnShow();
    }

    Hide():void{
        this.RemoveEvents();
        this.OnHide();
    }

    protected OnShow():void{
        
    }

    protected OnHide():void{

    }

    protected AddEvents():void{

    }

    protected RemoveEvents():void{

    }

    /**
     * 心跳
     * @param dt 
     */
    Tick(dt:number):void{
        this.enterFrame();
    }

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

    /**
     * 销毁
     */
    OnDestory():void{

    }

    /**
     * 所在层次
     */
    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);
    }
}
