using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Cromometro : MonoBehaviour {

    public static Cromometro current;

    public float minR;
    public float minG;
    public float minB;
    public float maxB;

    public float prevX = 0f;
    public float prevMaxX = 0f;
    public float prevMinX = 0f;
    public float prevT = 0f;
    public bool goingUp;
    public float frequency = 0f;
    public float amplitude = 0f;
    public float period = 0f;
    public float minAmplitude;

    public RawImage black;
    public RawImage red;
    public RawImage green;
    public RawImage blue;

    public Color genericBlack = new Color(0, 0, 0);
    public Color genericRed = new Color(1f, 0, 0);
    public Color genericGreen = new Color(0, 1f, 0);
    public Color genericBlue = new Color(0, 0, 1f);

    public AudioClip audioBlack;
    public AudioClip audioRed;
    public AudioClip audioGreen;
    public AudioClip audioBlue;

    public WaveColor currentColor_;

    void Awake()
    {
        current = this;

        minG = minR * 2;
        minB = minG * 2;
        maxB = minB * 2;

        UpdateColors();
    }

    private void UpdateColors()
    {
        ColorEnabled(red, currentColor == WaveColor.red);
        ColorEnabled(green, currentColor == WaveColor.green);
        ColorEnabled(blue, currentColor == WaveColor.blue);
        ColorEnabled(black, currentColor == WaveColor.black);
    }

    private void ColorEnabled(RawImage img, bool val)
    {
        img.color = new Color(img.color.r, img.color.g, img.color.b, val ? 1f : .3f);
    }

    void Update ()
    {
        period = Mathf.Max(Time.time - prevT, period);
        frequency = 1f / period;

        if (goingUp)
        {
            if(Input.mousePosition.x < prevX)
            {
                period = Time.time - prevT;
                prevMaxX = prevX;
                prevT = Time.time;
                amplitude = prevMaxX - prevMinX;
                goingUp = false;
            }
        }
        else if (Input.mousePosition.x > prevX)
        {
            period = Time.time - prevT;
            prevMinX = prevX;
            prevT = Time.time;
            amplitude = prevMaxX - prevMinX;
            goingUp = true;
        }

        var prev = currentColor;
        if (amplitude >= minAmplitude)
        {
            currentColor = frequency < minR ? WaveColor.black
                : (frequency < minG ? WaveColor.red
                : (frequency < minB ? WaveColor.green
                : WaveColor.blue));
        }
        else
            currentColor = WaveColor.black;

        if (prev != currentColor && currentColor != WaveColor.black)
            Nivel.PlayAudio(audioFrecuencia);

        UpdateColors();

        prevX = Input.mousePosition.x;
	}

    public WaveColor currentColor
    {
        set
        {
            if(value != currentColor_)
            {
                currentColor_ = value;
                Nivel.current.ActualizarColor();
            }
        }
        get { return currentColor_; }
    }

    private AudioClip audioFrecuencia
    {
        get
        {
            switch(currentColor)
            {
                default:
                    return null;
                case WaveColor.red:
                    return audioRed;
                case WaveColor.green:
                    return audioGreen;
                case WaveColor.blue:
                    return audioBlue;
            }
        }
    }
}
