using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class AlterScriptDispell : MonoBehaviour {
	public KeyCode[] ritualSequence;
	private Transform LetterPos;			// A position marking where to check if the player is grounded.
	public Text ritualText;
	public float lightLow = 3.55f;
	public float lightHigh = 8;
	private bool summoning = false;
	private bool summonOver = false;
	private int i = 0;
	private int wait = 0;
	public AudioClip failClip;
	public AudioClip successClip;
	public GameObject Player;
	public Light projectedLight;

	void Awake(){
		ritualText.text = "";
		projectedLight.intensity = lightLow;
	}

	void OnTriggerEnter2D (Collider2D col) 
	{
		// If it hits the player...
		if(col.tag == "Player" && !summonOver)
		{
			summoning = true;
			projectedLight.intensity = lightHigh;
		}
	}

	void OnTriggerExit2D (Collider2D col) 
	{
		// If it hits the player...
		if(col.tag == "Player")
		{
			ritualText.text = "";
			summoning = false;
			if(!summonOver)
			projectedLight.intensity = lightLow;
		}
	}

	void FixedUpdate ()
	{
		if (summoning && (wait == 0)) {
			ritualText.text = ritualSequence [i].ToString ();
			if (Input.anyKeyDown) {
				if (Input.GetKey(ritualSequence [i])) {
					if (i + 1 < ritualSequence.Length)
						i += 1;
					else {
						ritualText.text = "";
						summonOver = true;
						summoning = false;
						Player.GetComponent<PlayerControl> ().Dispell ();
						AudioSource.PlayClipAtPoint (successClip, transform.position);
						projectedLight.intensity = 0f;
					}
				} else if ((!Input.GetKey(KeyCode.UpArrow)) && (!Input.GetKey(KeyCode.LeftArrow)) &&
					(!Input.GetKey(KeyCode.DownArrow)) && (!Input.GetKey(KeyCode.RightArrow)) &&
					(!Input.GetKey(KeyCode.LeftShift)) && (!Input.GetKey(KeyCode.RightShift))) {
					AudioSource.PlayClipAtPoint (failClip, transform.position);
					i = 0;
				}
			}
		}
	}
}
