using System;
using AsteroidGame.Interfaces;
using UnityEngine;
using UnityEngine.Serialization;

namespace AsteroidGame.Entities.Structures.Scripts
{
    public class StructureBase : EntityBase, IBuildable, IPowerSystem
    {
        [Header("BuildParameters")] 
        [SerializeField]
        protected int cost;

        [SerializeField] private bool buildPlacementBlocked;
        [SerializeField] protected float buildTimer;
        
        [Header("Power")]
        [SerializeField] protected bool isPowerGenerator;
        [SerializeField] protected bool isPowerConsumer;
        [SerializeField] protected int maxPower;
        [SerializeField] protected int currentPower;
        
        #region Private


        #endregion
        
        #region Publics
        public bool IsPowerGenerator => isPowerGenerator;
        public bool IsPowerConsumer => isPowerConsumer;
        public bool BuildPlacementBlocked => buildPlacementBlocked;
        public int Cost => cost;

        #endregion

        private void OnEnable()
        {
            if (!isPowerConsumer && !isPowerGenerator)
            {
                Debug.Log("Power consumer/generator not set!");
            }
        }

        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 void SetMaxPower(int newValue)
        {
            maxPower = newValue;
        }
        

        #endregion

        #region Getters
        
        public int GetMaxPower()
        {
            return maxPower;
        }

        public int GetCurrentPower()
        {
            return currentPower;
        }

        public float GetPowerFactor()
        {
            // ReSharper disable once PossibleLossOfFraction
            return currentPower / maxPower;
        }
        
        #endregion
    }
}