using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChargePoint : MonoBehaviour
{
    public int chargesRequired = 1;

    int charges = 0;
    bool charged = false;
    ChargeCounter chargeCounter;
    ParticleSystem ps;
    SpriteRenderer sr;

    void Start()
    {
        chargeCounter = GameObject.FindWithTag("Charge Counter").GetComponent<ChargeCounter>();
        ps = GetComponent<ParticleSystem>();
        sr = GetComponent<SpriteRenderer>();
    }

    public void adjustCharges(int amount = 1)
    {
        charges += amount;
        Debug.Log("Charges is" + charges + "/" + chargesRequired);

        if (charges >= chargesRequired)
        {
            if (!charged) chargeCounter.AddChargedPoint(gameObject);
            charged = true;
        }
        else
        {
            chargeCounter.RemoveChargedPoint(gameObject);
            charged = false;
        }

        VisualUpdate();
    }

    void VisualUpdate()
    {
        var em = ps.emission;

        if (charges == 0)
        {
            em.rateOverTime = 5;
            Color transparent = Color.white;
            transparent.a = 0;
            sr.color = transparent;
        }
        else if (!charged)
        {
            em.rateOverTime = 20;
            Color orange = new Color(255, 182, 0);
            orange.a = 0.1f;
            sr.color = orange;
        }
        else
        {
            em.rateOverTime = 40;
            Color green = Color.green;
            green.a = 0.3f;
            sr.color = green;
        }
    }
}
