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 SoBuildableRuntimeSet _structureBaseSet;

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

        #region Publics

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

        #endregion

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

        private void InitializePower()
        {
            _power ??= gameObject.AddComponent<PowerBase>();
            _power.Initialize(_powerConfig);
        }

        protected void OnDisable()
        {
            _structureBaseSet.Remove(this);
        }

        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
    }
}