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

public class TypingManager : MonoBehaviour {
    public MainMenuHandler menu;
    public GameObject textCardPrefab;
    public GameObject typingPanel;
    public int charsLong = 6;
    public int cardSpacing = 5;

    private string possibleChars = "abcdefghijklmnopqrstuvwxyz";
    private List<GameObject> textCards = new List<GameObject>();
    private int textCardsCompleted;

    private float textCardHeight;


    void Awake()
    {
        textCardHeight = textCardPrefab.GetComponent<RectTransform>().rect.height;
    }


    void Start()
    {
        ResetTypingManager();
    }


    void Update()
    {
        if (!menu.inMenu)
        {
            typingPanel.GetComponent<Image>().enabled = true;
            char keyDown = ReadKeyDown();
            if (possibleChars.Contains(keyDown.ToString()) && textCards.Count > 0)
            {
                GameObject currentTextCardGO = textCards[0];
                TextCard currentTextCard = textCards[0].GetComponent<TextCard>();

                if (currentTextCard.GetCurrentCharacter() == keyDown)
                {
                    currentTextCard.currentChar += 1;
                }
                else
                {
                    currentTextCard.currentChar = 0;
                }

                if (currentTextCard.ShouldRemove())
                {
                    textCards.RemoveAt(0);
                    currentTextCard.GetComponent<TextCard>().Recipient.GetComponent<Employee>().papersInFax--;
                    Destroy(currentTextCardGO);
                    textCardsCompleted++;
                }

                currentTextCard.UpdateTextUI();
                SetAllTextCardPositions();
            }
        }
    }


    // ResetTypingManager, TextCardsInList, TextCardsCompleted, AddTextCard
    public void ResetTypingManager()
    {
        textCardsCompleted = 0;
        //AddTextCard();
        SetAllTextCardPositions();
    }


    public int TextCardsInList()
    {
        return textCards.Count;
    }


    public int TextCardsCompleted()
    {
        return textCardsCompleted;
    }


    public void AddTextCard(GameObject obj)
    {
        GameObject newTextCard = Instantiate(textCardPrefab, typingPanel.transform);
        textCards.Add(newTextCard);
		newTextCard.GetComponent<TextCard> ().Recipient = obj;
        SetTextCardText(newTextCard, GenerateRandomString(charsLong));
		SetAllTextCardPositions();
    }
    



    private char ReadKeyDown()
    {
        foreach(KeyCode kcode in Enum.GetValues(typeof(KeyCode)))
        {
            if (Input.GetKeyDown(kcode))
            {
                return (char)kcode;
            }
        }

        return '~';
    }


    private void SetAllTextCardPositions()
    {
        for(int i = 0; i < textCards.Count; i++)
        {
            SetTextCardPosition(textCards[i], i);
        }
    }


    private void SetTextCardText(GameObject textCard, string t)
    {
        textCard.GetComponent<TextCard>().SetText(t);
    }


    private void SetTextCardPosition(GameObject textCard, int index)
    {
        RectTransform textCardRT = textCard.GetComponent<RectTransform>();

        textCardRT.anchoredPosition = new Vector2(0, (index+1) * textCardHeight + (cardSpacing * index));
    }


    private char PickRandomCharacter()
    {
        return possibleChars[UnityEngine.Random.Range(0, possibleChars.Length)];
    }


    private string GenerateRandomString(int length)
    {
        char[] randomChars = new char[length];

        for (int i = 0; i < length; i++)
        {
            randomChars[i] = PickRandomCharacter();
        }

        return new string(randomChars);
    }
	
}
