using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Text;
using System.Linq;
using System.Text.RegularExpressions;

public class TypingHandler : MonoBehaviour {
	public GameScript gs;
	public Text text;

	public List<string> Sentences, PlayerSentences, RadioSentences, spareLetters;
	string[] Words;
	string Word, Sentence;
	char Letter;
	int WordCount, LetterCount;
	int CurrentWord, CurrentLetter ;
	AudioSource TypeSound, mmbl, type;
	string alphabet = "abcdefghijklmnopqrstuvwxyz";
	string Intro = "turn on the power";
	string currentTextColor;
	// Use this for initialization
	void Start () {
		mmbl = transform.Find("mmblSound").GetComponent<AudioSource>();
		type = transform.Find("TypeSound").GetComponent<AudioSource>();
		TypeSound = type;
		currentTextColor = "White";
	}

	public string FindSpareLetter() {
		if (spareLetters.Count < 1)
			return "Q";
		string ltr = spareLetters[UnityEngine.Random.Range(0,spareLetters.Count)];
		spareLetters.Remove(spareLetters[0]);
		if (spareLetters.Count < 2)
			PopulateSpareCharacters();
		return ltr.ToUpper();
	}

	public void FirstTimeInit() {
		Sentence = Intro;
		PopulateSpareCharacters();
		Words = Sentence.Split(" "[0]);
		Sentences.Remove(Sentence);
		WordCount = Words.Length;
		CurrentWord = 0;
		NewWord(true);
	}

	public void PopulateSpareCharacters() {
		foreach(char c in alphabet) {
			int count = Regex.Matches(Sentence,c.ToString()).Count;
			//print("There are "+count+c+"'s.");
			if (count == 0) { //If there are only 3 spare characters add some
				spareLetters.Add(c.ToString());
			}
		}
	}

	void SentenceChoice(bool Player) {
		GameObject.Find("Avatar").GetComponent<Animator>().SetBool("Player",Player);
		if (Player) {
			TypeSound = mmbl;
			currentTextColor = "White";
		} else {
			TypeSound = type;
			currentTextColor = "Red";
		}
	}
	
	int SwitchSentences() {
		if (PlayerSentences.Count > 0)
			return 1;
		else if (RadioSentences.Count > 0)
			return 2;
		else
			return 0;
	}

	void Init() {
		if (UnityEngine.Random.Range(0,2) != 0) {
			print("player Natural");
			Sentences = PlayerSentences;
			SentenceChoice(true);
			if (PlayerSentences.Count == 0) { //If this was empty all along
				Sentences = RadioSentences;
				SentenceChoice(false);
			}
		} else {
			Sentences = RadioSentences;
			SentenceChoice(false);
			if (RadioSentences.Count == 0) { //If this was empty all along
				print("player Fix");
				Sentences = PlayerSentences;
				SentenceChoice(true);
			}
		}
		int SentenceNum = UnityEngine.Random.Range(0, Sentences.Count);
		Sentence = Sentences[SentenceNum];
		foreach(char c in alphabet) {
			int count = Regex.Matches(Sentence,c.ToString()).Count;
			//print("There are "+count+c+"'s.");
			if (count == 0) {
				spareLetters.Add(c.ToString());
			}
		}
		Words = Sentence.Split(" "[0]);
		Sentences.Remove(Sentence);
		WordCount = Words.Length;
		CurrentWord = 0;
		NewWord(true);
	}

	void EndIntro() {
		gs.introMode = false;
		FindObjectOfType<LeakHandler>().firstLeak();
		//GameObject.Find("Radio").GetComponent<Animator>().SetBool("On",true);
		GameObject.Find("Sub").GetComponent<Animator>().SetBool("On",true);
	}
	
	void NewWord(bool firstWord) {
		if (!firstWord) { //Move to the Next Word
			CurrentWord++;
			if (!gs.introMode)
				gs.Rise(1); //Rise 1 point after every word. BALANCE.
		}
		if (CurrentWord == WordCount) {
			if (gs.introMode) {
				EndIntro();
			}
			if (Sentences.Count > 0) { //Sentence Over, Get a new one
				gs.Rise(5); //Rise 5 points after completing a sentence. BALANCE.
				Init();
			} else if (SwitchSentences() > 0 ) {
				Init(); //There are more sentences in the other list;
			} else { //No more sentences
				
				print("Player Wins");
				gs.Win();
				CurrentWord = 0;
			}
		}
		Word = Words[CurrentWord]; //Update current word
		text.text = Word;
		LetterCount = Word.Length - 1; //Update LetterCount for new Word
		CurrentLetter = 0; //Start at first letter, for the new word. 
		NewLetter(true); //Start at first letter, for the new word.
	}

	void NewLetter(bool firstLetter) {
		if (!firstLetter) { //Move to the next Letter
			string newText = "<color="+currentTextColor+">"+Word.Substring(0,CurrentLetter+1)+"</color>"+Word.Substring(CurrentLetter+1);
			TypeSound.pitch = UnityEngine.Random.Range(0.5f,1.5f);
			TypeSound.Play();
			text.text = newText;
			CurrentLetter++;
		}
		if (CurrentLetter == LetterCount+1) //If this is the last letter of the word.
			NewWord(false); //Move to the next word
		else 
			Letter = Word[CurrentLetter]; //The word isn't over, move to next char in string.	
	}

	void Update() {
		if (Input.anyKey && !gs.GAMEOVER) {
			string str = Input.inputString.ToLower();
			if (str == Letter.ToString()) {
				NewLetter(false);
			}
		}
	}
}
