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

[RequireComponent(typeof(AudioSource))]
[RequireComponent(typeof(RawImage))]
public class VaweController : MonoBehaviour
{


    public int width = 500; // texture width 
    public int height = 100; // texture height 
    public Color backgroundColor = Color.black;
    public Color waveformColor = Color.green;
    public int size = 2048; // size of sound segment displayed in texture

    private Color[] blank; // blank image array 
    private GameObject body;
    private Texture2D texture;
    private float[] samples; // audio samples array
    private GameObject musicInput;
    MicInput micIn;
    public GameObject audioInputObject;
    public GameObject platform;
    public float minFrequency;
    public float maxFrequency;

    IEnumerator Start()
    {
        this.GetComponent<RawImage>().enabled = false;

        if (audioInputObject == null)
        {
            audioInputObject = GameObject.Find("Platforms");
        }
        micIn = (MicInput)audioInputObject.GetComponent("MicInput");

        // create the samples array 
        samples = new float[size];

        // create the texture and assign to the guiTexture: 
        texture = new Texture2D(width, height);

        GetComponent<RawImage>().texture = texture;

        // create a 'blank screen' image 
        blank = new Color[width * height];

        for (int i = 0; i < blank.Length; i++)
        {
            blank[i] = backgroundColor;
        }

        // refresh the display each 100mS 
        while (true)
        {
            GetCurWave();
            yield return new WaitForSeconds(0.01f);
        }
    }

    void Update()
    {
       /* int f = (int)micIn.frequency; // Get the frequency from our MicrophoneInput script
        if (f >= 260 && f <= 292) // Compare the frequency to known value, take possible rounding error in to account
        {
            Debug.LogWarning("Middle-C played!");
        }
        else if (f > 245 && f<=259)
        {
            Debug.LogWarning("B3 played!");
        }
        else if (f > 219 && f <= 245)
        {
            Debug.LogWarning("A3 played!");
        }
        else if (f > 195 && f <= 219)
        {
            Debug.LogWarning("G3 played!");
        }
        else if (f > 174 && f <= 195)
        {
            Debug.LogWarning("F3 played!");
        }
        else if (f > 164 && f <= 174)
        {
            Debug.LogWarning("E3 played!");
        }
        else if (f > 146 && f <= 164)
        {
            Debug.LogWarning("D3 played!");
        }
        else if (f > 292 && f <= 328)
        {
            Debug.LogWarning("D4 played!");
        }
        else if (f > 328 && f <= 348)
        {
            Debug.LogWarning("E4 played!");
        }
        else if (f > 348 && f <= 391)
        {
            Debug.LogWarning("F4 played!");
        }
        else if (f > 391 && f <= 439)
        {
            Debug.LogWarning("G4 played!");
        }
        else if (f > 439 && f <= 492)
        {
            Debug.LogWarning("A4 played!");
        }
        else if (f > 492 && f <= 522)
        {
            Debug.LogWarning("B4 played!");
        }*/
    }

    void GetCurWave()
    {
        // clear the texture 
        texture.SetPixels(blank, 0);

        if(micIn.audio!=null)
        {
            micIn.audio.GetOutputData(samples, 0);
            //Debug.Log(micIn);
        }
        else
        {
            audioInputObject.GetComponent<AudioSource>().GetOutputData(samples, 0);

        }
        

        // draw the waveform 
        int f = (int)micIn.frequency;
        if (f > minFrequency && f <= maxFrequency && !GameObject.Find("Player").GetComponent<PlayerController>().silenced)
        {
            platform.SetActive(false);
            Destroy(platform.GetComponent<Rigidbody2D>());
            this.GetComponent<RawImage>().enabled = true;
            for (int i = 0; i < size; i++)
            {
                texture.SetPixel((int)(width * i / size), (int)(height * (samples[i] + 1f) / 2f), waveformColor);
            } // upload to the graphics card 
            texture.Apply();
        }
        else
        {
            ///TODO: only add rigidbody to the platform that reappears.

            platform.AddComponent<Rigidbody2D>();
            platform.GetComponent<Rigidbody2D>().isKinematic = true;
            platform.SetActive(true);
            this.GetComponent<RawImage>().enabled = false;
        }
       
    }
}