using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spectator : MonoBehaviour {
    public float[] deGustibus;
    public float standardDeviation=1;
    public float mean = 0;
    public float decay=0.001f;
    public float decayFrame = 0.1f;
    public bool noTaste;
    public float appreciationThreshold;
    public GameController controller;
    public bool havingFun;
    public Sprite havingFunSprite, boredSprite;
    public float jumpThreshold;
    public float initialY;
    private Transform myTrans;
    public float timer;
    private SpriteRenderer mySprite;
    public float freqMultiplier, minimumFreq;
	// Use this for initialization
	void Start () {
        deGustibus = new float[3];
        changeTastes();
        StartCoroutine(LoseTaste());
        myTrans = GetComponent<Transform>();
        initialY = myTrans.position.y;
        mySprite = GetComponent<SpriteRenderer>();
        mySprite.sprite = boredSprite;
	}
  
    IEnumerator LoseTaste()
    {
        do
        {
            noTaste = true;
            if (deGustibus[0] > decay) {
                deGustibus[0] -= decay;
                noTaste = false;
            }
            if (deGustibus[1] > decay)
            {
                deGustibus[1] -= decay;
                noTaste = false;
            }   
            if (deGustibus[2] > decay)
            {
                deGustibus[2] -= decay;
                noTaste = false;
            }
            if (deGustibus[0] < -decay)
            {
                deGustibus[0] += decay;
                noTaste = false;
            }
            if (deGustibus[1] < -decay)
            {
                deGustibus[1] += decay;
                noTaste = false;
            }
            if (deGustibus[2] < -decay)
            {
                deGustibus[2] += decay;
                noTaste = false;
            }
            if (noTaste) changeTastes();
            yield return new WaitForSeconds(decayFrame);
        }
        while (true);

    }

    // Recalculate Musical Tastes
    void changeTastes() {
        changeTaste(0);
        changeTaste(1);
        changeTaste(2);
    }

    void changeTaste(int id) {
        deGustibus[id] = HelpingFunctions.nextGaussian(mean, standardDeviation);
    }

	// Update is called once per frame
	void Update () {
        if (controller.soundPlaying != -1) 
            if ((deGustibus[controller.soundPlaying] > appreciationThreshold) && (!havingFun)) StartCoroutine(HaveFun());
	}

    IEnumerator HaveFun() {
        havingFun = true;
        mySprite.sprite = havingFunSprite;
        do
        {
            timer = 0;
            float frequence = (deGustibus[controller.soundPlaying] - appreciationThreshold)*freqMultiplier + minimumFreq ;
            do
            {
                timer += Time.deltaTime;
                float pos = Mathf.Sin(2 * Mathf.PI * frequence * timer);
                float Y = Mathf.Lerp(initialY, initialY + jumpThreshold, pos);
                myTrans.position = new Vector3(myTrans.position.x, Y, myTrans.position.z);
                yield return null;  
            }
            while (timer < 1 / (2 * frequence));
        }
        while ((deGustibus[controller.soundPlaying] > appreciationThreshold));
        havingFun = false;
        mySprite.sprite = boredSprite;
    }
}
