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

public class SendMessage : MonoBehaviour {

	private GameObject redBlink;
    private GameObject shieldCodeDisplay;
    public AudioClip sendSound;

	// Use this for initialization
	void Start () {	
		redBlink = GameObject.Find("redBlink");
        shieldCodeDisplay = GameObject.Find("shieldCodeDisplay");
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void OnClick()
	{
        if (Dictionaries.dictionaries.activeProgram != null && Dictionaries.dictionaries.currentDisplaySymbols.Count > 0) //If we have an active Program and a message to send
		{
            GetComponent<AudioSource>().PlayOneShot(sendSound);

            string[] activeCodes = {};
			int codePosition = -1;
			
			switch (Dictionaries.dictionaries.activeProgram.GetComponent<ProgramBehavior>().programType)
			{
				
			case 0:
				activeCodes = Dictionaries.dictionaries.shieldcodes.type0Codes;  
				break;
				
			case 1:
				activeCodes = Dictionaries.dictionaries.shieldcodes.type1Codes;  
				
				break;
				
			case 2:
				activeCodes = Dictionaries.dictionaries.shieldcodes.type2Codes;  
				
				break;
				
			case 3:
				activeCodes = Dictionaries.dictionaries.shieldcodes.type3Codes;  
				
				break;
				
			case 4:
				activeCodes = Dictionaries.dictionaries.shieldcodes.type4Codes;  
				
				break;
				
			case 5:
				activeCodes = Dictionaries.dictionaries.shieldcodes.type5Codes;  
				
				break;
				
			}

			if (Dictionaries.dictionaries.currentDictionary.ToString() == Dictionaries.dictionaries.activeProgram.GetComponent<ProgramBehavior>().programType.ToString()) //If the message is in the correct language
			{
				for (int i = 0; i < Dictionaries.dictionaries.shieldcodes.masterCodes.Length; i++)
				{
					if (activeCodes[i] == Dictionaries.dictionaries.messageToSend)
					{
						codePosition = i;
					}

				}
				if (codePosition != -1)
				{
					Dictionaries.dictionaries.activeProgram.GetComponent<ProgramBehavior>().accessGranted = 
						Dictionaries.dictionaries.shieldCode == Dictionaries.dictionaries.shieldcodes.masterCodes[codePosition];
				}
			}
			else //give visual feedback for the wrong language
			{
				//DoRedBlink(0.1f);
			}

            Debug.Log("Message Sent: " + Dictionaries.dictionaries.messageToSend + " in dictionary " + Dictionaries.dictionaries.currentDictionary) ;

            Dictionaries.dictionaries.messageToSend = "";
            foreach (GameObject displaySymbols in Dictionaries.dictionaries.currentDisplaySymbols)
            {
                Destroy(displaySymbols);
            }

            Dictionaries.dictionaries.currentDisplaySymbols.Clear();

            shieldCodeDisplay.GetComponent<ShieldCodeGenerator>().refreshShieldCode();
        }           

	}

	void DoRedBlink (float duration)
	{
		redBlink.SetActive (true);
		StartCoroutine (Blink(duration));

	}

	IEnumerator Blink (float time)
	{
		yield return new WaitForSeconds (time);

		redBlink.SetActive (false);
	}


}
