import { Node, Sprite, SpriteFrame } from "cc";
import { constants } from "../data/constants";
import { resManager } from "./resManager";


export class iconManager {

    private static _instance: iconManager;
    private constructor() { }
    public static get instance(): iconManager {
        if (!this._instance) {
            this._instance = new iconManager();
        }
        return this._instance;
    }

    iconsObj: any = {};

    loadAllIcons(onProgress?: Function, onComplete?: Function) {
        resManager.instance.loadAssetByBundleDir(constants.bundles.common.name, constants.bundles.common.dir.icon, SpriteFrame, (finish, total) => {
            if (onProgress) {
                onProgress(finish, total);
            }
        }, (assetArr) => {
            for (let i in assetArr) {
                let asset = assetArr[i];
                let name = asset.name;
                if (!name) {
                    continue;
                }
                this.iconsObj[name] = asset;
            }
            if (onComplete) {
                onComplete();
            }
        });
    }


    setSprite(node: Node, name: string) {
        if (!node || !name) {
            console.log("iconManager.setSprite error:", node, name);
            return;
        }
        let sp = this.iconsObj[name];
        if (!sp) {
            console.log("icon:" + name + " is null");
            return;
        }
        node.getComponent(Sprite).spriteFrame = sp;
    }

}

