// 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


export default class LayerManager {
    /**
     * 层次根节点
     */
    private root:cc.Node;

    /**
     * 层次
     */
    private layers:cc.Node[];
    
    constructor(){
        
    }

    /**
     * 初始化
     * @param root 
     * @param layerCount 
     */
    Init(root:cc.Node):void{
        this.root=root;
        let layer:cc.Node;
        this.layers=[];
        for (let index = 0; index < this.root.childrenCount; index++) {
            this.layers[index]= this.root.children[index];
        }
    }

    /**
     * 添加内容到层
     * @param layerIndex 
     * @param node 
     */
    AddToLayer(layerIndex:number,node:cc.Node):void{
        let layer:cc.Node=this.layers[layerIndex];
        if(layer==null){
            throw new Error("找不到："+layerIndex+"层！");
        }
        layer.addChild(node);
    }

    /**
     * 从层上删除内容
     * @param layerIndex 
     * @param node 
     */
    RemoveFormeLayer(layerIndex:number,node:cc.Node):void{
        let layer:cc.Node=this.layers[layerIndex];
        if(layer==null){
            throw new Error("找不到："+layerIndex+"层！");
        }
        layer.removeChild(node);
    }

    /**
     * 通过层索引获取层
     * @param layerIndex 
     */
    GetLayer(layerIndex:number){
        let layer:cc.Node=this.layers[layerIndex];
        if(layer==null){
            throw new Error("找不到："+layerIndex+"层！");
        }
        return layer;
    }

    /**
     * 单例
     */
    private static instance:LayerManager;
    public static get single():LayerManager{
        if(this.instance==null){
            this.instance=new LayerManager();
        }
        return this.instance;
    }
}