using System;
using System.Collections.Generic;
using AsteroidGame.Entities.Structures.Scripts;
using AsteroidGame.Interfaces;
using UnityEngine;
using UnityEngine.Pool;

namespace AsteroidGame.Handlers
{
    public class PowerHandler : HandlerBase
    {
        [Header("State")] [SerializeField] private int powerConsumption;
        [SerializeField] private int powerCapacity;
        [SerializeField] private float powerFactor;

        [Header("Connections")] [SerializeField]
        private BuildingHandler buildingHandler;
        // [SerializeField] private BuildingHandler buildingHandler;

        #region Private

        [SerializeField] private List<StructureBase> activeStructures = new();

        #endregion

        protected override void OnEnable()
        {
            base.OnEnable();
            buildingHandler = FindObjectOfType<BuildingHandler>();
            activeStructures = buildingHandler.GetActiveStructures();
        }

        private void Update()
        {
            powerConsumption = 0;
            powerCapacity = 0;
            foreach (var structure in activeStructures)
            {
                if (structure.IsPowerConsumer)
                {
                    powerConsumption += structure.GetMaxPower();
                }

                if (structure.IsPowerGenerator)
                {
                    powerCapacity += structure.GetMaxPower();
                }
            }

            if (powerCapacity > 0)
            {
                powerFactor = (float)powerConsumption / (float)powerCapacity;
            }
            else
            {
                powerFactor = 0;
            }
        }

        public int GetMaxPower()
        {
            throw new System.NotImplementedException();
        }

        public int GetCurrentPower()
        {
            throw new System.NotImplementedException();
        }

        public float GetPowerFactor()
        {
            throw new System.NotImplementedException();
        }
    }
}