using UnityEditor;
using UnityEngine;

namespace AlterchaseSDK.Editor
{
    [InitializeOnLoad]
    public class WelcomeWindow : EditorWindow
    {
        private const string PREF_KEY_SHOWN = "AlterchaseSDK_WelcomeShown";
        private const string PREF_KEY_GAME_PATH = "AlterchaseSDK_GamePath";
        private string gameRootPath = "";

        static WelcomeWindow()
        {
            EditorApplication.delayCall += ShowWelcomeWindowOnFirstLoad;
        }

        private static void ShowWelcomeWindowOnFirstLoad()
        {
            bool hasShown = EditorPrefs.GetBool(PREF_KEY_SHOWN, false);
            
            if (!hasShown)
            {
                EditorPrefs.SetBool(PREF_KEY_SHOWN, true);
                ShowWindow();
            }
        }

        [MenuItem("Alterchase SDK/Welcome", priority = 0)]
        public static void ShowWindow()
        {
            WelcomeWindow window = GetWindow<WelcomeWindow>("Alterchase SDK");
            window.minSize = new Vector2(500, 300);
            window.maxSize = new Vector2(500, 300);
            window.Show();
        }

        private void OnEnable()
        {
            gameRootPath = EditorPrefs.GetString(PREF_KEY_GAME_PATH, "");
        }

        private void OnGUI()
        {
            DrawHeader();
            
            GUILayout.Space(20);
            
            DrawBody();
            
            GUILayout.FlexibleSpace();
            
            DrawBrowseSection();
        }

        private void DrawHeader()
        {
            GUILayout.Space(20);
            
            GUIStyle headerStyle = new GUIStyle(EditorStyles.boldLabel)
            {
                fontSize = 24,
                alignment = TextAnchor.MiddleCenter,
                wordWrap = true
            };
            
            EditorGUILayout.LabelField("Welcome to the Alterchase SDK!", headerStyle);
            
            GUILayout.Space(10);
            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
        }

        private void DrawBody()
        {
            GUILayout.Space(10);
            
            GUIStyle bodyStyle = new GUIStyle(EditorStyles.label)
            {
                fontSize = 14,
                alignment = TextAnchor.UpperCenter,
                wordWrap = true
            };
            
            EditorGUILayout.LabelField("Please navigate to your Alterchase mods folder.", bodyStyle);
            
            GUILayout.Space(20);
            
            if (!string.IsNullOrEmpty(gameRootPath))
            {
                EditorGUILayout.BeginVertical("box");
                EditorGUILayout.LabelField("Current Game Root:", EditorStyles.boldLabel);
                EditorGUILayout.SelectableLabel(gameRootPath, EditorStyles.wordWrappedLabel, GUILayout.Height(40));
                EditorGUILayout.EndVertical();
            }
        }

        private void DrawBrowseSection()
        {
            GUILayout.Space(20);
            
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            
            if (GUILayout.Button("Browse for Alterchase Game Root", GUILayout.Height(35), GUILayout.Width(300)))
            {
                BrowseForGameRoot();
            }
            
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
            
            GUILayout.Space(20);
        }

        private void BrowseForGameRoot()
        {
            string selectedPath = EditorUtility.OpenFolderPanel(
                "Select Alterchase Game Root Folder",
                gameRootPath,
                ""
            );

            if (!string.IsNullOrEmpty(selectedPath))
            {
                gameRootPath = selectedPath;
                EditorPrefs.SetString(PREF_KEY_GAME_PATH, gameRootPath);
                
                Debug.Log($"Alterchase game root set to: {gameRootPath}");
                
                EditorUtility.DisplayDialog(
                    "Path Saved",
                    $"Alterchase game root has been set to:\n\n{gameRootPath}",
                    "OK"
                );
            }
        }

        [MenuItem("Alterchase SDK/Reset Welcome Window", priority = 100)]
        private static void ResetWelcomePreference()
        {
            EditorPrefs.DeleteKey(PREF_KEY_SHOWN);
            Debug.Log("Welcome window preference reset. It will show again on next Unity launch.");
        }
    }
}
