import Sprite3D = Laya.Sprite3D;
import PoolManager from "./PoolManager";
import Vector4 = Laya.Vector4;

export enum RoadType{baseroad,fixedroad,things}
export enum baseroaddata{ZhiRoad}
export enum fixedroaddata{Ramps,PlateFinish}
export enum thingsdata{Obstacle1 = 1,Obstacle2 = 2,Obstacle3 = 3,Obstacle4 = 4,Obstacle5 = 5,PlateTrampoline = 6,RingBoost = 7,RingEagle = 8,PlateBend = 9,Box = 10,Box1 = 11,Box2 = 12,Coin = 13}
export enum buildtype{xrz,tt,ft}
export enum effecttype{tuowei,caidai,huo}
export enum RoleType{colorrole1,colorrole2,colorrole3,colorrole4,colorrole5,colorrole6,fruitrole1,fruitrole2,fruitrole3,fruitrole4,fruitrole5,fruitrole6}
export enum decore{hat,cibang}
export enum decoretype{colorhat1,colorhat2,colorhat3,colorhat4,colorhat5,colorhat6,colorcb1,colorcb2,colorcb3,colorcb4,colorcb5,colorcb6}

export class AssetPaths
{
    static scencepath = "subpackage/res/gamescene/LayaScene_gamescene/Conventional/gamescene.ls";

    static playerpath = 
    {
        1:"subpackage/res/playermodel/player1/Conventional/playermodel.lh",
        2:"subpackage/res/playermodel/player2/Conventional/playermodel.lh",
        3:"subpackage/res/playermodel/player3/Conventional/playermodel.lh",
        4:"subpackage/res/playermodel/player4/Conventional/playermodel.lh",
        5:"subpackage/res/playermodel/player5/Conventional/playermodel.lh",
        6:"subpackage/res/playermodel/player6/Conventional/playermodel.lh",
        7:"subpackage/res/playermodel/player7/Conventional/playermodel.lh"
    };

    static roadpath = 
    {
        1:"subpackage/res/road/baseroad/Conventional/road.lh",
        2:"subpackage/res/road/fixedroad/Conventional/road.lh",
        3:"subpackage/res/road/things/Conventional/road.lh",
    }

    //static effectpath = "subpackage/res/effect/effect/Conventional/effect.lh"

    static buildpath = "subpackage/res/builds/builds/Conventional/building.lh";

    static decoratepath = "subpackage/res/decorate/decorate/Conventional/decorate.lh";

    static planetxture = 
    [
        "subpackage/res/planetexture/1.png",
        "subpackage/res/planetexture/2.png",
        "subpackage/res/planetexture/3.png"
    ];
}

export class ResourcesManager
{
    private static instance:ResourcesManager;
    public static get Instance():ResourcesManager
    {
        if(ResourcesManager.instance)
        {
            return ResourcesManager.instance;
        }
        else
        {
            return new ResourcesManager();
        }
    }
    constructor()
    {
        ResourcesManager.instance = this;
    }

    rolecolor = 
    [
        new Vector4(137/255,67/255,299/255,1),
        new Vector4(201/255,67/255,229/255,1),
        new Vector4(228/255,69/255,114/255,1),
        new Vector4(223/255,146/255,39/255,1),
        new Vector4(207/255,61/255,62/255,1),
        new Vector4(53/255,172/255,24/255,1)
    ]

    hatcolor = 
    [
        new Vector4(9/255,119/255,183/255,1),
        new Vector4(42/255,166/255,12/255,1),
        new Vector4(96/255,72/255,20/255,1),
        new Vector4(155/255,53/255,181/255,1),
        new Vector4(234/255,104/255,127/255,1),
        new Vector4(243/255,136/255,0,1)
    ];

    public scenceasset:Laya.Scene3D;
    public FalconMesh:Sprite3D;
    public crown:Sprite3D;
    public playerassets:{[key:number]:Sprite3D} = {};
    public roleassets:{[key:number]:Sprite3D} = {};
    public decorateasset:{[key:number]:Sprite3D} = {};
    public hatasset:{[key:number]:Sprite3D} = {};
    public cbasset:{[key:number]:Sprite3D} = {};
    public bsroadassets:{[key:number]:Sprite3D} = {};
    public fxroadassets:{[key:number]:Sprite3D} = {};
    public tsroadassets:{[key:number]:Sprite3D} = {};
    //public effectassets:{[key:number]:Sprite3D} = {};
    public buildsassets:{[key:number]:Sprite3D} = {};
    public planetextures:{[key:number]:Laya.Texture2D} = {}

    InitSkinAsset()
    {
        for(var key in this.playerassets)
        {
            if(key == "1")
            {
                var role1 = this.playerassets[key];
                for(var i = 0;i < 6;i++)
                {
                    var colorrole = i == 0?this.playerassets[key]:Sprite3D.instantiate(role1);
                    var skin = colorrole.getChildAt(0).getChildAt(0).getChildAt(2) as Laya.SkinnedMeshSprite3D;
                    var materail = skin.skinnedMeshRenderer.material as Laya.BlinnPhongMaterial;
                    materail.albedoColor = this.rolecolor[i];
                    colorrole.getChildAt(0).name = RoleType[i];
                    this.roleassets[i + 1] = colorrole;
                }
            }
            else
            {
                this.roleassets[5 + Number(key)] = this.playerassets[key];
                this.roleassets[5 + Number(key)].getChildAt(0).name = RoleType[5 + Number(key)];
            }
        }

        for(var key in this.decorateasset)
        {
            if(key == "0")
            {
                var hat = this.decorateasset[key];
                for(var i = 0;i < 6;i++)
                {
                    var colorhat = i == 0?this.decorateasset[key]:Sprite3D.instantiate(hat);
                    var Sprite = colorhat.getChildAt(0) as Laya.MeshSprite3D;
                    var materail2 = Sprite.meshRenderer.material as Laya.PBRStandardMaterial;
                    materail2.albedoColor = this.hatcolor[i];
                    Sprite.name = decoretype[i];
                    this.hatasset[i + 1] = Sprite;
                }
            }
            else
            {
                var cb = this.decorateasset[key];
                for(var i = 0;i < 6;i++)
                {
                    var colorcb = i == 0?this.decorateasset[key]:Sprite3D.instantiate(cb);
                    var Sprite = colorcb.getChildAt(0) as Laya.MeshSprite3D;
                    var materai3 = Sprite.meshRenderer.material as Laya.PBRStandardMaterial;
                    materai3.albedoColor = this.rolecolor[i];
                    Sprite.name = decoretype[i + 6];
                    this.cbasset[i + 1] = Sprite;
                }
            }
        }
    }

    public LoadBSRoad(_id:number,_complete:Function)
    {
        if(this.bsroadassets[_id])
        {
            _complete(this.bsroadassets[_id]);
        }
        else
        {
            var path = AssetPaths.roadpath[_id];
            Laya.loader.create(path,Laya.Handler.create(this,()=>
            {
                var asset = Laya.loader.getRes(path.url);
                this.bsroadassets[_id] = asset;
                _complete(asset);
                Laya.loader.clearRes(path.url);
            }))
        }
    }

    public LoadFXRoad(_id:number,_complete:Function)
    {
        if(this.fxroadassets[_id])
        {
            _complete(this.fxroadassets[_id]);
        }
        else
        {
            var path = AssetPaths.roadpath[_id];
            Laya.loader.create(path,Laya.Handler.create(this,()=>
            {
                var asset = Laya.loader.getRes(path.url);
                this.fxroadassets[_id] = asset;
                _complete(asset);
                Laya.loader.clearRes(path.url);
            }))
        }
    }

    public LoadTSRoad(_id:number,_complete:Function)
    {
        if(this.tsroadassets[_id])
        {
            _complete(this.tsroadassets[_id]);
        }
        else
        {
            var path = AssetPaths.roadpath[_id];
            Laya.loader.create(path,Laya.Handler.create(this,()=>
            {
                var asset = Laya.loader.getRes(path.url);
                this.tsroadassets[_id] = asset;
                _complete(asset);
                Laya.loader.clearRes(path.url);
            }))
        }
    }

    public LoadPlayer(_id:number,_complete:Laya.Handler)
    {
        if(this.playerassets[_id])
        {
            _complete.runWith(this.playerassets[_id]);
        }
        else
        {
            var path = AssetPaths.playerpath[_id];
            Laya.loader.create(path,Laya.Handler.create(this,()=>
            {
                var asset = Laya.loader.getRes(path.url);
                this.playerassets[_id] = asset;
                _complete.runWith(asset);
                Laya.loader.clearRes(path.url);
            }))
        }
    }
}