import { Asset, Node, Prefab } from "cc";
import { EventMgr } from "../event/EventManager";
import { LoadQueue } from "../res/LoadQueue";
import { StringUtil } from "../util/StringUtil";
import { BaseLayer } from "./BaseLayer";
import { BaseView } from "./BaseView";
import { LayerConf, LayerType } from "../config/LayerConf";

//UI数据
export interface LayerInfo {
    id: number;
    view: BaseView;
    args: any[];
    res: Asset;
    callback: Function;
    active: Boolean;
    //是否加载中
    bLoading: Boolean;
}

export class LayerManager {
    private _game_layer: BaseLayer = null;
    private _ui_layer: BaseLayer = null;
    private _notice_layer: BaseLayer = null;
    private _guide_layer: BaseLayer = null;
    private _layer_conf: { [key: number]: LayerConf } = {};
    private _class_uuid = StringUtil.getUUID(32);
    constructor(root: Node, conf: any) {
        this._layer_conf = conf;
        //预加载资源
        for (let id in this._layer_conf) {
            let conf = this._layer_conf[id];
            (conf.preload) && LoadQueue.pushPreload(1, conf.bundle, conf.url, Prefab);
            //conf.destroy = (conf.destroy) ? conf.destroy : true;
        }
        this._game_layer = new BaseLayer("Game_Layer", this._layer_conf);
        root.addChild(this._game_layer);
        this._ui_layer = new BaseLayer("UI_Layer", this._layer_conf, true);
        root.addChild(this._ui_layer);
        this._notice_layer = new BaseLayer("Notice_Layer", this._layer_conf);
        root.addChild(this._notice_layer);
        this._guide_layer = new BaseLayer("Guide_Layer", this._layer_conf);
        root.addChild(this._guide_layer);
        EventMgr.addEvent("_Framewrok_Show_GameLayer", (value: boolean) => {
            // this._game_layer.showLayerUI(value);
        }, this, this._class_uuid);
    }

    /**
     * 打开UI
     * @param id UIID
     * @param callback UI打开成功回调
     * @param args 打开UI的可传参数
     */
    open(id: number, callback: Function = null, ...args: any[]) {
        // let openState = GameUtil.checkSystemOpen(id)
        // if (!openState.state) {
        //     Framework.tips.setTips(openState.tips);
        //     return
        // }
        let conf = this._layer_conf[id];
        // Framework.tips.setTips('huidhkaj shdgk ')
        if (conf) {
            //完全打开需要去检测引导手指
            let mCallback = () => {
                // Framework.event.fireEvent(GameEvent.GuideClick_Update_Finger, GuideManager.needShowFinger());
                if (callback) {
                    callback();
                }
            };
            console.log('conf.type', conf.type)
            if (conf.type === LayerType.Game) {
                this._game_layer.open(id, mCallback, args);
            } else if (conf.type === LayerType.UI) {
                this._ui_layer.open(id, mCallback, args);
            } else if (conf.type === LayerType.Notice) {
                this._notice_layer.open(id, mCallback, args);
            } else if (conf.type === LayerType.Guide) {
                this._guide_layer.open(id, mCallback, args);
            }
            return true;
        }
        console.error("未找到对应UI:" + id);
        return false;
    }

    /**
    * 关闭UI
    * @param ui 待关闭的UI,该值可为id也可为this
    */
    close(ui: number | BaseView) {
        if (ui) {
            let id = (typeof ui != "number") ? ui.view_id : ui;
            let conf = this._layer_conf[id];
            if (conf) {
                if (conf.type === LayerType.Game) {
                    this._game_layer.close(ui);
                } else if (conf.type === LayerType.UI) {
                    this._ui_layer.close(ui);
                } else if (conf.type === LayerType.Notice) {
                    this._notice_layer.close(ui);
                } else if (conf.type === LayerType.Guide) {
                    this._guide_layer.close(ui);
                }
                return true;
            }
        }
        return false;
    }
    ShowUI(ui, state, callback: Function = null, ...args: any[]) {
        this._ui_layer.ShowUI(ui, state, callback, args);
    }
    /**
     * 获取UI对应的UIView
     * @param id UIID
     */
    getUIView(id: number) {
        let conf = this._layer_conf[id];
        if (conf) {
            if (conf.type === LayerType.Game) {
                return this._game_layer.getUIView(id);
            } else if (conf.type === LayerType.UI) {
                return this._ui_layer.getUIView(id);
            } else if (conf.type === LayerType.Notice) {
                return this._notice_layer.getUIView(id);
            } else if (conf.type === LayerType.Guide) {
                return this._guide_layer.getUIView(id);
            }
        }
        console.error("未找到对应UI:" + id);
        return null;
    }

    /**
    * 关闭所有UI
    */
    closeAllUI(withoutID: number = -1) {
        EventMgr.fireEvent("_Framewrok_Show_GameLayer", true);
        this._ui_layer.closeAll(withoutID);
    }

    closeAllGame(withoutID: number = -1) {
        this._game_layer.closeAll(withoutID);
    }
}