#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using IStalk.Data;

namespace IStalk.Editor.Inspectors
{
    [CustomEditor(typeof(GlobalRules_SO))]
    public class GlobalRulesEditor : UnityEditor.Editor
    {
        private GlobalRules_SO _rules;
        private bool _showBalancePreview = true;
        private bool _showEyeTypes = true;
        private bool _showWearables = true;
        private bool _showCoop = true;
        private bool _showExtraction = true;

        private void OnEnable()
        {
            _rules = (GlobalRules_SO)target;
        }

        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            EditorGUILayout.Space(15);
            _showBalancePreview = EditorGUILayout.Foldout(_showBalancePreview, "Balance Preview", true);

            if (_showBalancePreview)
            {
                EditorGUILayout.LabelField("Weight Tiers", EditorStyles.boldLabel);
                DrawTierRow("Naked", 0, _rules.weightThresholdLight, _rules.speedMultNaked);
                DrawTierRow("Light", _rules.weightThresholdLight, _rules.weightThresholdModerate, _rules.speedMultLight);
                DrawTierRow("Moderate", _rules.weightThresholdModerate, _rules.weightThresholdHeavy, _rules.speedMultModerate);
                DrawTierRow("Heavy", _rules.weightThresholdHeavy, _rules.weightThresholdOverloaded, _rules.speedMultHeavy);
                DrawTierRow("Overloaded", _rules.weightThresholdOverloaded, float.MaxValue, _rules.speedMultOverloaded);

                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Movement at Base Speed", EditorStyles.boldLabel);
                EditorGUILayout.LabelField($"  Walk: {_rules.baseRunSpeed:F1} m/s");
                EditorGUILayout.LabelField($"  Sprint: {_rules.baseRunSpeed * _rules.sprintMultiplier:F1} m/s");
                EditorGUILayout.LabelField($"  Slide: {_rules.slideSpeed:F1} m/s");
                EditorGUILayout.LabelField($"  Overloaded Walk: {_rules.baseRunSpeed * _rules.speedMultOverloaded:F1} m/s");

                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Extraction Economy", EditorStyles.boldLabel);
                EditorGUILayout.LabelField($"  Threshold: {_rules.extractionThreshold} loot value");
                EditorGUILayout.LabelField($"  Min loot in map: ~{_rules.extractionThreshold * _rules.lootValueToThresholdRatio:F0} value");
                EditorGUILayout.LabelField($"  Channel time (empty): {_rules.altarChannelBaseTime:F1}s");
                EditorGUILayout.LabelField($"  Channel time (20kg): {_rules.GetChannelTime(20):F1}s");

                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Map Generation", EditorStyles.boldLabel);
                EditorGUILayout.LabelField($"  Rooms: {_rules.minRoomsPerMap}–{_rules.maxRoomsPerMap}");
                EditorGUILayout.LabelField($"  Floors: {_rules.minFloors}–{_rules.maxFloors}");
                EditorGUILayout.LabelField($"  Altars: {_rules.minAltars}–{_rules.maxAltars}");
                EditorGUILayout.LabelField($"  Map area (max): ~{_rules.maxRoomsPerMap * _rules.cellWidth * _rules.cellWidth:F0}m²");

                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Match Timing", EditorStyles.boldLabel);
                EditorGUILayout.LabelField($"  Grace: {_rules.gracePeriodDuration}s");
                EditorGUILayout.LabelField($"  Hunt: {_rules.matchTimeLimit / 60f:F1} min");
                EditorGUILayout.LabelField($"  Total max: {(_rules.gracePeriodDuration + _rules.matchTimeLimit) / 60f:F1} min");
            }

            EditorGUILayout.Space(10);
            _showEyeTypes = EditorGUILayout.Foldout(_showEyeTypes, "Eye Type Balance", true);
            if (_showEyeTypes)
            {
                EditorGUILayout.LabelField("Flash/Eyelid System", EditorStyles.boldLabel);
                EditorGUILayout.LabelField($"  Flash blind duration: {_rules.flashDuration:F1}s");
                EditorGUILayout.LabelField($"  Team flash cooldown: {_rules.flashTeamCooldown:F1}s");

                EditorGUILayout.Space(3);
                EditorGUILayout.LabelField("Watcher", EditorStyles.boldLabel);
                EditorGUILayout.LabelField($"  Scan cooldown: x{_rules.watcherScanCooldownMultiplier:F2} (faster)");
                EditorGUILayout.LabelField($"  Scan radius: x{_rules.watcherScanRadiusMultiplier:F2} (wider)");

                EditorGUILayout.Space(3);
                EditorGUILayout.LabelField("Predator", EditorStyles.boldLabel);
                EditorGUILayout.LabelField($"  Laser cooldown: x{_rules.predatorLaserCooldownMultiplier:F2} (faster)");
                EditorGUILayout.LabelField($"  Laser radius: x{_rules.predatorLaserRadiusMultiplier:F2} (wider)");

                EditorGUILayout.Space(3);
                EditorGUILayout.LabelField("Trapper", EditorStyles.boldLabel);
                EditorGUILayout.LabelField($"  Trap cost: x{_rules.trapperDreadCostMultiplier:F2} (cheaper)");
                EditorGUILayout.LabelField($"  Max active bonus: +{_rules.trapperMaxActiveBonus}");
            }

            EditorGUILayout.Space(10);
            _showWearables = EditorGUILayout.Foldout(_showWearables, "Wearable Balance", true);
            if (_showWearables)
            {
                EditorGUILayout.LabelField($"  Max slots: {_rules.maxWearableSlots}");
                EditorGUILayout.LabelField($"  Dash cooldown: {_rules.dashCooldown:F1}s");
                EditorGUILayout.LabelField($"  Air Dash cooldown: {_rules.airDashCooldown:F1}s");
                EditorGUILayout.LabelField($"  Grapple cooldown: {_rules.grappleCooldown:F1}s");
                EditorGUILayout.LabelField($"  Throw range: {_rules.throwRangeBase:F1}m (weight scale: {_rules.throwRangeWeightScale:F2})");
            }

            EditorGUILayout.Space(10);
            _showCoop = EditorGUILayout.Foldout(_showCoop, "Co-op Balance", true);
            if (_showCoop)
            {
                EditorGUILayout.LabelField($"  Boost height: {_rules.boostHeight:F1}m (range: {_rules.boostRange:F1}m)");
                EditorGUILayout.LabelField($"  Linked Sprint bonus: +{_rules.linkedSprintBonus:P0} speed");
                EditorGUILayout.LabelField($"  Linked Sprint radius: {_rules.linkedSprintRadius:F1}m");
                EditorGUILayout.LabelField($"  Linked Sprint slow on break: {_rules.linkedSprintSlowDuration:F1}s");
            }

            EditorGUILayout.Space(10);
            _showExtraction = EditorGUILayout.Foldout(_showExtraction, "Extraction Room", true);
            if (_showExtraction)
            {
                EditorGUILayout.LabelField($"  Channel duration: {_rules.extractionChannelDuration:F0}s (all runners present)");
                EditorGUILayout.LabelField($"  Anti-AFK ritual: {_rules.antiAfkRitualDuration:F0}s");
                EditorGUILayout.LabelField($"  Min voters to start ritual: {_rules.antiAfkMinVoters}");
            }
        }

        private void DrawTierRow(string name, float min, float max, float speedMult)
        {
            string range = max == float.MaxValue ? $"{min:F1}kg+" : $"{min:F1}–{max:F1}kg";
            EditorGUILayout.LabelField($"  {name}: {range} → {speedMult:P0} speed");
        }
    }
}
#endif
