using UnityEngine;
using System.Collections;

// notes -12 to 0

public class FreqSwingScript3D : FreqReactorScript {

    public float swing_frequency;

    public float curr_pow;
    public float max_swing_amplitude;

    public float rot_change_delta;
    bool fall;

    Vector3 fioo;
    Quaternion origrot;

    private FreqOriginScript freq_source;
    public GameObject vase;

    public float dirmul;

    // Use this for initialization
    void Awake() {
        RandomizeMe();
        fioo = transform.forward;
        origrot = transform.rotation;
        fall = false;
        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);
        curr_pow = 0;
    }

    void RandomizeMe() {
        resonance_frequency = 0;
        resonance_note = Random.Range(0f, 5f);
    }

    // Update is called once per frame
    void Update() {
        if (!fall) vase.transform.position = transform.position + transform.up * 8.9f;
        if (!fall || true) {
            //print("ss" + freq_source.Freq().ToString());
            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; //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);
                if (fall) pow = 0;
                //print(pow);
                //print(freq_source.Amplitude());
                //print(amplitude_needed);
                if (pow > 1) pow = 1;

            }
            if (pow > curr_pow)
                curr_pow = Mathf.MoveTowards(curr_pow, pow, rot_change_delta);
            else
                curr_pow = Mathf.MoveTowards(curr_pow, pow, 0.1f * rot_change_delta);
            //print(curr_pow);
            transform.parent.transform.rotation =
               Quaternion.Euler(new Vector3(0, 0, dirmul * (- curr_pow * max_swing_amplitude)));
            //transform.rotation = origrot * Quaternion.AngleAxis(-curr_pow * max_swing_amplitude, fioo);
            //transform.RotateAround(transform.position + transform.right * 0.3f, transform.forward, -curr_pow * max_swing_amplitude);
            //transform.parent.GetComponent<Rigidbody>().AddForceAtPosition(transform.position+2*Vector3.up, transform.right*1*max_swing_amplitude);
            //GameObject.CreatePrimitive(PrimitiveType.Sphere).transform.position = transform.position + Vector3.up;

            if (curr_pow > 0.4f) Fall();
        }
    }

    void Fall() {
        fall = true;
        vase.GetComponent<ShardOnImpact>().StartMe();
        vase.transform.parent = null;
        vase.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;
        //vase.GetComponent<Rigidbody>().constantForce = new Vector3(1, 0, 0);

        Destroy(this, 1);
    }

    void LateUpdate() {
        //transform.localEulerAngles = new Vector3(0, 0, transform.localEulerAngles.z);
    }

}
