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

public enum ControllerButton
{
	A,
	B,
	X,
	Y,
	RB1,
	RB2
}

public class InputManager : MonoBehaviour
{

//	void OnGUI()
//	{
//		Vector2 axis1 = GetJoystick(0);
//		GUILayout.Label("C1 Axis: " + axis1.ToString());
//		Vector2 axis2 = GetJoystick(1);
//		GUILayout.Label("C2 Axis: " + axis2.ToString());
//	}

	private static KeyCode GetKeyCodeForButton(int inPlayerIndex, ControllerButton inButton)
	{
		KeyCode key = KeyCode.None;
		if(inPlayerIndex == 0)
		{
			switch(inButton)
			{
				case ControllerButton.A:
					key = KeyCode.Alpha2;
					break;
				case ControllerButton.B:
					key = KeyCode.Alpha1;
					break;
				case ControllerButton.X:
					key = KeyCode.Alpha3;
					break;
				case ControllerButton.Y:
					key = KeyCode.Alpha4;
					break;
			}
		}
		else if(inPlayerIndex == 1)
		{
			switch(inButton)
			{
				case ControllerButton.A:
					key = KeyCode.Keypad2;
					break;
				case ControllerButton.B:
					key = KeyCode.Keypad1;
					break;
				case ControllerButton.X:
					key = KeyCode.Keypad3;
					break;
				case ControllerButton.Y:
					key = KeyCode.Keypad4;
					break;
			}
		}

		return key;
	}

	public static Vector2 GetJoystick(int inPlayerIndex)
	{
		string xAxis = (inPlayerIndex == 0) ? "Xbox C1 Horizontal" : "Xbox C2 Horizontal";
		string yAxis = (inPlayerIndex == 0) ? "Xbox C1 Vertical" : "Xbox C2 Vertical";

		string xKeyAxis = (inPlayerIndex == 0) ? "Keyboard 1 Horizontal" : "Keyboard 2 Horizontal";
		string yKeyAxis = (inPlayerIndex == 0) ? "Keyboard 1 Vertical" : "Keyboard 2 Vertical";

		Vector2 joystickAxis = new Vector2(Input.GetAxis(xAxis), Input.GetAxis(yAxis));
		Vector2 keyboardAxis = new Vector2(Input.GetAxis(xKeyAxis), Input.GetAxis(yKeyAxis));

		return new Vector2(Mathf.Clamp(joystickAxis.x + keyboardAxis.x, -1f, 1f), Mathf.Clamp(joystickAxis.y + keyboardAxis.y, -1f, 1f));
	}

	public static bool GetButtonDown(int inPlayerIndex, ControllerButton inButton)
	{
		string button = (inPlayerIndex == 0) ? "Xbox C1 " : "Xbox C2 ";
		button += inButton.ToString();

		KeyCode key = GetKeyCodeForButton(inPlayerIndex, inButton);

		return Input.GetButtonDown(button) || Input.GetKeyDown(key);
	}

	public static bool GetButton(int inPlayerIndex, ControllerButton inButton)
	{
		string button = (inPlayerIndex == 0) ? "Xbox C1 " : "Xbox C2 ";
		button += inButton.ToString();

		KeyCode key = GetKeyCodeForButton(inPlayerIndex, inButton);

		return Input.GetButton(button) || Input.GetKey(key);
	}

	public static bool GetButtonUp(int inPlayerIndex, ControllerButton inButton)
	{
		string button = (inPlayerIndex == 0) ? "Xbox C1 " : "Xbox C2 ";
		button += inButton.ToString();

		KeyCode key = GetKeyCodeForButton(inPlayerIndex, inButton);

		return Input.GetButtonUp(button) || Input.GetKeyUp(key);
	}
}
