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

public class DictionaryKeyBehavior : MonoBehaviour {

	public char keyId;
    public AudioClip pressDictionaryKeySound;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

	}


	public void onClick()
	{
        GetComponent<AudioSource>().PlayOneShot(pressDictionaryKeySound);

        shuffleSymbolKeyCodes();

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

		Dictionaries.dictionaries.currentDictionary = keyId;

        for(int i=0; i< Dictionaries.dictionaries.symbolKeys.Length; i++)
        {
            Dictionaries.dictionaries.symbolKeys[i].name = Dictionaries.dictionaries.symbolKeyCodes[i].ToString();
            Dictionaries.dictionaries.symbolKeys[i].GetComponentInChildren<Image>().sprite = 
                Resources.Load<Sprite>("Symbols/keyboard_" + keyId + Dictionaries.dictionaries.symbolKeys[i].name);
        }
    }

    private void shuffleSymbolKeyCodes()
    {
        int length = Dictionaries.dictionaries.symbolKeyCodes.Length;

        while (length > 1)
        {
            length--;
            int index = Random.Range(0, length + 1);
            char key = Dictionaries.dictionaries.symbolKeyCodes[index];
            Dictionaries.dictionaries.symbolKeyCodes[index] = Dictionaries.dictionaries.symbolKeyCodes[length];
            Dictionaries.dictionaries.symbolKeyCodes[length] = key;
        }
    }
}
