using UnityEngine;
using System.Collections;

public class TM_InputManager : MonoBehaviour
{
    public static float StickDeadZone = 0.2f;
    static Vector2 vLeftStick;
    static Vector2 vRightStick;

    private static float fRightTrigger;
    private static float fLeftTrigger;


    private bool bDrawDebugInfo = true;

    private static TM_InputManager _instance;


    private static TM_InputManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType<TM_InputManager>();

                if (_instance != null)
                {
                    return _instance;
                }

                GameObject go = new GameObject("TM_InputManager");
                _instance = go.AddComponent<TM_InputManager>();
            }

            return _instance;
        }
    }

	// Use this for initialization
	void Start () 
    {
	
	}
	
	// Update is called once per frame
	void Update ()
	{
        vLeftStick.x = Input.GetAxis("Horizontal");
        vLeftStick.y = Input.GetAxis("Vertical");
        vRightStick.x = Input.GetAxis("Xbox360ControllerRightX");
        vRightStick.y = Input.GetAxis("Xbox360ControllerRightY");

        fRightTrigger = Input.GetAxis("RightTrigger");
        fLeftTrigger = Input.GetAxis("LeftTrigger");
	}

    void OnGUI()
    {
        //if( bDrawDebugInfo )
        //   Debug_DrawStickInfo();
    }

    void Debug_DrawStickInfo()
    {
        GUI.Label(new Rect(32, 32, 400, 32), "LeftStick X,Y: " + vLeftStick.x + "," + vLeftStick.y);
        GUI.Label(new Rect(32, 48, 400, 32), "RightStick X,Y: " + vRightStick.x + "," + vRightStick.y);
        GUI.Label(new Rect(32, 64, 400, 32), "Left Normalized: " + GetLeftStickNormalized());
        GUI.Label(new Rect(32, 80, 400, 32), "Right Normalized: " + GetRightStickNormalized());
        GUI.Label(new Rect(32, 96, 400, 32), "Triggers: " + fLeftTrigger + " | " + fRightTrigger);

    }

    public static Vector2 GetLeftStickNormalized()
    {
        if (vLeftStick.sqrMagnitude < (StickDeadZone * StickDeadZone))
            return Vector2.zero;

        return vLeftStick.normalized;
    }

    public static Vector2 GetLeftStickNormalizedForWorldMovement()
    {
        Vector2 vRet = GetLeftStickNormalized();
        vRet.y *= -1;
        return vRet;
    }

    public static Vector2 GetRightStickNormalized()
    {
        if (vRightStick.sqrMagnitude < (StickDeadZone * StickDeadZone))
            return Vector2.zero;

        return vRightStick.normalized;
    }

    public static Vector2 GetRightStickNormalizedForWorldMovement()
    {
        Vector2 vRet = GetRightStickNormalized();
        vRet.y *= -1;
        return vRet;
    }

    public static float RightTrigger()
    {
        return fRightTrigger;
    }

    public static float LeftTrigger()
    {
        return fLeftTrigger;
    }

}
