using UnityEngine;
using System.Collections;

public class FreqExploScript : FreqReactorScript {

    public Sprite[] shards;
    public GameObject shard;

    private FreqOriginScript freq_source;

    Vector2 offs;
    float health;
    public float healthmax;

	// Use this for initialization
	void Awake () {
        Randomize_Me();

        health = healthmax;
        freq_source = GameObject.FindGameObjectWithTag("Freq").GetComponent<FreqOriginScript>();
        if (resonance_frequency == 0) resonance_frequency = Tools.Note_To_Freq(resonance_note);
        else resonance_note = Tools.Freq_To_Note(resonance_frequency);
        offs = Vector2.zero;
	}

    void Randomize_Me() {
        resonance_note = GetComponent<FreqSwingScript>().resonance_note + 1;
        /*precision_needed = Random.Range(0.3f, 3f);
        amplitude_needed = 1;
        healthmax = Random.Range(0.3f,2f);*/
    }

    // Update is called once per frame
    void Update () {
        Randomize_Me();
        float pow = 0;
        if (freq_source.Amplitude() < 0.01f /*|| freq_source.Freq() < 1*/) ;
        else {
            float[] fs = freq_source.Freqs();
            float bestnote = 10000;
            for (int i = 0; i < fs.Length; i++) {
                float nn = Tools.Freq_To_Note(fs[i]);
                if (Mathf.Abs(nn - resonance_note) < Mathf.Abs(bestnote - resonance_note)) bestnote = nn;
            }
            //print(bestnote);
            float curr_note = bestnote;
            //float curr_note = Tools.Freq_To_Note(freq_source.Freq());
            float diff = curr_note - resonance_note;
            //print(diff);
            pow = Mathf.Max(0, precision_needed - Mathf.Abs(diff)) * Mathf.Max(0, freq_source.Amplitude() / amplitude_needed);
            //print(pow);
            //print(freq_source.Amplitude());
            //print(amplitude_needed);
            if (pow > 1) pow = 1;
        }
        if (pow > 0.25f) {
            float ss = (pow - 0.25f)/0.25f * 0.05f;
            Vector2 newoffs = new Vector2(Random.Range(-ss,ss),Random.Range(-ss,ss));
            transform.position = transform.position + (Vector3)(newoffs - offs);
            offs = newoffs;
        } else {
            transform.position = transform.position - (Vector3)offs;
            offs = Vector2.zero;
        }
        if (pow >= 0.25f) {
            health -= 0.03f;
            if (health <= 0) {
                if (shards.Length != 0) {
                    for (int i = 0; i < 30; i++) {
                        GameObject sha = GameObject.Instantiate(shard);
                        sha.GetComponent<SpriteRenderer>().sprite = shards[Random.Range(0, shards.Length)];
                        sha.transform.position = transform.position-2*transform.up+new Vector3(Random.Range(-0.7f,0.7f), Random.Range(-0.25f, 0.6f) - 1, 0);
                        sha.GetComponent<Rigidbody>().rotation = Quaternion.Euler(0,0,Random.Range(0, 360));
                        sha.GetComponent<Rigidbody>().angularVelocity = new Vector3(0,0,Random.Range(-2f,2f));
                        //float r = (GetComponent<Rigidbody>().rotation*3-90)/360f*Mathf.PI*2;
                        
                        //+ new Vector3(3*Mathf.Cos(r), 3*Mathf.Sin(r), 0);
                        if (GetComponent<FreqSwingScript>()) {
                            FreqSwingScript f = GetComponent<FreqSwingScript>();
                            sha.GetComponent<Rigidbody>().velocity += new Vector3(Random.Range(-f.curr_pow * 8, f.curr_pow * 8), 0,
                                Random.Range(-f.curr_pow *8, f.curr_pow * 8));
                            //float deriv = f.max_swing_amplitude * f.curr_pow * Mathf.Cos(Mathf.PI * 2 * Time.time * f.swing_frequency);
                            //print(deriv);
                            //sha.GetComponent<Rigidbody>().velocity += new Vector3(deriv*0.3f, 0, 0);
                        } else {
                            sha.GetComponent<Rigidbody>().velocity = new Vector3(Random.Range(-2f, 2f), Random.Range(-2f, 0), Random.Range(-2f, 2f));
                        }
                    }
                }
                ComboCenter.Instance.IncreaseScoreBy(10);
                GameObject.Destroy(gameObject);
            }
        } else {
            health += 0.1f;
            if (health > healthmax) health = healthmax;
        }
        //print(curr_pow);
    }
}
