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

public class EnterCodeUI : MonoBehaviour
{
	public bool working = false;

	public string secretCode;
	public InputField secretInput;
	public Text secretHint;
	public Animator animator;

	static char[] disableChars = new char[] { 'H', 'C', 'S', 'M', 'X' };

	public void Show()
	{
		secretCode = "";
		for (int i = 0; i < Random.Range(3, 6); i++)
		{
			char cc;
			do
			{
				cc = (char)Random.Range('A', 'Z');
			} while (System.Array.IndexOf(disableChars, cc) >= 0);

			secretCode += cc;
		}

		secretHint.text = secretCode;
		secretInput.text = "";

		secretInput.Select();
		secretInput.ActivateInputField();

		animator.Play("enter");
		working = true;
	}

	public void Hide()
	{
		animator.Play("exit");
		working = false;
	}

	public void SetInputFocus()
	{
		secretInput.Select();
		secretInput.ActivateInputField();
	}

	public void OnTextChange()
	{
		if (!working)
			return;

		if (secretInput.text.ToUpper() == secretCode.ToUpper())
		{
			Hide();
			Player.LocalPlayer.EnterKeyQTE();
		}
	}

	public void OnConfirm()
	{
		if (!working)
			return;

		if (secretInput.text.ToUpper() == secretCode.ToUpper())
		{
			Hide();
			Player.LocalPlayer.EnterKeyQTE();
		}
		else
		{
			secretInput.text = "";
			secretInput.Select();
			secretInput.ActivateInputField();
		}
	}
}
