using AsteroidGame.ScriptableObjects;
using GameDev.CoreSystems;
using UnityEngine;

namespace AsteroidGame.Entities
{
    public class EntityBase : MonoBehaviour
    {
        [field: Header("Damageable")]
        [field: SerializeField] public Damageable Damageable { get; set; }

        [field: Header("Targetable")]
        [field: SerializeField] public Targetable Targetable { get; set; }

        [Header("UI")]
        [SerializeField] protected string _uiFriendlyName;

        [Space]
        [SerializeField] private SoEntityBaseRuntimeSet _entityBaseSet;

        #region Props

        public string UiFriendlyName => _uiFriendlyName;

        #endregion

        private void Awake()
        {
            InitializeDamageable();

            InitializeTargetable();

            AssignDamageable();
        }

        protected virtual void OnEnable()
        {
            _entityBaseSet.Add(this);
        }

        protected virtual void OnDisable()
        {
            _entityBaseSet.Remove(this);
        }

        private void InitializeDamageable()
        {
            if (Damageable != null) return;
            Damageable = gameObject.AddComponent<Damageable>();
        }

        private void InitializeTargetable()
        {
            if (Targetable != null) return;
            Targetable = gameObject.AddComponent<Targetable>();
        }

        private void AssignDamageable()
        {
            if (Targetable.Damageable != null) return;

            Targetable.Damageable = Damageable;
        }
    }
}