#if UNITY_EDITOR

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

namespace mgaia.Editor
{
    /// <summary>
    /// Plugin utilizado para gerar assets de classes que herdam de ScriptableObject
    /// </summary>
    /// <owner>Victor</owner>
    public class ScriptableObjectBuilder : EditorWindow
    {
        private const string DEFAULT_SCRIPTABLE_OBJECTS_PATH = "Assets/Resources";
        private const string ASSET_FILE_NAME_FORMAT = "{0}.asset";
        private const string CREATE_BUTTON_TITLE = "Create";
        private const string TARGET_FOLDER_LABEL_TITLE = "Target folder: ";
        private const string ROOT_MENU_ITEM_TITLE = "Scriptable Object Builder/";
        private const string SHOW_CREATE_WINDOW_TITLE = "Show Create Window";
        private const string CLASS_LABEL_TITLE = "Class: ";
        private const string WINDOW_TITLE = "Create Scriptable Object Asset";

        private MonoScript mSelectedScriptableObject;
        private string mTargetFolder = DEFAULT_SCRIPTABLE_OBJECTS_PATH;

        /// <summary>
        /// Mostra a janela do editor
        /// </summary>
        [MenuItem(ROOT_MENU_ITEM_TITLE + SHOW_CREATE_WINDOW_TITLE)]
        public static void ShowWindow()
        {
            EditorWindow.GetWindow(typeof(ScriptableObjectBuilder), false, WINDOW_TITLE, true);
        }

        /// <summary>
        /// Callback do editor, desenha os componentes da GUI
        /// </summary>
        private void OnGUI()
        {
            mSelectedScriptableObject = (MonoScript)EditorGUILayout.ObjectField(CLASS_LABEL_TITLE, mSelectedScriptableObject, typeof(MonoScript), false);
            mTargetFolder = EditorGUILayout.TextField(TARGET_FOLDER_LABEL_TITLE, mTargetFolder);
            if (GUILayout.Button(CREATE_BUTTON_TITLE))
            {
                TryCreateScriptableObjectAsset();
            }
        }

        /// <summary>
        /// Tenta criar um asset para a classe escolhida no ObjectField
        /// </summary>
        private void TryCreateScriptableObjectAsset()
        {
            if (mSelectedScriptableObject != null)
            {
                Type scriptableObjectType = mSelectedScriptableObject.GetClass();

                if (scriptableObjectType.IsSubclassOf(typeof(ScriptableObject)))
                {
                    ScriptableObject scriptableObject = ScriptableObject.CreateInstance(scriptableObjectType);

                    string assetFileName = String.Format(ASSET_FILE_NAME_FORMAT, scriptableObjectType.Name);
                    string destinationPath = Path.Combine(mTargetFolder, assetFileName);
                    string finalPath = AssetDatabase.GenerateUniqueAssetPath(destinationPath);

                    AssetDatabase.CreateAsset(scriptableObject, finalPath);
                    AssetDatabase.SaveAssets();

                    Debug.Log("Scriptable Object Asset created at path: " + finalPath);
                }
                else
                {
                    Debug.LogError("Invalid type! The class must extend ScriptableObject!");
                }
            }
        }
    }
}
#endif