import { EResType } from "./ResBaseAsset";
import UEBase from "./UEBase";

/**
 * 资源定义 bundle + url
 */
export interface IBundleUrl {
    typ?: EResType,
    bundle: string,
    url: string
}

export interface LoadingAssetItem {
    typ: EResType,
    data: IBundleUrl
}


declare global {
    interface IResCollector {
        AddList(tpe: EResType, list: IBundleUrl[]): void;
        AddUEClass(list: UEClass<UEBase>[]): void;
        AddAudio(bundleUrl: IBundleUrl): void;
        AddImageAsset(bundleUrl: IBundleUrl): void;
        AddMaterial(bundleUrl: IBundleUrl): void;
        AddPrefabUrl(bundleUrl: IBundleUrl): void;
        AddSpine(bundleUrl: IBundleUrl): void;
        AddSpriteFrame(bundleUrl: IBundleUrl): void;
        AddSpriteAtlas(bundleUrl: IBundleUrl): void;
        AddTexture2D(bundleUrl: IBundleUrl): void;
        AddTTFFont(bundleUrl: IBundleUrl): void
        AddText(bundleUrl: IBundleUrl): void;
        AddTypList(items: IBundleUrl[]): void;
    }
}

export class ResCollector implements IResCollector {

    private m_LoadAssets: Array<LoadingAssetItem>;

    constructor() {
        this.m_LoadAssets = [];
    }

    /**
        * 加载 UE 类（实际是加载对应预制资源）
        * @param list 
        * @returns 
        */
    AddUEClass(list: UEClass<UEBase>[]): void {
        list.forEach(cls => {
            this.AddListItem(EResType.Prefab, {
                bundle: cls.BundleKey,
                url: cls.PrefabUrl
            });
        });
    }



    AddAudio(bundleUrl: IBundleUrl): void {
        this.AddListItem(EResType.Audio, bundleUrl);
    }


    /** 加载ImageAsset图片 */
    AddImageAsset(bundleUrl: IBundleUrl): void {
        this.AddListItem(EResType.ImageAsset, bundleUrl);
    }

    /** 加载 material */
    AddMaterial(bundleUrl: IBundleUrl): void {
        this.AddListItem(EResType.Material, bundleUrl);
    }

    /** 通过预制 Url 加载 */
    AddPrefabUrl(bundleUrl: IBundleUrl): void {
        this.AddListItem(EResType.Prefab, bundleUrl);
    }

    /** 加载spine */
    AddSpine(bundleUrl: IBundleUrl): void {
        this.AddListItem(EResType.Spine, bundleUrl);
    }

    /** 加载精灵图片 */
    AddSpriteFrame(bundleUrl: IBundleUrl): void {
        this.AddListItem(EResType.SpriteFrame, bundleUrl);
    }

    /** 加载精灵图片 */
    AddSpriteAtlas(bundleUrl: IBundleUrl): void {
        this.AddListItem(EResType.SpriteAtlas, bundleUrl);
    }

    /** 加载Texture2D图片 */
    AddTexture2D(bundleUrl: IBundleUrl): void {
        this.AddListItem(EResType.Texture2D, bundleUrl);
    }

    AddTTFFont(bundleUrl: IBundleUrl): void {
        this.AddListItem(EResType.TTFFont, bundleUrl);
    }
    AddText(bundleUrl: IBundleUrl): void {
        this.AddListItem(EResType.Text, bundleUrl);
    }

    /**
     * 通用接口，添加数据类型
     * @param typ 
     * @param items 
     * @returns 
     */
    public AddList(typ: EResType, items: IBundleUrl[]): void {
        items.forEach(it => this.AddListItem(typ, it));
    }

    /**
     * 通用接口，增加加载项
     */
    AddListItem(typ: EResType, it: IBundleUrl) {
        const that = this;
        if (!that.m_LoadAssets) {
            that.m_LoadAssets = [];
        }
        that.m_LoadAssets.push({ typ: typ, data: it });
    }

    public AddTypList(items: IBundleUrl[]): void {
        items.forEach(it => {
            if (it.typ == EResType.SpriteFrame) {
                this.AddSpriteFrame(it);
            } else if (it.typ == EResType.Spine) {
                this.AddSpine(it);
            }
        });
    }



    GetResList() {
        return this.m_LoadAssets;
    }
}
