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

public class CharacterUserInput : MonoBehaviour
{
    private BaseCharacterController baseCharacter;
    public AICharacterInput aiCharacter;
    public PauseMenu pauseMenu;
    BaseCharacterController aiController;
    public Text textBox;

    float horizontal;
    float vertical;

    // Use this for initialization
    void Awake ()
    {
        baseCharacter = GetComponent<BaseCharacterController>();
        aiController = GameObject.Find("Companion").GetComponent<BaseCharacterController>();
	}
	
	// Update is called once per frame
	void Update ()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");

        if (Input.GetButtonDown("Call"))
        {
            baseCharacter.playerAnimator.SetTrigger("Wave");
            if (aiCharacter.ToggleCall())
            {
                ShowText("Follow");
            }
            else
            {
                ShowText("Wait");
            }
        }

        if (Input.GetButtonDown("CompanionJump"))
        {
            aiController.Jump = true;
            aiController.state = BaseCharacterController.STATE.INAIR;
            baseCharacter.playerAnimator.SetTrigger("CompanionJump");
            ShowText("Jump");
        }

        if (Input.GetButtonDown("Pause"))
        {
            
            if (!pauseMenu.GetPaused())
            {
                pauseMenu.gameObject.SetActive(true);
                pauseMenu.Pause();
            } 
            else
            {
                pauseMenu.UnPause();
            }  
        }

        baseCharacter.Move(horizontal, vertical);

        if(showText)
        {
            textBox.transform.parent.gameObject.SetActive(true);
            textBox.text = message;
            textCounter -= Time.deltaTime;
            if(textCounter < 0)
            {
                showText = false;
            }
        }
        else
        {
            textBox.transform.parent.gameObject.SetActive(false);
        }
    }

    bool showText = false;
    float textCounter = 0;
    string message;

    void ShowText(string message)
    {
        textCounter = 1.0f;
        showText = true;
        this.message = message;
    }
}
