using UnityEngine;
using System.Collections;

public class BackgroundColorChange : MonoBehaviour {

	private GameObject audioInputObject0;
	private GameObject audioInputObject1;
	private MicrophoneInput micIn0;
	private MicrophoneInput micIn1;
	private SpriteRenderer bg;
	private Color newColor;
	private Quaternion newRotation;
	// Use this for initialization
	void Start () {
		audioInputObject0 = GameObject.Find ("MicInput0");
		micIn0 = (MicrophoneInput) audioInputObject0.GetComponent("MicrophoneInput");
		audioInputObject1 = GameObject.Find ("MicInput1");
		micIn1 = (MicrophoneInput) audioInputObject1.GetComponent("MicrophoneInput");
	
		bg = GetComponent<SpriteRenderer> ();
		newRotation = new Quaternion (0f, 0f, 0f, -1f);
	}
	
	// Update is called once per frame
	void Update () {
		Debug.Log(Camera.main.transform.rotation);
		bgColorShifter ();
		ScreenShaker ();
	}

	void bgColorShifter() {
		newColor = new Color(Random.value, Random.value, Random.value, 1);//Random.value; // value is already between 0 and 1
		bg.color = Color.Lerp(bg.color, newColor, Time.deltaTime * (micIn0.loudness*10 + micIn1.loudness*10) );
	}

	void ScreenShaker(){
		
		Camera.main.transform.rotation = Quaternion.Lerp (Camera.main.transform.rotation, newRotation, Time.deltaTime*100);
		newRotation.w *= -1f;
	}
}
