using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TypeWordRepair : MonoBehaviour
{
    public List<Word> words;

    public WordSpawner wordSpawner;
    public GameObject platform;

    private bool hasActiveWord;
    private Word activeWord;
    public static int wordIndex = 0;
    public static bool finishedTyping = false;
    // Start is called before the first frame update
    void Start()
    {
       wordIndex = 0;
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void Addword()
    {
        Word word = new Word(WordGenerator.GetRandomWord(), wordSpawner.SpawnWord());
        words.Add(word);
    }


    public void TypeLetter (char letter)
    {
        if (hasActiveWord)
        {
            if (activeWord.GetNextletter() == letter)
            {
                activeWord.TypeLetter();
            }
        }
        else
        {
            foreach(Word word in words)
            {
                if (word.GetNextletter() == letter)
                {
                    activeWord = word;
                    hasActiveWord = true;
                    word.TypeLetter();
                }
            }
        }

        if (hasActiveWord && activeWord.WordTyped())
        {
            hasActiveWord = false;
            finishedTyping = true;
            // GameObject.Find("AudioManager").GetComponent<AudioManager>().PlayAudio(0,false,false);
            Debug.Log("Play Audio");
            words.Remove(activeWord);
            platform.transform.GetChild(wordIndex).gameObject.SetActive(true);
            wordSpawner.SetWordCanvas(wordIndex+ 1);
            wordIndex++;
        }
    }
}
