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

public class Scoreboard : MonoBehaviour
{
    public static Scoreboard instance;

    public List<SpriteRenderer> blueGems;
    public List<SpriteRenderer> redGems;

    public int scoreRed;
    public int scoreBlue;

    public List<Sprite> blueFrames;
    public List<Sprite> redFrames;

    int currentFrameRed;
    int currentFrameBlue;
    float frameTimer;
    float removeTimer;
    float removePercent;
    float timePerFrame;
    float frameInterval = 0.5f;
    float framePercent;

    bool doingSomething = false;
    bool addingRed = false;
    bool addingBlue = false;
    bool removingRed = false;
    bool removingBlue = false;

    Color c;

    void Awake() { instance = this; }

	// Use this for initialization
	void Start ()
    {
	    
	}
	
	// Update is called once per frame
	void Update ()
    {
        //if (Input.GetKeyDown(KeyCode.P))
        //    AddRed();
        //if (Input.GetKeyDown(KeyCode.O))
        //    AddBlue();
        //if (Input.GetKeyDown(KeyCode.L))
        //    RemoveRed();
        //if (Input.GetKeyDown(KeyCode.K))
        //    RemoveBlue();

        if (doingSomething)
        {
            frameTimer += Time.deltaTime;
            framePercent = frameTimer / frameInterval;

            removeTimer = frameInterval - frameTimer;
            removePercent = 1 - framePercent;
        }

	    if(addingRed)
        {
            currentFrameRed = Mathf.FloorToInt((frameTimer / timePerFrame));
            if (currentFrameRed >= redFrames.Count) currentFrameRed = redFrames.Count - 1;

            redGems[scoreRed - 1].sprite = redFrames[currentFrameRed];

            c = redGems[scoreRed - 1].color;
            c.a = Mathf.Lerp(0.5f, 1.0f, framePercent);
            redGems[scoreRed - 1].color = c;
        }

        if(addingBlue)
        {
            currentFrameBlue = Mathf.FloorToInt((frameTimer / timePerFrame));
            if (currentFrameBlue >= blueFrames.Count) currentFrameBlue = blueFrames.Count - 1;

            blueGems[scoreBlue - 1].sprite = blueFrames[currentFrameBlue];

            c = blueGems[scoreBlue - 1].color;
            c.a = Mathf.Lerp(0.5f, 1.0f, framePercent);
            blueGems[scoreBlue - 1].color = c;
        }

        if(removingRed)
        {
            currentFrameRed = Mathf.FloorToInt((removeTimer / timePerFrame));
            currentFrameRed = Mathf.Clamp(currentFrameRed, 0, redFrames.Count - 1);

            redGems[scoreRed].sprite = redFrames[currentFrameRed];

            c = redGems[scoreRed].color;
            c.a = Mathf.Lerp(0.5f, 1.0f, removePercent);
            redGems[scoreRed].color = c;
        }

        if(removingBlue)
        {
            currentFrameBlue = Mathf.FloorToInt((removeTimer / timePerFrame));
            currentFrameBlue = Mathf.Clamp(currentFrameBlue, 0, blueFrames.Count - 1);

            blueGems[scoreBlue].sprite = blueFrames[currentFrameBlue];

            c = blueGems[scoreBlue].color;
            c.a = Mathf.Lerp(0.5f, 1.0f, removePercent);
            blueGems[scoreBlue].color = c;
        }
	}

    void LateUpdate()
    {
        if(doingSomething)
        {
            if(frameTimer >= frameInterval)
            {
                doingSomething = false;
                addingRed = false;
                addingBlue = false;
                removingRed = false;
                removingBlue = false;
            }
        }
    }

    public void AddRed()
    {
        if (scoreRed >= 3) return;

        scoreRed++;

        addingRed = true;
        doingSomething = true;
        frameTimer = 0;
        removeTimer = frameInterval;
        timePerFrame = frameInterval / redFrames.Count;
    }

    public void AddBlue()
    {
        if (scoreBlue >= 3) return;

        scoreBlue++;

        addingBlue = true;
        doingSomething = true;
        frameTimer = 0;
        removeTimer = frameInterval;
        timePerFrame = frameInterval / blueFrames.Count;
    }

    public void RemoveRed()
    {
        if (scoreRed <= 0) return;

        scoreRed--;

        removingRed = true;
        doingSomething = true;
        frameTimer = 0;
        removeTimer = frameInterval;
        timePerFrame = frameInterval / redFrames.Count;
    }

    public void RemoveBlue()
    {
        if (scoreBlue <= 0) return;

        scoreBlue--;

        removingBlue = true;
        doingSomething = true;
        frameTimer = 0;
        removeTimer = frameInterval;
        timePerFrame = frameInterval / redFrames.Count;
    }
}
