using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class PlayerHealth : Singleton<PlayerHealth>
{
    //max "health" is 1.0f
    private ElementalSpawner _spawner;

    public void DamageAll(float damage)
    {
        foreach(EnergyWell well in wells)
        {
            well.Energy -= damage;
        }
    }

    public void TakeDamage(int damage, bool doesElementalDamage, Element.Component comp)
    {
        //ints are too big, translate to the float!
        float d = (float)damage/100f;
        if (doesElementalDamage) {
            EnergyWell well = _spawner.mercuryWell;
            if (comp == Element.Component.Soul) {
                well = _spawner.soulWell;
            } else if (comp == Element.Component.Plasma) {
                well = _spawner.plasmaWell;
            }
            well.Energy -= damage * 3f;
        } else {
            DamageAll(d);
        }
    }

    private List<EnergyWell> wells;

    public override void Awake()
    {
        base.Awake();
        _spawner = GetComponent<ElementalSpawner>();
    }

    protected void Start()
    {
        wells = new List<EnergyWell>()
        {
            _spawner.mercuryWell,
            _spawner.soulWell,
            _spawner.plasmaWell
        };
    }

    private float GetAverageEnergy()
    {
        float sum = 0;
        foreach(var well in wells)
        {
            sum += well.Energy;
        }
        float average = sum / wells.Count;
        return average;
    }

    protected void Update()
    {
        if(GetAverageEnergy()<=.01)
        {
            Game.Instance.PlayerDied();
        }
    }

}
