using Assets.Source.Misc;
using UnityEngine;

namespace Assets.Source.Core.Gameplay.Topdown
{
    public class Sun : MonoBehaviour, IDamager
    {
        public CollisionDetector PlayerDetector;

        [SerializeField] private float damageInterval = 1f;
        [SerializeField] private int damage = 1;

        private PlayerShipTopdown player;
        private bool inDangerZone = false;
        private float tickDanger = 0;

        void Awake()
        {
            PlayerDetector.OnTEnter2D += c =>
            {
                player = c.GetComponentInParent<PlayerShipTopdown>();
                player?.SetDangerWarning(inDangerZone = true);
            };
            PlayerDetector.OnTExit2D += c =>
            {
                var player = c.GetComponentInParent<PlayerShipTopdown>();
                player?.SetDangerWarning(inDangerZone = false);
            };
        }

        void Update()
        {
            if (inDangerZone)
            {
                tickDanger += Time.deltaTime;
                if (tickDanger >= damageInterval)
                {
                    tickDanger = 0;
                    player?.TakeDamage(this, damage);
                }
            }
        }
    }
}
