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

namespace Jovian.PopupSystem.Editor {
    public sealed class PopupSettingsProvider : SettingsProvider {
        private PopupSettings settings;

        private PopupSettingsProvider(string path, SettingsScope scope)
            : base(path, scope) { }

        private SerializedObject serializedSettings;

        public override void OnActivate(string searchContext, UnityEngine.UIElements.VisualElement rootElement) {
            var guids = AssetDatabase.FindAssets("t:PopupSettings");
            if(guids.Length > 0) {
                var path = AssetDatabase.GUIDToAssetPath(guids[0]);
                settings = AssetDatabase.LoadAssetAtPath<PopupSettings>(path);
            }
            if(settings != null) {
                serializedSettings = new SerializedObject(settings);
            }
        }

        public override void OnGUI(string searchContext) {
            if(settings == null) {
                EditorGUILayout.HelpBox("No PopupSettings asset found. Create one via Assets > Create > Jovian > Popup System > Popup Settings.", MessageType.Warning);
                return;
            }

            EditorGUILayout.Space(10);
            EditorGUILayout.LabelField("Popup System Settings", EditorStyles.boldLabel);
            EditorGUILayout.Space(5);

            serializedSettings.Update();
            EditorGUI.BeginChangeCheck();

            EditorGUILayout.PropertyField(serializedSettings.FindProperty("popupDelay"));
            EditorGUILayout.PropertyField(serializedSettings.FindProperty("fadeDuration"));
            EditorGUILayout.PropertyField(serializedSettings.FindProperty("defaultAnchorSide"));
            EditorGUILayout.PropertyField(serializedSettings.FindProperty("screenEdgePadding"));
            EditorGUILayout.PropertyField(serializedSettings.FindProperty("maxPopupWidth"));
            EditorGUILayout.PropertyField(serializedSettings.FindProperty("sortingOrder"));

            EditorGUILayout.Space(5);
            EditorGUILayout.LabelField("Follow Mouse", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(serializedSettings.FindProperty("followMouseOffset"));

            EditorGUILayout.Space(5);
            EditorGUILayout.LabelField("Input", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(serializedSettings.FindProperty("touchHoldDuration"));
            EditorGUILayout.PropertyField(serializedSettings.FindProperty("gamepadFocusTrigger"));

            EditorGUILayout.Space(5);
            EditorGUILayout.LabelField("Element Prefabs", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(serializedSettings.FindProperty("elementPrefabs"), true);

            EditorGUILayout.Space(5);
            EditorGUILayout.LabelField("Priority", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(serializedSettings.FindProperty("categoryPriorities"), true);

            EditorGUILayout.Space(5);
            EditorGUILayout.LabelField("Per-Category Overrides", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(serializedSettings.FindProperty("categoryDelayOverrides"), true);

            if(EditorGUI.EndChangeCheck()) {
                serializedSettings.ApplyModifiedProperties();
                EditorUtility.SetDirty(settings);
            }
        }

        [SettingsProvider]
        public static SettingsProvider CreateProvider() {
            var provider = new PopupSettingsProvider("Project/Jovian/Popup System", SettingsScope.Project) {
                keywords = new HashSet<string>(new[] { "popup", "tooltip", "hover", "delay" })
            };
            return provider;
        }
    }
}
