using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Ktyl.Util
{
    
    [RequireComponent(typeof(BoxCollider))]
    public partial class GameEventTrigger : MonoBehaviour
    {
        [Serializable]
        private struct Event
        {
            public GameEvent game;
            public UnityEvent unity;

            public void Raise()
            {
                if (game) game.Raise();
                unity.Invoke();
            }
        }

        [SerializeField] private Event _onEnter;
        [SerializeField] private Event _onExit;
        
        private void Start()
        {
            GetComponent<BoxCollider>().isTrigger = true;
        }

        private void OnTriggerEnter(Collider other)
        {
            if (!other.CompareTag("Player")) return;
            
            Trigger();
        }

        private void Trigger()
        {
            _onEnter.Raise();
        }
        
        private void OnTriggerExit(Collider other)
        {
            _onExit.Raise();
        }
    }
    
#if UNITY_EDITOR
    public partial class GameEventTrigger
    {
        public void EDITOR_Trigger() => Trigger();
    }
    
    [CustomEditor(typeof(GameEventListener))]
    public class GameEventTriggerEditor : Editor
    {
        private GameEventTrigger _data;

        private void OnEnable()
        {
            _data = target as GameEventTrigger;
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            if (GUILayout.Button("Trigger"))
            {
                _data.EDITOR_Trigger();
            }
        }
    }
#endif
}