#define USE_RESOURCES
//#define USE_ASSETBUNDLE
//#define USE_ADDRESSABLE

using Giant.Res;
using Giant.Resource;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using UnityEngine;
using Object = UnityEngine.Object;

public class ResourceManager : InstanceBase<ResourceManager>
{
    public const string NormalRefPath = "Refs/";
    public const string HotfixRefPath = "Generated/Refs/";

    internal bool IsCompatible(Type t1, Type t2)
    {
        return t1 == t2 || t1.IsSubclassOf(t2) || t1.IsAssignableFrom(t2);
    }

    public T Load<T>(string path, string suffix = "") where T : Object
    {
        return Load(path, typeof(T), suffix) as T;
    }



    private string GetSuffix(Type type)
    {
        string suffix = null;

        if (IsCompatible(type, typeof(GameObject)))
        {
            suffix = IResourceLoader.strPrefabSuffix;
        }
        else if (IsCompatible(type, typeof(ScriptableObject)))
        {
            suffix = IResourceLoader.strAssetSuffix;
        }
        else if (IsCompatible(type, typeof(Texture)) || IsCompatible(type, typeof(Sprite)))
        {
            suffix = IResourceLoader.strSpriteSuffix;
        }
        else if (IsCompatible(type, typeof(TextAsset)))
        {
            suffix = IResourceLoader.strBytesSuffix;
        }
        else if (IsCompatible(type, typeof(RuntimeAnimatorController)))
        {
            suffix = IResourceLoader.strAnimSuffix;
        }
        else if (IsCompatible(type, typeof(Font)))
        {
            suffix = IResourceLoader.strFontSuffix;
        }
        else if (IsCompatible(type, typeof(Mesh)))
        {
            suffix = IResourceLoader.strAssetSuffix;
        }
        else
        {
           
        }
        return suffix;
    }


    public object Load(string path, Type type, string suffix = "")
    {
        if (string.IsNullOrEmpty(suffix))
        {
            suffix = GetSuffix(type);
            if(string.IsNullOrEmpty(suffix))
            {
                throw new System.Exception("unknown resource type..." + " typename=" + type.Name + " path=" + path);
            }
        }
        //...

        return LoadImp(path, type, suffix);
    }

    private AssetRequestHandle LoadAysnc(string path,Type type,string suffix = "")
    {
        return AssetsMgr.LoadAssetAsync(path, type, suffix);
    }

    private object LoadImp(string path, Type type, string suffix = "")
    {
        return AssetsMgr.LoadAsset(path, type, suffix).asset;
        //return ResourceHelper.LoadResourceSync("", path, suffix, type);
    }

    public GameObject Instantiate(string path, bool pool = true)
    {
        //return Object.Instantiate(Load<GameObject>(path, true).Result);
        //return Object.Instantiate(Load<GameObject>(path));
        return AssetsMgr.Instantiate(path, Vector3.zero, Quaternion.identity).asset as GameObject;
    }

    public void Destory(GameObject go)
    {
        AssetsMgr.ReleaseInstance(go);
    }
}
