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

public class ChargePoint : MonoBehaviour
{
    public int chargesRequired = 1;
    public float particleMultiplier = 100.0f;
    public float particleStartingAmount = 20.0f;
    public Color chargedColor = Color.green;
    public Color unchargedColor = new Color(255, 182, 0);

    int charges = 0;
    bool charged = false;
    ChargeCounter chargeCounter;
    ParticleSystem ps;
    SpriteRenderer outerRing;
    SpriteRenderer middleRing;
    SpriteRenderer innerRing;

    void Start()
    {
        chargeCounter = GameObject.FindWithTag("Charge Counter").GetComponent<ChargeCounter>();
        ps = GetComponent<ParticleSystem>();
        outerRing = transform.Find("Rings/Outer").GetComponent<SpriteRenderer>();
        middleRing = transform.Find("Rings/Middle").GetComponent<SpriteRenderer>();
        innerRing = transform.Find("Rings/Inner").GetComponent<SpriteRenderer>();
        VisualUpdate();
    }

    public void adjustCharges(int amount = 1)
    {
        charges += amount;

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

        VisualUpdate();
    }

    void VisualUpdate()
    {
        var em = ps.emission;
        Color transparent = Color.white;
        transparent.a = 0;

        em.rateOverTime = (1.0f / chargesRequired * charges * particleMultiplier) + particleStartingAmount;
        outerRing.color = unchargedColor;
        middleRing.color = unchargedColor;
        innerRing.color = unchargedColor;
        if (chargesRequired < 3) innerRing.color = transparent;
        if (chargesRequired < 2) middleRing.color = transparent;
        if (charges >= 1) outerRing.color = chargedColor;
        if (charges >= 2) middleRing.color = chargedColor;
        if (charges >= 3) innerRing.color = chargedColor;

    }
}
