﻿using UnityEditor;
using UnityEngine;

namespace BattleRoyale.Spells
{
    public class CreateNewSpellCollection : EditorWindow
    {
        public static bool defaultListAvailable;
        public string editorWindowText = "Enter Class Key: ";
        public string classKey = "";
        //int projectNumber = 1;
    
        // Characters to remove from tags
        string invalidString = " 1234567890!@#$%^&*()_=+-*/[]{};:|<>,.?`~";

        [MenuItem("Battle Royale/Advanced/Create New Spell Collection", false, 4)]
        static void CreateNewSpellCatalogWindow()
        {
            CreateNewSpellCollection window = GetWindow<CreateNewSpellCollection>("New Spell Collection");

            defaultListAvailable = IsDefaultCollectionAvailable();

            if (defaultListAvailable)
            {
                window.minSize = new Vector2(450, 162);
            }
            else
            {
                window.minSize = new Vector2(320, 308);
                Debug.Log(window.minSize.ToString());
            }

            window.maxSize = window.minSize;
        }

        //[MenuItem("Battle Royale/Advanced/Create New Spell Collection", true)]
        static bool IsDefaultCollectionAvailable()
        {
            return false; //(SpellCollection)AssetDatabase.LoadAssetAtPath("Assets/Do Not Edit OR Delete/Scriptables/Collections/DEFAULT_SpellCollection.asset", typeof(SpellCollection)) != null;
        }

        void OnGUI()
        {
            GUIStyle myStyle;

            GUILayout.Space(6); // Top Padding

            GUILayout.BeginHorizontal();    // Start Hor1
        
                GUILayout.Space(6); // Left Padding
        
                GUILayout.BeginVertical("Create a New Spell Collection", "window");
        
                    GUILayout.Space(5);

                    if (defaultListAvailable)
                    {
                        GUILayout.BeginHorizontal(GUILayout.Width(420));    // Start Hor2

                        myStyle = new GUIStyle(GUI.skin.textField);
                        myStyle.alignment = TextAnchor.MiddleCenter;
                        myStyle.fontSize = 18;
                        myStyle.fixedHeight = 30;
                        myStyle.stretchWidth = true;

                        classKey = EditorGUILayout.TextField(editorWindowText, classKey, myStyle).ToUpper();
           
                        GUILayout.EndHorizontal();  // End Hor2

                        GUILayout.Space(15);

                        myStyle = new GUIStyle(GUI.skin.label);
                        myStyle.fontStyle = FontStyle.Italic;
                        myStyle.alignment = TextAnchor.MiddleCenter;

                        EditorGUILayout.LabelField("This code will be used to distinguish between class file types.", myStyle);
                        EditorGUILayout.LabelField("Optionally, you may use any 1-6 letter code.", myStyle);

                        GUILayout.FlexibleSpace();
            
                        GUILayout.BeginHorizontal();    // Start Hor3
                        #region
                        
                            // Truncates the length of the key if it is longer than 6 letters
                            if (classKey.Length > 6)
                            {
                                string newKey = "";

                                for (int i = 0; i < 3; i++)
                                    newKey += classKey[i];

                                classKey = newKey;
                            }

                            // Removes invalid characters from submitted keys
                            if (classKey.Length > 0)
                                foreach (char c in invalidString)
                                    classKey = classKey.Replace(c.ToString(), "");

                            // Use a bolded button for the creation button
                            myStyle = new GUIStyle(GUI.skin.button);
                            myStyle.fontStyle = FontStyle.Bold;
                            myStyle.stretchWidth = true;

                            // Generates a new SpellList ScriptableObject based on the default setup
                            if (GUILayout.Button("Create Spell Collection", myStyle, GUILayout.Height(50), GUILayout.Width(position.width - 20)))
                            {
                                // If the class key is empty, return
                                if (classKey.Length == 0)
                                    return;

                                // Default and destination paths for the new SpellList
                                string defaultPath = "Assets/Do Not Edit OR Delete/Scriptables/Collections/DEFAULT_SpellCollection.asset";
                                string newPath = "Assets/SpellCollections/" + classKey + "_SpellCollection.asset";

                                // If a SpellList asset with the same class key already exists, ask if the user wants to overwrite that asset.
                                if ((SpellList)AssetDatabase.LoadAssetAtPath(newPath, typeof(BattleRoyale.Spells.SpellCollection)))
                                    if (!EditorUtility.DisplayDialog("Overwrite Existing Spell Collection", "A Spell Collection with the key " + classKey + " already exists. Overwriting will reset the existing custom Spell Collection data. Continue?", "Continue"))
                                        return;

                                // Make a copy of the default at the new path and close
                                AssetDatabase.CopyAsset(defaultPath, newPath);
                                this.Close();
                            }

                            GUILayout.FlexibleSpace();

                        #endregion
                        GUILayout.EndHorizontal();      // End Hor3
                    }
                    else
                    {
                        GUILayout.Label("Uh oh! We couldn't locate the Default Spell Collection.", EditorStyles.wordWrappedLabel);

                        GUILayout.Space(10);

                        GUILayout.Label("Make sure the default Spell Collection 'DEFAULT_SpellCollection.asset' is placed in the 'Assets/Do Not Edit Or Delete/Scriptables/Collections directory in the project files.", EditorStyles.wordWrappedLabel);

                        GUILayout.Space(10);

                        GUILayout.Label("You can access the folder by clicking 'Window' at the top of your screen, then 'General -> Project'. The 'Collections' folder can be found in the default 'Assets' directory under 'Do Not Edit Or Delete/Scriptables'.", EditorStyles.wordWrappedLabel);

                        GUILayout.Space(10);

                        GUILayout.Label("Ask your instructor for help locating your Default Spell Collection, or you may contact one of our Tech Support agents by clicking the button below:", EditorStyles.wordWrappedLabel);

                        GUILayout.Space(10);

                        if (GUILayout.Button("Black Rocket Support"))
                        {
                            Application.OpenURL("https://www.blackrocket.com/help");
                        }
                    }
        
                GUILayout.EndVertical();    // End Ver1

                GUILayout.Space(6); // Right padding
        
            GUILayout.EndHorizontal();
            GUILayout.Space(6); // Bottom Padding
        }
    }
}