#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 void OnEnable()
        {
            _rules = (GlobalRules_SO)target;
        }

        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

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

            if (!_showBalancePreview) return;

            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");
        }

        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
