
import { Prefab, instantiate, Node, Vec3 } from "cc";
import GameConst from "../common/GameConst";
import BasePanel from "../component/BasePanel";
import Utils from "../utils/Utils";


export default class WindowManager {
    private static instance: WindowManager;
    private _windowContainer: Node;

    private _windows: Array<Node> = []
    private _showlist: Object = {};
    private _prefabMap: Object = {}
    public static get ins(): WindowManager {
        if (!WindowManager.instance) {
            WindowManager.instance = new WindowManager();
        }
        return WindowManager.instance;
    }

    public register(container: Node): void {
        this.clearAll();
        this._windowContainer = container
    }

    public open(name: string, showData: any = null, pos: Vec3 = null, parent: Node = null): Promise<any> {
        if (this._showlist[name])
            return;
        return new Promise((resolve, reject) => {
            if (this._showlist[name]) {
                // console.error(name)
                reject(null);
                return
            }
            this._showlist[name] = true
            let path = 'gameui/' + name;

            // BusyLoadingManager.ins.addBusy(BUSY_TYPE.RES)

            Utils.loadRes(path, "prefabs", Prefab, (err: Error, res: Prefab) => {
                if (err) {
                    console.log('加载错误', path)
                    this._showlist[name] = false
                    reject(null);
                    return
                }
                if (this._showlist[name] == false) {
                    reject(null);
                    return
                }
                console.log('open win ', name)
                let win: Node = instantiate(res)
                win.name = name
                win.parent = parent || this._windowContainer;
                this.addWin(win)
                // this.sortWins()
                if (pos != null) {
                    win.position = pos
                }
                this._prefabMap[path] = res

                let basePanel: any = win.getComponent(name)
                if (basePanel) {
                    basePanel.show(showData)
                    // BusyLoadingManager.ins.removeBusy(BUSY_TYPE.RES)
                }

                resolve(basePanel);
            })
        })
    }
    private addWin(win: Node): void {
        var winPanel: BasePanel = win.getComponent(win.name) as BasePanel
        var layer: number = winPanel.layer
        var index: number = 0
        for (let i = this._windows.length - 1; i >= 0; i--) {
            let tempWin = this._windows[i]
            let tempWinPanel: BasePanel = tempWin.getComponent(tempWin.name) as BasePanel
            if (tempWinPanel.layer <= layer) {
                index = i + 1
                break
            }
        }
        this._windows.splice(index, 0, win);
    }
    // private sortWins(): void {
    //     var bol: boolean = false
    //     for (var i = this._windows.length - 1; i >= 0; i--) {
    //         var win: Node = this._windows[i]
    //         // win.zIndex = i
    //         var winComp: BasePanel = win.getComponent(BasePanel);
    //         if (!winComp) {
    //             debugger;
    //         }
    //         if (bol) {
    //             win.active = false
    //         } else {
    //             win.active = true
    //             bol = winComp.winType == GameConst.WIN_TYPE.FULLSCREEN
    //         }
    //     }
    // }

    public removeWin(name: string): void {
        if (this._showlist[name]) {
            delete this._showlist[name]
        }
        for (var i = this._windows.length - 1; i >= 0; i--) {
            var win = this._windows[i]
            if (win.name == name) {
                var winComp: BasePanel = win.getComponent(win.name) as BasePanel
                if (winComp.winType != GameConst.WIN_TYPE.PERMANENT) {
                    win.destroy()
                    win = null
                    if (winComp.destroyAfterClose) {
                        //to fix
                        // if (this._prefabMap[name])
                        //     loader.release(this._prefabMap[name])
                    }
                    this._windows.splice(i, 1)
                    break
                }
            }
        }
        // this.sortWins()
    }
    public removeAll(exclude: string[] = null) {
        for (var i = this._windows.length - 1; i >= 0; i--) {
            var win = this._windows[i]
            if (exclude && exclude.indexOf(win.name))
                continue
            this.removeWin(win.name)

        }
    }

    public isOpen(name: string) {
        // return this._showlist[name] == true;
        return this.getWin(name) != null
    }

    public isShow(name: string) {
        return this._showlist[name] == true;
    }

    public getWinCount() {
        return Utils.count(this._showlist)
    }
    public getWin(name: string) {
        for (var i = this._windows.length - 1; i >= 0; i--) {
            var win = this._windows[i]
            if (win.name == name)
                return win

        }
        return null
    }
    get container() {
        return this._windowContainer
    }


    //大杀器 切场景调用
    public clearAll() {
        this._windows = []
        this._showlist = {};
    }
}

window['WindowManager'] = WindowManager;