using System;
using System.Collections.Generic;
using Intrepid.Core.Camera.Events;
using Intrepid.Core.Entities;
using Intrepid.Core.Tags;
using Intrepid.GameManagers;
using Intrepid.Utilities;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Intrepid.Core.Camera
{
    [RequireComponent(typeof(SphereCollider))]
    public class CameraProximityTrigger : EntityTrigger
    {
        [GroupConfigurations, SerializeField, Range(.5f, 5f)] private float radius = 1.5f;
        [GroupConfigurations, SerializeField] private CameraZoomLevel zoomLevel = CameraZoomLevel.ZoomIn;
        [GroupConfigurations, SerializeField, Range(.05f, 3f)] private float enterZoomVelocity = .5f;
        [GroupConfigurations, SerializeField, Range(.05f, 3f)] private float exitZoomVelocity = .5f;

        public SphereCollider Collider => (SphereCollider) MainCollider;
        public float Radius
        {
            get => radius;
            set
            {
                if (Collider.IsNotNull()) Collider.radius = value;
                radius = value;
            }
        }

        [GroupDebug, Button(ButtonSizes.Medium)]
        private void AdjustCollider()
        {
            if (Collider.IsNull()) return;
            Collider.isTrigger = true;
            Collider.radius = radius;
        }

        protected override void WhenRaiseEnterEvent(Collider other, EntitySensor entitySensor, List<BehaviourTag> behaviourTags)
        {
            Proxy.Instance.EventBus.Raise(new CameraZoomEvent
            {
                ZoomLevel = zoomLevel,
                ZoomVelocity = enterZoomVelocity
            });
        }

        protected override void WhenRaiseExitEvent(Collider other, EntitySensor entitySensor)
        {
            Proxy.Instance.EventBus.Raise(new CameraZoomEvent
            {
                ZoomLevel = CameraZoomLevel.None,
                ZoomVelocity = exitZoomVelocity
            });
        }

#if UNITY_EDITOR
        private void OnDrawGizmos()
        {
            Gizmos.color = Color.yellow;
            Gizmos.DrawWireSphere(transform.position, radius);
        }
#endif
    }
}