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

namespace IStalk.Editor.Inspectors
{
    [CustomEditor(typeof(TrapData_SO))]
    public class TrapDataEditor : UnityEditor.Editor
    {
        private TrapData_SO _trap;

        private void OnEnable()
        {
            _trap = (TrapData_SO)target;
        }

        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            EditorGUILayout.Space(10);
            EditorGUILayout.LabelField("Balance Preview", EditorStyles.boldLabel);

            EditorGUILayout.LabelField($"Cost/Duration ratio: {(_trap.effectDuration > 0 ? _trap.dreadCost / _trap.effectDuration : 0):F1} dread/sec");
            EditorGUILayout.LabelField($"Area coverage: {Mathf.PI * _trap.effectRadius * _trap.effectRadius:F1}m²");
            EditorGUILayout.LabelField($"Trigger-to-arm delay: {_trap.armDelay:F1}s");

            EditorGUILayout.Space();
            if (GUILayout.Button("Generate ID from Name"))
            {
                Undo.RecordObject(_trap, "Generate Trap ID");
                _trap.trapId = _trap.name.Replace(" ", "_").ToLower();
                EditorUtility.SetDirty(_trap);
            }

            DrawValidation();
        }

        private void DrawValidation()
        {
            EditorGUILayout.Space();
            if (string.IsNullOrEmpty(_trap.trapId))
                EditorGUILayout.HelpBox("Trap ID is empty.", MessageType.Error);
            if (_trap.placedPrefab == null)
                EditorGUILayout.HelpBox("No placed prefab assigned.", MessageType.Warning);
            if (_trap.dreadCost <= 0)
                EditorGUILayout.HelpBox("Dread cost is 0.", MessageType.Warning);
        }
    }

    [CustomEditor(typeof(ShrineItemData_SO))]
    public class ShrineItemDataEditor : UnityEditor.Editor
    {
        private ShrineItemData_SO _item;

        private void OnEnable()
        {
            _item = (ShrineItemData_SO)target;
        }

        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            EditorGUILayout.Space(10);
            EditorGUILayout.LabelField("Balance Preview", EditorStyles.boldLabel);

            EditorGUILayout.LabelField($"Net weight change: {(_item.weightDelta >= 0 ? "+" : "")}{_item.weightDelta:F1}kg");
            EditorGUILayout.LabelField($"Cost/Effect ratio: {(_item.effectDuration > 0 ? _item.lootCost / _item.effectDuration : 0):F1}");
            if (_item.cooldown > 0)
                EditorGUILayout.LabelField($"Uptime: {_item.effectDuration / (_item.effectDuration + _item.cooldown):P0}");

            EditorGUILayout.Space();
            if (GUILayout.Button("Generate ID from Name"))
            {
                Undo.RecordObject(_item, "Generate Shrine Item ID");
                _item.itemId = _item.name.Replace(" ", "_").ToLower();
                EditorUtility.SetDirty(_item);
            }

            if (string.IsNullOrEmpty(_item.itemId))
                EditorGUILayout.HelpBox("Item ID is empty.", MessageType.Error);
        }
    }
}
#endif
