using UnityEngine;
using Saltosion.OneWeapon.Utils;
using Saltosion.OneWeapon.Player;
using Saltosion.OneWeapon.Enemies;

namespace Saltosion.OneWeapon.AI.Triggers {
    public class PlayerSighted : Trigger {
        public LayerMask SearchLayerMask;
        [Tooltip("How close the player needs to be for the AI to notice them.")]
        public float Radius;
        [Tooltip("How far the player needs to escape for the AI to \"forget\" them.")]
        public float ForgetRadius;
        [Tooltip("How long does it take for the enemy to forget where the player was?")]
        public float ForgetTimer;
        public PlayerController Player { get; private set; }

        private bool CountdownStarted = false;
        private float ForgetCountdownStartTime = 0.0f;

        public override bool IsTriggered() {
            PlayerController Sighted = Util.GetClosestTo<PlayerController>(transform, Player == null ? Radius : ForgetRadius, SearchLayerMask, true);
            if (Sighted != null) {
                CountdownStarted = false;
                Player = Sighted;
            } else if (!CountdownStarted) {
                CountdownStarted = true;
                ForgetCountdownStartTime = Time.time;
            } else if (Time.time - ForgetCountdownStartTime > ForgetTimer) {
                Player = null;
            }
            return Player != null;
        }

        private void OnDrawGizmosSelected() {
            Gizmos.color = Color.green;
            Gizmos.DrawWireSphere(transform.position, Radius);
            Gizmos.color = new Color(Color.green.r, Color.green.g, Color.green.b, 0.5f);
            Gizmos.DrawWireSphere(transform.position, ForgetRadius);
        }
    }
}
