using System;
using GameDev.CoreSystems;
using ScriptableObjectArchitecture;
using UnityEngine;

namespace AsteroidGame.Handlers
{
    public class PowerHandler : HandlerBase
    {
        [Header("State")]
        [SerializeField] private int _powerConsumption;
        [SerializeField] private int _powerConsumptionPeak;
        [SerializeField] private int _powerCapacity;
        [SerializeField] private float _powerFactor;

        [Header("Connections")]
        [SerializeField] private SoPowerSystemRuntimeSet _activePowerStructures;
        
        
        private void Update()
        {
            _powerConsumption = 0;
            _powerConsumptionPeak = 0;
            _powerCapacity = 0;
            foreach (var structure in _activePowerStructures)
            {
                if (structure.IsConsumer)
                {
                    _powerConsumption += structure.GetCurrentPower();
                    _powerConsumptionPeak += structure.GetMaxPower();
                }

                if (structure.IsGenerator)
                {
                    _powerCapacity += structure.GetMaxPower();
                }
            }

            if (_powerCapacity > 0)
            {
                _powerFactor = (float)_powerConsumption / _powerCapacity;
            }
            else
            {
                _powerFactor = 0;
            }
        }
        
        public int GetMaxPower()
        {
            throw new NotImplementedException();
        }

        public int GetCurrentPower() => _powerConsumption;

        public float GetPowerFactor() => _powerFactor;

    }
}