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

namespace AsteroidGame.Entities
{
    public class StructureBase : EntityBase, IBuildable
    {
        [Header("BuildParameters")]
        [SerializeField] protected int _cost;
        [SerializeField] private bool _buildPlacementBlocked;
        [SerializeField] protected float _buildTimer;

        [Header("Configuration")]
        [SerializeField] private SoEntityBaseRuntimeSet _structureBaseSet;
        [SerializeField] private SoPowerBaseRuntimeSet _powerBaseSet;

        [Header("Power")]
        [SerializeField] private SoPowerConfig _powerConfig;
        [SerializeField] private PowerBase _power;

        #region Publics

        public PowerBase Power => _power;
        public bool BuildPlacementBlocked => _buildPlacementBlocked;

        #endregion

        protected override void OnEnable()
        {
            base.OnEnable();
            _structureBaseSet.Add(this);
            InitializePower();
        }

        private void InitializePower()
        {
            _power = gameObject.AddComponent<PowerBase>();
            _power.SetConfig(_powerConfig);
            _powerBaseSet.Add(_power);

        }

        protected override void OnDisable()
        {
            base.OnDisable();
            _structureBaseSet.Remove(this);
            _powerBaseSet.Remove(_power);
        }

        private void OnTriggerStay(Collider other)
        {
            if (other.name == "BuildCollider")
            {
                _buildPlacementBlocked = true;
            }
        }

        private void OnTriggerExit(Collider other)
        {
            if (other.name == "BuildCollider")
            {
                _buildPlacementBlocked = false;
            }
        }

        #region Setters

        public int SetCost(int newCost) => _cost = newCost;

        #endregion

        #region Getters

        public int GetCost() => _cost;

        #endregion
    }
}