using UnityEngine;
using System.Collections;

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

    private bool bDrawDebugInfo = true;

    private static bool bShootIsDown;
    
    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("LeftStickX");
        vLeftStick.y = Input.GetAxis("LeftStickY");
        vRightStick.x = Input.GetAxis("RightStickX");
        vRightStick.y = Input.GetAxis("RightStickY");
	}
    /*
    void OnGUI()
    {
        if( bDrawDebugInfo )
            Debug_DrawStickInfo();
    }
    */

    void Debug_DrawStickInfo()
    {
        GUI.Label(new Rect(32, 32, 400, 32), "LeftStick X,Y: " + Input.GetAxis("LeftStickX") + "," + Input.GetAxis("LeftStickY"));
        GUI.Label(new Rect(32, 48, 400, 32), "RightStick X,Y: " + Input.GetAxis("RightStickX") + "," + Input.GetAxis("RightStickY"));
        GUI.Label(new Rect(32, 64, 400, 32), "Left Normalized: " + GetLeftStickNormalized());
        GUI.Label(new Rect(32, 80, 400, 32), "Right Normalized: " + GetRightStickNormalized());
        
    }

    public static Vector2 GetMoveVector()
    {
        Vector2 vMoveVec = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        if (vMoveVec == Vector2.zero)
        {
            vMoveVec = GetLeftStickNormalized();
        }

        return vMoveVec;
    }

    public static Vector2 GetAimVector()
    {

        Vector2 vAimVec = new Vector2(Input.GetAxis("HorizontalArrows"), Input.GetAxis("VerticalArrows"));
        if (vAimVec == Vector2.zero)
        {
            vAimVec = GetRightStickNormalizedForWorldMovement();
        }

        return vAimVec;
    }

    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 bool IsShiftPressed()
    {
        return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
    }

    public static bool IsAnyFireKeyPressed( bool bRequireShift)
    {
        //If we need to press shift and aren't pressing it, get out
        if( bRequireShift != IsShiftPressed())
        {
            return false;
        }

        if (
            Input.GetKey(KeyCode.Space) ||
            Input.GetKey(KeyCode.RightArrow) ||
            Input.GetKey(KeyCode.LeftArrow) ||
            Input.GetKey(KeyCode.UpArrow) ||
            Input.GetKey(KeyCode.DownArrow) 
            )
        {
            return true;
        }

        return false;
    }

    public static bool IsAnyFireTriggerPulled(bool bCheckLeftTriggerInsteadOfRight)
    {
        float fTriggerAxis = 0; //bCheckLeftTriggerInsteadOfRight ? Input.GetAxis("LeftTrigger") : Input.GetAxis("RightTrigger");
        return fTriggerAxis > 0;
    }

    public static bool IsShoppingPlacementButtonDown()
    {
        bool bDownThisFrame = Input.GetButtonDown("Submit");

        //check triggers
        if (IsAnyFireTriggerPulled(false))
        {
            //if we aren't already in shoot mode, then we must have just pulled it.
            if (!bShootIsDown)
            {
                bDownThisFrame = true;
            }

            bShootIsDown = true;
        }
        else
        {
            bShootIsDown = false;
        }

        return bDownThisFrame;
    }

    public static bool IsEscapePressed()
    {
        if (Input.GetButtonDown("Cancel"))
        {
            return true;
        }
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            return true;
        }
        return false;
    }

    public static Vector2 GetArrowKeysMenuInput()
    {
        if( !Input.GetKeyDown(KeyCode.RightArrow) &&
            !Input.GetKeyDown(KeyCode.LeftArrow) &&
            !Input.GetKeyDown(KeyCode.UpArrow) &&
            !Input.GetKeyDown(KeyCode.DownArrow))
        {
            return Vector2.zero;
        }

        return new Vector2(Input.GetAxis("Menu Horizontal"), Input.GetAxis("Menu Vertical"));
    }

}
