using UnityEngine;
using System;
using System.Collections.Generic;
using System.IO;

namespace mgaia.Utils
{
    /// <summary>
    /// Classe que facilita o carregamento de objects contidos em uma pasta dentro da pasta Resources
    /// </summary>
    /// <owner>Victor</owner>
    public static class ResourceLoader
    {
        public enum ResourceExtension
        {
            Prefab,
            Asset
        }

        /// <summary>
        /// Carrega um Resource considerando o nome dele o nome da classe enviada via template
        /// </summary>
        public static T Load<T>(string path, ResourceExtension extension = ResourceExtension.Asset) where T : UnityEngine.Object
        {
            return Load<T>(path, typeof(T).Name);
        }

        /// <summary>
        /// Carrega um ScriptableObject com um nome customizado
        /// </summary>
        public static T Load<T>(string path, string fileName, ResourceExtension extension = ResourceExtension.Asset) where T : UnityEngine.Object
        {
#if UNITY_EDITOR
            FileInfo fileInfo = new FileInfo(fileName);

            if (!fileInfo.Extension.Equals(String.Empty))
            {
                throw new ArgumentException("fileName não pode possuir extensão!", "fileName");
            }
#endif

            if (!Application.isPlaying)
            {
#if UNITY_EDITOR
                return (T)UnityEditor.AssetDatabase.LoadAssetAtPath(PathHelper.CompletePathToAsset(fileName, extension.ToString().ToLower(), Constants.RESOURCES_FOLDER_NAME, path), typeof(T));
#else
                return null;
#endif
            }
            else
            {
                string completeFilePath = fileName;
                if (path != null && !path.Equals(String.Empty))
                {
                    completeFilePath = Path.Combine(path, completeFilePath).ToResourcesFilePath();
                }

                return Resources.Load<T>(completeFilePath);
            }
        }
    }
}