using UnityEngine;
using System.Collections;

public class FreqOriginScript : MonoBehaviour {

    public const int MOUSECONTROL = 0;
    public const int SINGCONTROL = 1;
    public const int SPACECONTROL = 2;
    public int control;

    MousePitchHandler mph;
    Microphoner m;
    SpacePitchHandler sph;
    public int OctaveMultiplier;

    void Awake() {
        OctaveMultiplier = 0;
        control = StaticStore.SelectedMethod;

        mph = ((GameObject)GameObject.Find("MousePitchHandler")).GetComponent<MousePitchHandler>();
        sph = ((GameObject)GameObject.Find("SpacePitchHandler")).GetComponent<SpacePitchHandler>();
        m = ((GameObject)GameObject.Find("Microphoner")).GetComponent<Microphoner>();

        if (control == SINGCONTROL) {
            m.InitMike();
        }
        if (control == SPACECONTROL || control == MOUSECONTROL) {
            //Camera.main.gameObject.AddComponent<SineGenerator>();
            Camera.main.gameObject.AddComponent<SingGenerator>();
            Camera.main.GetComponent<AudioSource>().Play();
        }
    }
    
    public float Freq() {
        if (control == SINGCONTROL)
            return m.pitch*Mathf.Pow(2,OctaveMultiplier);
        if (control == MOUSECONTROL)
            return mph.x * Mathf.Pow(2, OctaveMultiplier);
        if (control == SPACECONTROL)
            return sph.playedpitch * Mathf.Pow(2, OctaveMultiplier);
        return 0;
    }

    public float[] Freqs() {
        if (control == SINGCONTROL) {
            if (m.allpitches.Length > 7) return new float[0];
            //print(x);
            float[] ap = new float[m.allpitches.Length];
            for (int i = 0; i < ap.Length; i++)
                ap[i] = m.allpitches[i]*Mathf.Pow(2, OctaveMultiplier);   
            return ap;
        }
        if (control == MOUSECONTROL)
            return new float[] { mph.x * Mathf.Pow(2, OctaveMultiplier) };
        if (control == SPACECONTROL)
            return new float[] { sph.playedpitch * Mathf.Pow(2, OctaveMultiplier) };
        return new float[] { };
    }

    public float Amplitude() {
        if (control == SINGCONTROL)
            return m.amplitude;
        else if (control == MOUSECONTROL)
            return mph.amp;
        else if (control == SPACECONTROL)
            return sph.amp;
        return 0;
    }

    void Update() {
        if (Input.GetKeyDown(KeyCode.F3)) {
            OctaveMultiplier--;
        }
        else if (Input.GetKeyDown(KeyCode.F4)) {
            OctaveMultiplier++;
        }
        if (OctaveMultiplier <= -3) OctaveMultiplier = -2;
        if (OctaveMultiplier >= 3) OctaveMultiplier = 2;

        if (Input.GetKeyDown(KeyCode.UpArrow)) {
            Microphoner.origthreshold += 0.0003f;
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow)) {
            Microphoner.origthreshold -= 0.0003f;
            if (Microphoner.origthreshold < 0.0005f) Microphoner.origthreshold = 0.0005f;
        }
    }
    void OnGUI() {
        //GUI.Label(new Rect(50, 100, 100, 100), OctaveMultiplier.ToString());
    }
}
