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

public class GuiStatusText : MonoBehaviour {

    public static GuiStatusText ms_instance;
    public static GuiStatusText Instance
    {
        get { return ms_instance; }
    }

    string m_displayText = "";
    float m_timeoutLeft;

    bool m_hasClicked;
    bool m_lastMouse;

    const int WIDTH = 450;
    const int HEIGHT = 80;

    public const float DEFAULT_TIME = 0.25f;
    public const float ATTACK_TIME = 0.25f;

    public static void PurgeText()
    {
        Instance.m_timeoutLeft = 0.0f;
    }

    public static void DisplayText(string text, float forTime)
    {
        Instance.m_displayText = text;
        Instance.m_timeoutLeft = forTime;

        // Hacking this to make the mouse skip the messages. Using the timeout now just for debouncing
        Instance.m_timeoutLeft = 0.25f;
        Instance.m_hasClicked = false;
    }

    public static void AppendText( string append )
    {
        Instance.m_displayText += append;
        
        // Hacking this to make the mouse skip the messages. Using the timeout now just for debouncing
        Instance.m_timeoutLeft = 0.25f;
        Instance.m_hasClicked = false;
    }

    public static bool CanContinue
    {
        get 
        {
            return (Instance.m_hasClicked || (Instance.m_displayText.Length == 0 ));        
        }
    }

	// Use this for initialization
	void Start ()
    {
        if (ms_instance != null)
        {
            Debug.LogError("Only one statustext allowed");
        }
        ms_instance = this;	
	}
	
	// Update is called once per frame
	void Update () 
    {
        bool newMouse = Input.GetMouseButton(0);

        if ( newMouse != m_lastMouse )
        {
            if (m_timeoutLeft < 0)
            {
                if (newMouse)
                {
                    m_hasClicked = true;
                    m_displayText = "";
                }
            }
        }

        m_lastMouse = newMouse;

	}

    void OnGUI()
    {
        if (m_timeoutLeft > 0)
        {
            m_timeoutLeft -= Time.deltaTime;
        }
        
        if ( m_displayText.Length > 0 )
        {
            GUI.Box(new Rect((Screen.width - WIDTH) / 2, Screen.height - HEIGHT, WIDTH, HEIGHT), m_displayText);
        }
    }

}
