import { AssetPath } from "./AssetPath";
import { Global } from "../Global";

/*
* name;
*/
export class RecConfigs extends Laya.EventDispatcher
{
    private static ins:RecConfigs;

    public ResDic:any;
    /**
     * 是否已经初始化
     */
    public static inited:boolean = false;

    public static get Instance():RecConfigs
    {
        if( RecConfigs.ins == null )
        {
            return new RecConfigs();
        }
        return RecConfigs.ins;
    }
    constructor()
    {
        super();
        RecConfigs.ins = this;
    }

    public init():void
    {
        this.ResDic = {};
    }

    /****************************************************************** */
    private loadUI():void {
        Laya.loader.load(["res/threeDimen/ui/button.png"], Laya.Handler.create(this, function():void {
            var changeActionButton:Laya.Button = Laya.stage.addChild(new Laya.Button("res/threeDimen/ui/button.png", "正常模式")) as Laya.Button;
            changeActionButton.size(160, 40);
            changeActionButton.labelBold = true;
            changeActionButton.labelSize = 30;
            changeActionButton.sizeGrid = "4,4,4,4";
            changeActionButton.scale(Laya.Browser.pixelRatio, Laya.Browser.pixelRatio);
            changeActionButton.pos(Laya.stage.width / 2 - changeActionButton.width * Laya.Browser.pixelRatio / 2, Laya.stage.height - 100 * Laya.Browser.pixelRatio);
            changeActionButton.on(Laya.Event.CLICK, this, function():void {
                if (++this.curStateIndex % 2 == 1) {
                    this.sprite3D.active = false;
                    this.lineSprite3D.active = true;
                    changeActionButton.label = "网格模式";
                } else {
                    this.sprite3D.active = true;
                    this.lineSprite3D.active = false;
                    changeActionButton.label = "正常模式";
                }
            });
        }));
    }
    /****************************************************************** */

    public initRec( _res:any ):void
    {
       if( _res != null && !RecConfigs.inited )
       {
            RecConfigs.inited = true;
            var groupsArr:Array<any> = _res.groups;
            var resourcesArr:Array<any> = _res.resources;
            var resourcesDict:any = {};
            for( var item in resourcesArr )
            {
                var child:any = resourcesArr[item];
                switch( child.type )
                {
                    case "bin":
                        child.type = Laya.Loader.BUFFER;
                    break;
                    case "ls":
                        child.type = Laya.Scene;
                    break;
                    case "lh":
                        child.type = Laya.Sprite3D;
                    break;
                    case "lm":
                        child.type = Laya.MeshSprite3D;
                    break;
                }
                resourcesDict[child.name] = child;
            }
            for( var grouptype in groupsArr )
            {
                var groupchild:any = groupsArr[grouptype];
                var arr:Array<string> = groupchild.keys.split( "," );
                var childArr:Array<any> = [];
                var chidstr;
                for( chidstr in arr )
                {
                    var strChild:string = arr[chidstr];
                    if( strChild != "" )
                    {
                        var urls:string = resourcesDict[strChild].url;
                        urls = urls.replace("res/",AssetPath.res);
                        childArr.push( { url:Global.getUrl(urls), type:resourcesDict[strChild].type } );
                    }
                }
                this.ResDic[groupchild.name] = childArr;
            }
            Laya.loader.clearRes(AssetPath.getResConfig());
       }
    }



}