using UnityEngine;
using System.Collections;


// NB. FreqMeter has s[i] for i = 0, ..., maxfreq - minfreq - 1
// but microphoner does all its calcs with the 1024 array of freqs
// so there's a lot of translation which tends to get fucked up
public class FreqMeter : MonoBehaviour {

    GameObject[] s;
    GameObject[] eb;
    GameObject[] obfreqs;

    public float width;

    public GameObject bar;

    float heihei;

    FreqOriginScript freq_source;
    Microphoner m;

    public Vector2 poz;

    // Use this for initialization
    void Awake () {
        freq_source = GameObject.FindGameObjectWithTag("Freq").GetComponent<FreqOriginScript>();
        m = ((GameObject)GameObject.Find("Microphoner")).GetComponent<Microphoner>();
        heihei = -5.7f;
        s = new GameObject[Microphoner.maxfreq - Microphoner.minfreq];
        for (int i = 0; i < Microphoner.maxfreq - Microphoner.minfreq; i++) {
            s[i] = GameObject.Instantiate(bar);
            //s[i].transform.localScale = new Vector3(1, 1, 1f);
            s[i].transform.position = Idx_To_Bar_Pos(i, 0);
                /*(Vector3)poz +
                new Vector3(i * (1f / (Microphoner.maxfreq - Microphoner.minfreq)) * 4, 0, 0);*/
            s[i].GetComponent<SpriteRenderer>().material.color = Color.red;
            //print(s[i].transform.position);
        }
        eb = new GameObject[0];
        /*for (int i = 0; i < eb.Length; i++) {
            print("mero");
            eb[i] = GameObject.Instantiate(bar);
            eb[i].transform.localScale = new Vector3(0.4f, 2f, 1f);
            eb[i].transform.position = Idx_To_Bar_Pos(
                Microphoner.Pitch_To_Idx(Mathf.Pow(2, i) * 220f) - Microphoner.minfreq,
                Microphoner.threshold);// Microphoner.threshold);
            eb[i].GetComponent<SpriteRenderer>().material = new Material(eb[i].GetComponent<SpriteRenderer>().material);
            eb[i].GetComponent<SpriteRenderer>().material.color = Color.red;
            eb[i].GetComponent<SpriteRenderer>().sortingOrder = 1;
        }*/
        UpdateObjFreqs(true);
    }

    void UpdateObjFreqs(bool first) {
        int keekko = 0;
        FreqReactorScript[] objs = GameObject.FindObjectsOfType<FreqReactorScript>();
        for (int i = 0; i < Microphoner.maxfreq - Microphoner.minfreq; i++) {
            bool green = false;
            for (int j = 0; j < objs.Length; j++) {
                if (Mathf.Abs(Microphoner.Pitch_To_Idx(objs[j].resonance_frequency) - (i + Microphoner.minfreq)) == 0) {
                    green = true;
                    continue;
                }
            }
            if (green) {
                s[i].GetComponent<SpriteRenderer>().material.color = Color.green;
                //s[i].GetComponent<SpriteRenderer>().sortingOrder = 1;
            } else {
                s[i].GetComponent<SpriteRenderer>().material.color = Color.red;
                //s[i].GetComponent<SpriteRenderer>().sortingOrder = 0;
            }
        }
        //print(keekko);

            /*if (!first) {
                for (int i = 0; i < obfreqs.Length; i++) Destroy(obfreqs[i]);
            }


            obfreqs = new GameObject[objs.Length];
            for (int i = 0; i < objs.Length; i++) {
                obfreqs[i] = GameObject.Instantiate(bar);
                //obfreqs[i].transform.localScale = new Vector3(1, 1, 1f);
                obfreqs[i].transform.position = Idx_To_Bar_Pos(
                    Microphoner.Pitch_To_Idx(objs[i].resonance_frequency) - Microphoner.minfreq,
                            Microphoner.origthreshold);
                obfreqs[i].GetComponent<SpriteRenderer>().material = new Material(obfreqs[i].GetComponent<SpriteRenderer>().material);
                obfreqs[i].GetComponent<SpriteRenderer>().material.color = Color.green;
                obfreqs[i].GetComponent<SpriteRenderer>().sortingOrder = -1;
            }*/
        }

    void OnGUI() {
        if (freq_source.control == FreqOriginScript.SINGCONTROL) {
            string s = "";
            for (int i = 0; i < m.allpitches.Length; i++) {
                s += " " + (m.allpitches[i] * Mathf.Pow(2,freq_source.OctaveMultiplier)).ToString();
            }
            //GUI.Label(new Rect(50, 50, 100, 100), s);
        } else {
            //print("i am");
            if (freq_source.Amplitude() > 0.01f) {
                //GUI.Label(new Rect(150, 250, 100, 100),
                //    (freq_source.Freq() * Mathf.Pow(2, freq_source.OctaveMultiplier)).ToString());
                //print("fly");
                //print(freq_source.Freq().ToString());
            }
            else;
                //GUI.Label(new Rect(50, 150, 100, 100),
                 //   "no");
        }

        //GUI.Label(new Rect(50, 300, 100, 100), Microphoner.origthreshold.ToString());
    }

    void Update() {
        UpdateObjFreqs(false);
        for (int i = 0; i < Microphoner.maxfreq - Microphoner.minfreq; i++) {
            if (freq_source.control == FreqOriginScript.SINGCONTROL) {
                //print(m.li.Length);
                int usi = (int)((i + Microphoner.minfreq)*Mathf.Pow(2,-freq_source.OctaveMultiplier));
                float ll = 0;
                if (usi < m.li.Length)
                    ll = m.li[usi];

                s[i].transform.position = Idx_To_Bar_Pos(i,ll);
                    ;
            } else {
                if (freq_source.Amplitude() < 0.01f) {
                    s[i].transform.position = Idx_To_Bar_Pos(i, 0);
                }  else {
                    float pitch = freq_source.Freq();
                    float pos = Microphoner.Pitch_To_Idx(pitch * Mathf.Pow(2, freq_source.OctaveMultiplier)); //*(44100f / 2) / 1024f is index to freq
                    s[i].transform.position = Idx_To_Bar_Pos(i,
                        Mathf.Max(0, (4 - (Mathf.Abs(i + Microphoner.minfreq - pos)))) * 0.002f);
                }
            }
            //new Vector3(i*0.1f, li[i]*100, 0);
            //if (/*i == maxN*/ li[i] > threshold) s[i].transform.localScale = new Vector3(1, 1, 1);
            //else s[i].transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);

            
        }
        for (int i = 0; i < eb.Length; i++) {
            eb[i].transform.position = Idx_To_Bar_Pos(
                Microphoner.Pitch_To_Idx(Mathf.Pow(2, i) * 220f) - Microphoner.minfreq,
                Microphoner.threshold);
            eb[i].GetComponent<SpriteRenderer>().material.color = Color.red;
            eb[i].GetComponent<SpriteRenderer>().sortingOrder = -1;
        }
    }

    Vector3 Idx_To_Bar_Pos(int i, float amp) {
        if (amp > 0.01f) amp = 0.01f;
        return (Vector3)poz +
                    new Vector3(i * (1f / (Microphoner.maxfreq - Microphoner.minfreq)) * width,
                    amp * 150, 0);
    }
}
