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

import Sprite = Laya.Sprite;
import Stage = Laya.Stage;
import GlowFilter = Laya.GlowFilter;
import Texture = Laya.Texture;
import Browser = Laya.Browser;
import Handler = Laya.Handler;
import WebGL = Laya.WebGL;

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

    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 + " 资源加载失败");
        });
    }
    public cloth = [];
    public InitAsset() {
        AssetPath.Player.forEach(element => {
            let _player = Laya.loader.getRes(element);
            Laya.loader.clearRes(element);
            this.LoadPoolRes(_player.name, _player);
        });

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

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

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

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

        AssetPath.Barrier.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.InitSelf(name, prefab);
    }

    /*************************************************************** */
    private apePath: string = "res/apes/monkey2.png";
    private ape: Sprite;

    private setup(): void {
        this.createApe();
        this.applayFilter();
    }
    private createApe(): void {
        this.ape = new Sprite();
        this.ape.loadImage(this.apePath);

        var texture: Texture = Laya.loader.getRes(this.apePath);
        this.ape.x = (Laya.stage.width - texture.width) / 2;
        this.ape.y = (Laya.stage.height - texture.height) / 2;

        Laya.stage.addChild(this.ape);
    }

    private applayFilter(): void {
        //创建一个发光滤镜
        var glowFilter: GlowFilter = new GlowFilter("#ffff00", 10, 0, 0);
        //设置滤镜集合为发光滤镜
        this.ape.filters = [glowFilter];
    }
/***************************************************************************** */

    

    public Load(callback?) {
        var arr = [
            AssetPath.MainScene,
            AssetPath.Enemy,
            AssetPath.PlayerShot,
            AssetPath.Mound,
        ];
        let arr1 = arr.concat(AssetPath.Player);
        let arr2 = arr1.concat(AssetPath.Barrier);
        let arr3 = arr2.concat(AssetPath.Particle);
     //   let arr4 = arr3.concat(AssetPath.enemytexture);
        ResourcesManager.LoadResource(arr3, () => {
            this.InitAsset();
            if (callback) callback();
        })
    }

}