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

namespace IntelligentProjectAnalyzer.Editor
{
    // This class registers the settings provider, which is what makes the settings
    // appear in the Project Settings window.
    internal static class AnalyzerSettingsProvider
    {
        private static SerializedObject _mSettings;

        [SettingsProvider]
        public static SettingsProvider CreateAnalyzerSettingsProvider()
        {
            var provider = new SettingsProvider("Project/Project AssetDatabase/Analyzer Settings", SettingsScope.Project)
            {
                label = "Analyzer Settings",
                guiHandler = GuiDraw,
                keywords = new HashSet<string>(new[] { "Analyzer", "Dependency", "Roslyn", "System", "Types" })
            };

            return provider;
        }

        private static void GuiDraw(string _)
        {
            _mSettings ??= new SerializedObject(AnalyzerSettingsCrud.GetOrCreateSettings());

            EditorGUILayout.HelpBox("This settings is used to determine which types are system types, when the code is analyzed to find the dependencies till it reaches the root (e.g., 'System', 'UnityEngine', 'UnityEditor').", MessageType.Info);
            EditorGUILayout.LabelField("Default System Types", EditorStyles.boldLabel);
            
            EditorGUI.BeginDisabledGroup(true);
            EditorGUILayout.TextField("System");
            EditorGUILayout.TextField("UnityEngine");
            EditorGUILayout.TextField("UnityEditor");
            EditorGUI.EndDisabledGroup();

            EditorGUILayout.Space();

            EditorGUILayout.LabelField("Custom System Types", EditorStyles.boldLabel);
            EditorGUILayout.HelpBox("Add additional namespace prefixes to be ignored by the analyzer (e.g., 'MyCompany.CoreLib').", MessageType.Info);
            
            EditorGUILayout.PropertyField(_mSettings.FindProperty("mCustomSystemTypes"), new GUIContent("Ignored Namespaces"), true);
            
            EditorGUILayout.Space();
            
            EditorGUILayout.LabelField("Custom Analysis Rules", EditorStyles.boldLabel);
            EditorGUILayout.HelpBox("Choose which type of unity types to analyze.", MessageType.Info);
            
            EditorGUILayout.PropertyField(_mSettings.FindProperty("mAnalyzeScripts"), new GUIContent("Analyze Scripts"));
            EditorGUILayout.PropertyField(_mSettings.FindProperty("mAnalyzePrefabs"), new GUIContent("Analyze Prefabs"));
            EditorGUILayout.PropertyField(_mSettings.FindProperty("mAnalyzeScriptableObjects"), new GUIContent("Analyze Scriptable Objects"));
            EditorGUILayout.PropertyField(_mSettings.FindProperty("mAnalyzeMiscellaneous"), new GUIContent("Analyze Other Assets"));

            if (_mSettings.hasModifiedProperties)
            {
                _mSettings.ApplyModifiedProperties();
            }
        }
    }
}