using UnityEngine;
using System.Collections;

public class BackgroundManager : MonoBehaviour {

    private TimeOfDay timeScript;

    public Color currentColour;
    public Color targetColour;
    public Color[] dayColours;

    public AnimationCurve pulseAnim;

    public float TransitionSpeed;
    public float PulseSpeed;

    public float newAlpha;

    private float currentAnimPercent;

	// Use this for initialization
	void Start ()
    {
        timeScript = GetComponent<TimeOfDay>();
        currentColour = new Color(0, 0, 0);
        currentAnimPercent = 0;
	}
	
	// Update is called once per frame
	void Update ()
    {
        targetColour = dayColours[(int)timeScript.timeState];

        if (currentAnimPercent < 1)
            currentAnimPercent += Time.deltaTime * PulseSpeed;
        else
            currentAnimPercent = 0;

        newAlpha = pulseAnim.Evaluate(currentAnimPercent);

        if (currentColour.r != targetColour.r)
        {
            if (currentColour.r < targetColour.r)
            {
                if ((targetColour.r - currentColour.r) > 0.01f)
                    currentColour.r += Time.deltaTime * TransitionSpeed;
            }
            else if (currentColour.r > targetColour.r)
            {
                if ((currentColour.r - targetColour.r) > 0.01f)
                    currentColour.r -= Time.deltaTime * TransitionSpeed;
            }
        }

        if (currentColour.g != targetColour.g)
        {
            if (currentColour.g < targetColour.g)
            {
                if ((targetColour.g - currentColour.g) > 0.01f)
                    currentColour.g += Time.deltaTime * TransitionSpeed;
            }
            else if (currentColour.g > targetColour.g)
            {
                if ((currentColour.g - targetColour.g) > 0.01f)
                    currentColour.g -= Time.deltaTime * TransitionSpeed;
            }
        }

        if (currentColour.b != targetColour.b)
        {
            if (currentColour.b < targetColour.b)
            {
                if ((targetColour.b - currentColour.b) > 0.01f)
                    currentColour.b += Time.deltaTime * TransitionSpeed;
            }
            else if (currentColour.b > targetColour.b)
            {
                if ((currentColour.b - targetColour.b) > 0.01f)
                    currentColour.b -= Time.deltaTime * TransitionSpeed;
            }
        }

        
    }
}
