import AssetPath from "./AssetManager";
import Sprite3D = Laya.Sprite3D;
import Scene3D = Laya.Scene3D;
import { GamePool } from "./GamePool";
import { Scene3DManager } from "./Scene3DManager";

export default class AssetController {
    private static instance: AssetController;
    public static get Instance(): AssetController {
        return this.instance || (this.instance = new AssetController());
    }
    public LoadAsset(callback?) {
        var arr = [
            AssetPath.main_path,
            AssetPath.enemy_path,
            AssetPath.playerBullet_path,
            AssetPath.tower_path,

        ];
        let arr1 = arr.concat(AssetPath.player_path);
        let arr2 = arr1.concat(AssetPath.barrier_paths);
        let arr3 = arr2.concat(AssetPath.effect_path);
        let arr4 = arr3.concat(AssetPath.enemytexture);
        AssetController.LoadResource(arr4, () => {
            this.InitAsset();
            if (callback) callback();
        })
    }

    public cloth = [];
    public InitAsset() {
        AssetPath.player_path.forEach(element => {
            let _player = Laya.loader.getRes(element);
            Laya.loader.clearRes(element);
            this.LoadPoolRes(_player.name, _player);
        });

        AssetPath.enemytexture.forEach(element => {
            let _tex = Laya.loader.getRes(element);
            Laya.loader.clearRes(element);
            this.cloth.push(_tex);
        });

        AssetPath.effect_path.forEach(element => {
            let _effect = Laya.loader.getRes(element);
            Laya.loader.clearRes(element);
            this.LoadPoolRes(_effect.name, _effect);
        });

        let _enemy = Laya.loader.getRes(AssetPath.enemy_path);
        Laya.loader.clearRes(AssetPath.enemy_path);
        this.LoadPoolRes(_enemy.name, _enemy);

        let _playerbullet = Laya.loader.getRes(AssetPath.playerBullet_path);
        Laya.loader.clearRes(AssetPath.playerBullet_path);
        this.LoadPoolRes(_playerbullet.name, _playerbullet);

        let _tower = Laya.loader.getRes(AssetPath.tower_path);
        Laya.loader.clearRes(AssetPath.tower_path);
        this.LoadPoolRes(_tower.name, _tower);
        let _scene = Laya.loader.getRes(AssetPath.main_path);
        Laya.loader.clearRes(AssetPath.main_path);

        AssetPath.barrier_paths.forEach(element => {
            let _barrier = Laya.loader.getRes(element);
            Laya.loader.clearRes(element);
            this.LoadPoolRes(_barrier.name, _barrier);
        });

        Scene3DManager.Instance.Init(_scene);
    }
    LoadPoolRes(name, prefab) {
        GamePool.Instance.InitData(name, prefab);
    }


    public static LoadResource(path: Array<any>, complet?: Function) {
        Laya.loader.retryNum = 2;
        Laya.loader.maxLoader = 3;
        Laya.loader.retryDelay = 100;
        Laya.loader.create(path, new Laya.Handler(this, complet));
        Laya.loader.on(Laya.Event.ERROR, this, (data) => {
            console.log(data + " 资源加载失败");
        });
    }

}