import { GUIManager } from "./GUIManager";
import GUIMediator from "./GUIMediator";
import LayerManager from "./layers/LayerManager";


/**
 * UI代理
 */
export default class GUIProxy
{
    key:number;
    assetBundleName:string;
    assetBundle:cc.AssetManager.Bundle;
    packageName:string;
    resName:string;
    mediatorClass:any;

    uiNode:fgui.GComponent;
    mediator:GUIMediator;
    /**
     * 状态 0 未加载  1加载中 2加载完成
     */
    state:number=0;

    /**
     * 是否显示
     */
    isShowing:boolean;

    data:any;

    constructor(key:number,assetBundle:string,packageName:string,resName:string,mediatorClass:any){
        this.key=key;
        this.assetBundleName=assetBundle;
        this.packageName=packageName;
        this.resName=resName;
        this.mediatorClass=mediatorClass;
    }

    /**
     * 显示(内部接口)
     * @param data 
     */
    Show(data?:any):void{
        this.data=data;
        this.isShowing=true;
        if(this.state==0){
            this.LoadUIAssets();
        }else if(this.state==1){
            //加载中啥也不干
        }else{
            this.AddToLayer();
        }
    }
    
    /**
     * 内部接口
     */
    Hide():void{
        if(this.mediator){
            LayerManager.single.RemoveFormeLayer(this.mediator.LayerIndex,this.uiNode);
            this.mediator.Hide();
        }
        this.isShowing=false;
    }

    /**
     * 心跳
     * @param dt 
     */
    Tick(dt:number):void{
        if(this.mediator!=null){
            this.mediator.Tick(dt);
        }
    }

    /**
     * 添加到层上并显示
     */
    private AddToLayer():void{
        this.uiNode.setSize(fgui.GRoot.inst.width,fgui.GRoot.inst.height);
        LayerManager.single.AddToLayer(this.mediator.LayerIndex,this.uiNode);
        this.mediator.Show(this.data);
    }
    
    private LoadUIAssets(){
        this.state=1;
        this.assetBundle=cc.assetManager.getBundle(this.assetBundleName);
        if(this.assetBundle==null){
            cc.assetManager.loadBundle(this.assetBundleName,this.OnLoadAssetBundleComplete.bind(this));
        }else{
            this.StartLoadUIPackage();
        }
    }

    private OnLoadAssetBundleComplete(err:Error,bundle:cc.AssetManager.Bundle):void{
        if(err){
            throw new Error("UI所在AssetBundle"+this.assetBundleName+"加载出错！");
        }
        this.assetBundle=bundle;
        this.StartLoadUIPackage();
    }

    private StartLoadUIPackage():void{
        fgui.UIPackage.loadPackage(this.assetBundle,this.packageName,this.OnLoadUIAssetComplete.bind(this));
    }
    
    private OnLoadUIAssetComplete(error: Error):void{
        if(error!=null){
            throw new Error("加载UI资源包出错:"+this.packageName);
        }
        this.state=2;
        this.uiNode=fgui.UIPackage.createObject(this.packageName,this.resName).asCom;
        this.mediator=new this.mediatorClass();
        this.mediator.uiKey=this.key;
        this.mediator._view=this.uiNode;
        //添加到层
        if(this.isShowing){
            this.AddToLayer();
        }
    }

    /**
     * 获取UI所属层
     */
    get LayerIndex(){
        return 3;
    }
}