using UnityEngine;
using System.Collections;

public class ComboChecker : MonoBehaviour {

    public int inputNumber = 0, inputCounter = 0;

    public ComboGenerator comboGen;
    public bool isChecking = false;

    void Awake()
    {
        comboGen = gameObject.GetComponent<ComboGenerator>();
    }

    void OnEnable()
    {
        EventManager.StartListening(Constants.CheckForInput, CheckForInput);
    }

    void OnDisable()
    {
        EventManager.StopListening(Constants.CheckForInput, CheckForInput);
    }

    public void CheckForInput()
    {
        if (inputCounter < 4)
        {
            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                
                inputNumber = 1;
                inputCounter++;

                CheckPressedKey(inputNumber, inputCounter);
            }
            else if (Input.GetKeyDown(KeyCode.Alpha2))
            {
                inputNumber = 2;
                inputCounter++;

                CheckPressedKey(inputNumber, inputCounter);

            }
            else if (Input.GetKeyDown(KeyCode.Alpha3))
            {
                inputNumber = 3;
                inputCounter++;

                CheckPressedKey(inputNumber, inputCounter);

            }
            else if (Input.GetKeyDown(KeyCode.Alpha4))
            {
                inputNumber = 4;
                inputCounter++;

                CheckPressedKey(inputNumber, inputCounter);

            }
        }
        else
        {
            inputCounter = 0;
            inputNumber = 0;
            EventManager.TriggerEvent(Constants.CombinationSuccessful);
        }
       
    }

    void CheckPressedKey(int number, int counter)
    {
        if (CheckCombo(inputNumber, inputCounter - 1))
        {
            EventManager.TriggerEvent(Constants.CorrectInput);
            return;
        }
        else
        {
            EventManager.TriggerEvent(Constants.IncorrectInput);
            inputCounter = 0;
            inputNumber = 0;
            EventManager.TriggerEvent(Constants.CombinationFailed);

        }
    }

    bool CheckCombo(int input, int iteration)
    {
        if (input == comboGen.generatedCombo[iteration])
        {
            return true;
        }

        return false;
    }
}
