using UnityEngine;
using System.Collections;

public class Controller : MonoBehaviour {

    public bool invert;

    public InputManager inputManager;

	//Have to assume Controllers are connected first?
	//Plug in Unity Input axes for the axes?


    public string VerticalAxisRight;
    public string HorizontalAxisRight;

    public string VerticalAxisLeft;
    public string HorizontalAxisLeft;

	public string TriggerRightAxis;

	/*
    public string DPadVerticalAxis;
    public string DPadHorizontalAxis;

    
    public string TriggerLeftAxis;
	//*/

	public KeyCode ButtonUp;
    public KeyCode ButtonDown;
    public KeyCode ButtonRight;
    public KeyCode ButtonLeft;

    public KeyCode BumperRight;
    public KeyCode BumperLeft;

    public KeyCode ButtonLeftStick;
    public KeyCode ButtonRightStick;
    
    public KeyCode start;
    public KeyCode select;

	// Use this for initialization
	void Start ()
    {
        
	}

	// Update is called once per frame
	void Update() {

		//Poll the controller
		//If proper key is pressed message the inputManager
		//InputManager messages the proper game object

		#region Old Thumbstick
		/*
        //Axis Left Stick Control
        if(Input.GetAxisRaw(VerticalAxisLeft) > 0.4f)
        {
			//Debug.Log("VerticalLeft");
			if (!invert)
            {
                InputManager.KeyPress(ControllerInput.LeftStickUp, this);
            }
            else
            {
                InputManager.KeyPress(ControllerInput.LeftStickDown, this);
            }
        }
        else if(Input.GetAxisRaw(VerticalAxisLeft) < -0.4f)
        {
			//Debug.Log("VerticalLeft");
			if (!invert)
            {
                InputManager.KeyPress(ControllerInput.LeftStickDown, this);
            }
            else
            {
                InputManager.KeyPress(ControllerInput.LeftStickUp, this);
            }
        }

        if(Input.GetAxisRaw(HorizontalAxisLeft) > 0.4f)
        {
			//Debug.Log("HorizontalLeft");
            InputManager.KeyPress(ControllerInput.LeftStickRight, this);
        }
        else if(Input.GetAxisRaw(HorizontalAxisLeft) < -0.4f)
        {
			//Debug.Log("HorizontalLeft");
			InputManager.KeyPress(ControllerInput.LeftStickLeft, this);
        }

	//*/
		//Axis Right Stick Control
		//if(Input.GetAxisRaw(VerticalAxisRight) > 0.5f)
		//{
		//    InputManager.KeyPress(ControllerInput.RightStickUp, this);
		//}
		//else if(Input.GetAxisRaw(VerticalAxisRight) < -0.5f)
		//{
		//    InputManager.KeyPress(ControllerInput.RightStickDown, this);
		//}

		//if(Input.GetAxisRaw(HorizontalAxisRight) > 0.5f)
		//{
		//    InputManager.KeyPress(ControllerInput.RightStickRight, this);
		//}
		//else if(Input.GetAxisRaw(HorizontalAxisRight) < -0.5f)
		//{
		//    InputManager.KeyPress(ControllerInput.RightStickLeft, this);
		//}

		#endregion

		float thumbLeft_horiz = Input.GetAxisRaw(HorizontalAxisLeft);
		float thumbLeft_vert = Input.GetAxisRaw(VerticalAxisLeft);

		float thumbRight_horiz = Input.GetAxisRaw(HorizontalAxisRight);
		float thumbRight_vert = Input.GetAxisRaw(VerticalAxisRight);

		//If ThumbstickLeft
		if (Mathf.Abs(thumbLeft_horiz) > 0.6f
			|| Mathf.Abs(thumbLeft_vert) > 0.6f)
		{
			//Debug.Log("Controler ThumbstickLeft");
			//Debug.Log("Vert = " + thumbLeft_vert + " , Horiz = " + thumbLeft_horiz);
			InputManager.ThumbstickLeft(thumbLeft_vert, thumbLeft_horiz, this);
		}

		//*
		//If ThumbstickRight
		if (Mathf.Abs(thumbRight_horiz) > 0.6f
			|| Mathf.Abs(thumbRight_vert) > 0.6f)
		{
			//Debug.Log("Controler ThumbstickRight");
			//Debug.Log("RIGHT: Vert = " + thumbRight_vert + " , Horiz = " + thumbRight_horiz);
			InputManager.ThumbstickRight(Input.GetAxisRaw(VerticalAxisRight), Input.GetAxisRaw(HorizontalAxisRight), this);
		}
        //*/

        //Axis DPad Control
        //if(Input.GetAxisRaw(DPadVerticalAxis) > 0.1f)
        //{
        //    InputManager.KeyPress(ControllerInput.DPadUp, this);
        //}
        //else if(Input.GetAxisRaw(DPadVerticalAxis) < -0.1f)
        //{
        //    InputManager.KeyPress(ControllerInput.DPadDown, this);
        //}

        //if(Input.GetAxisRaw(DPadHorizontalAxis) > 0.1f)
        //{
        //    InputManager.KeyPress(ControllerInput.DPadRight, this);
        //}
        //else if(Input.GetAxisRaw(DPadHorizontalAxis) < -0.1f)
        //{
        //    InputManager.KeyPress(ControllerInput.DPadLeft, this);
        //}

        //Axis Right Trigger Control


        //AN IF/ELSE HERE? JESUS FUCKING CHRIST MAN THINK ABOUT THIS ONE, YOU'RE IN THE UPDATE LOOP
        //Needs a currentState/previousState check to avoid hitting every frame trigger is down
        //Debug.Log("TrigerRightAxis = " + Input.GetAxisRaw(TriggerRightAxis));
        //if (Input.GetAxisRaw(TriggerRightAxis) > 0.1f)	//negative numbers for shared both-trigger axis
        //{
        //    Debug.Log("TRIGGER PULL");
        //    InputManager.KeyPress(ControllerInput.TriggerRight, this);
        //    //Debug.Log("TrigerRightAxis = " + Input.GetAxisRaw(TriggerRightAxis));
        //}
		//else
		//{
		//    //InputManager.KeyPress(ControllerInput.TriggerRightUp, this);
		//}

		/*
		//Axis Left Trigger Control
		if (Input.GetAxisRaw(TriggerLeftAxis) > 0.4f)
        {
            //Debug.Log("TRIGGER LEFT");
            InputManager.KeyPress(ControllerInput.TriggerLeft, this);
        }
        else
        {
            InputManager.KeyPress(ControllerInput.TriggerLeftUp, this);
        }
		//*/

		//Button Up Control
		if (Input.GetKey(ButtonUp))
		{
			InputManager.KeyPress(ControllerInput.ButtonUp, this);
		}

		//Button Down Control
		if (Input.GetKey(ButtonDown))
        {
            InputManager.KeyPress(ControllerInput.ButtonDown, this);
        }
		//if (Input.GetKeyUp(ButtonDown))
		//{
		//    InputManager.KeyPress(ControllerInput.ButtonDownRelease, this);
		//}

		//Button Left Control
		if (Input.GetKey(ButtonLeft))
		{
			InputManager.KeyPress(ControllerInput.ButtonLeft, this);
		}

		//Button Right Control
		if (Input.GetKey(ButtonRight))
        {
            InputManager.KeyPress(ControllerInput.ButtonRight, this);
        }
		//if(Input.GetKeyUp(ButtonRight))
		//{
		//    //InputManager.KeyPress(ControllerInput.ButtonRightRelease, this);
		//}

		//Button Right Bumper
		if (Input.GetKey(BumperRight))
		{
			InputManager.KeyPress(ControllerInput.BumperRight, this);
		}

		//Button Left Bumper
		if (Input.GetKey(BumperLeft))
		{
			InputManager.KeyPress(ControllerInput.BumperLeft, this);
		}

		//Button Right Stick
		//if (Input.GetKey(ButtonRightStick))
		//{
		//    InputManager.KeyPress(ControllerInput.ButtonRightStick, this);
		//}

		//Button Left Stick
		//if (Input.GetKey(ButtonLeftStick))
		//{
		//    InputManager.KeyPress(ControllerInput.ButtonLeftStick, this);
		//}

		//Button Start
		//if (Input.GetKey(start))
		//{
		//    InputManager.KeyPress(ControllerInput.CntStart, this);
		//}

		//Button Select
		//if (Input.GetKey(select))
		//{
		//    InputManager.KeyPress(ControllerInput.CntSelect, this);
		//}

	}

	public void SetAxisMappings(int controllerNumber)
	{
		HorizontalAxisLeft = "HorizLeft" + (controllerNumber + 1).ToString();
		VerticalAxisLeft = "VertLeft" + (controllerNumber + 1).ToString();
		HorizontalAxisRight = "HorizRight" + (controllerNumber + 1).ToString();
		VerticalAxisRight = "VertRight" + (controllerNumber + 1).ToString();
		TriggerRightAxis = "Cancel";
		switch (controllerNumber)
		{
			case (0):
				ButtonDown	= KeyCode.Joystick1Button0;
				ButtonRight = KeyCode.Joystick1Button1;
				ButtonLeft	= KeyCode.Joystick1Button2;
				ButtonUp	= KeyCode.Joystick1Button3;
				BumperLeft	= KeyCode.Joystick1Button4;
				BumperRight = KeyCode.Joystick1Button5;
				break;
			case (1):
				ButtonDown	= KeyCode.Joystick2Button0;
				ButtonRight = KeyCode.Joystick2Button1;
				ButtonLeft	= KeyCode.Joystick2Button2;
				ButtonUp	= KeyCode.Joystick2Button3;
				BumperLeft	= KeyCode.Joystick2Button4;
				BumperRight = KeyCode.Joystick2Button5;
				break;
			case (2):
				ButtonDown	= KeyCode.Joystick3Button0;
				ButtonRight = KeyCode.Joystick3Button1;
				ButtonLeft	= KeyCode.Joystick3Button2;
				ButtonUp	= KeyCode.Joystick3Button3;
				BumperLeft	= KeyCode.Joystick3Button4;
				BumperRight = KeyCode.Joystick3Button5;
				break;
			case (3):
				ButtonDown	= KeyCode.Joystick4Button0;
				ButtonRight = KeyCode.Joystick4Button1;
				ButtonLeft	= KeyCode.Joystick4Button2;
				ButtonUp	= KeyCode.Joystick4Button3;
				BumperLeft	= KeyCode.Joystick4Button4;
				BumperRight = KeyCode.Joystick4Button5;
				break;
			default:
				Debug.Log("SetAxisMappings fucked up");
				break;
		}
		
	}
}
