#ifndef BUTTONCODE_H
#define BUTTONCODE_H

#ifdef _WIN32
#pragma once
#endif

#include "InputEnums.h"
#include "mathlib.h"

enum
{
	JOYSTICK_MAX_BUTTON_COUNT = 32,
	JOYSTICK_POV_BUTTON_COUNT = 4,
	JOYSTICK_AXIS_BUTTON_COUNT = MAX_JOYSTICK_AXES * 2,
};

#define JOYSTICK_BUTTON_INTERNAL( _joystick, _button ) ( JOYSTICK_FIRST_BUTTON + ((_joystick) * JOYSTICK_MAX_BUTTON_COUNT) + (_button) )
#define JOYSTICK_POV_BUTTON_INTERNAL( _joystick, _button ) ( JOYSTICK_FIRST_POV_BUTTON + ((_joystick) * JOYSTICK_POV_BUTTON_COUNT) + (_button) )
#define JOYSTICK_AXIS_BUTTON_INTERNAL( _joystick, _button ) ( JOYSTICK_FIRST_AXIS_BUTTON + ((_joystick) * JOYSTICK_AXIS_BUTTON_COUNT) + (_button) )

#define JOYSTICK_BUTTON( _joystick, _button ) ( (ButtonCode_t)JOYSTICK_BUTTON_INTERNAL( _joystick, _button ) )
#define JOYSTICK_POV_BUTTON( _joystick, _button ) ( (ButtonCode_t)JOYSTICK_POV_BUTTON_INTERNAL( _joystick, _button ) )
#define JOYSTICK_AXIS_BUTTON( _joystick, _button ) ( (ButtonCode_t)JOYSTICK_AXIS_BUTTON_INTERNAL( _joystick, _button ) )

enum ButtonCode_t
{
	BUTTON_CODE_INVALID = -1,
	BUTTON_CODE_NONE = 0,

	KEY_FIRST = 0,

	KEY_NONE = KEY_FIRST,
	KEY_0,
	KEY_1,
	KEY_2,
	KEY_3,
	KEY_4,
	KEY_5,
	KEY_6,
	KEY_7,
	KEY_8,
	KEY_9,
	KEY_A,
	KEY_B,
	KEY_C,
	KEY_D,
	KEY_E,
	KEY_F,
	KEY_G,
	KEY_H,
	KEY_I,
	KEY_J,
	KEY_K,
	KEY_L,
	KEY_M,
	KEY_N,
	KEY_O,
	KEY_P,
	KEY_Q,
	KEY_R,
	KEY_S,
	KEY_T,
	KEY_U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
	MOUSE_MIDDLE,
	MOUSE_4,
	MOUSE_5,
	MOUSE_WHEEL_UP,		               
	MOUSE_WHEEL_DOWN,	              

	MOUSE_LAST = MOUSE_WHEEL_DOWN,
	MOUSE_COUNT = MOUSE_LAST - MOUSE_FIRST + 1,

	JOYSTICK_FIRST = MOUSE_LAST + 1,

	JOYSTICK_FIRST_BUTTON = JOYSTICK_FIRST,
	JOYSTICK_LAST_BUTTON = JOYSTICK_BUTTON_INTERNAL(MAX_JOYSTICKS - 1, JOYSTICK_MAX_BUTTON_COUNT - 1),
	JOYSTICK_FIRST_POV_BUTTON,
	JOYSTICK_LAST_POV_BUTTON = JOYSTICK_POV_BUTTON_INTERNAL(MAX_JOYSTICKS - 1, JOYSTICK_POV_BUTTON_COUNT - 1),
	JOYSTICK_FIRST_AXIS_BUTTON,
	JOYSTICK_LAST_AXIS_BUTTON = JOYSTICK_AXIS_BUTTON_INTERNAL(MAX_JOYSTICKS - 1, JOYSTICK_AXIS_BUTTON_COUNT - 1),

	JOYSTICK_LAST = JOYSTICK_LAST_AXIS_BUTTON,

#if !defined ( _X360 )
	NOVINT_FIRST = JOYSTICK_LAST + 2,              

	NOVINT_LOGO_0 = NOVINT_FIRST,
	NOVINT_TRIANGLE_0,
	NOVINT_BOLT_0,
	NOVINT_PLUS_0,
	NOVINT_LOGO_1,
	NOVINT_TRIANGLE_1,
	NOVINT_BOLT_1,
	NOVINT_PLUS_1,

	NOVINT_LAST = NOVINT_PLUS_1,
#endif

	BUTTON_CODE_LAST,
	BUTTON_CODE_COUNT = BUTTON_CODE_LAST - KEY_FIRST + 1,

	KEY_XBUTTON_UP = JOYSTICK_FIRST_POV_BUTTON,	  
	KEY_XBUTTON_RIGHT,
	KEY_XBUTTON_DOWN,
	KEY_XBUTTON_LEFT,

	KEY_XBUTTON_A = JOYSTICK_FIRST_BUTTON,		 
	KEY_XBUTTON_B,
	KEY_XBUTTON_X,
	KEY_XBUTTON_Y,
	KEY_XBUTTON_LEFT_SHOULDER,
	KEY_XBUTTON_RIGHT_SHOULDER,
	KEY_XBUTTON_BACK,
	KEY_XBUTTON_START,
	KEY_XBUTTON_STICK1,
	KEY_XBUTTON_STICK2,

	KEY_XSTICK1_RIGHT = JOYSTICK_FIRST_AXIS_BUTTON,	  
	KEY_XSTICK1_LEFT,							  
	KEY_XSTICK1_DOWN,							  
	KEY_XSTICK1_UP,								  
	KEY_XBUTTON_LTRIGGER,						  
	KEY_XBUTTON_RTRIGGER,						  
	KEY_XSTICK2_RIGHT,							  
	KEY_XSTICK2_LEFT,							  
	KEY_XSTICK2_DOWN,							  
	KEY_XSTICK2_UP,								  
};

inline bool IsAlpha(ButtonCode_t code)
{
	return (code >= KEY_A) && (code <= KEY_Z);
}

inline bool IsAlphaNumeric(ButtonCode_t code)
{
	return (code >= KEY_0) && (code <= KEY_Z);
}

inline bool IsSpace(ButtonCode_t code)
{
	return (code == KEY_ENTER) || (code == KEY_TAB) || (code == KEY_SPACE);
}

inline bool IsKeypad(ButtonCode_t code)
{
	return (code >= MOUSE_FIRST) && (code <= KEY_PAD_DECIMAL);
}

inline bool IsPunctuation(ButtonCode_t code)
{
	return (code >= KEY_0) && (code <= KEY_SPACE) && !IsAlphaNumeric(code) && !IsSpace(code) && !IsKeypad(code);
}

inline bool IsKeyCode(ButtonCode_t code)
{
	return (code >= KEY_FIRST) && (code <= KEY_LAST);
}

inline bool IsMouseCode(ButtonCode_t code)
{
	return (code >= MOUSE_FIRST) && (code <= MOUSE_LAST);
}

inline bool IsNovintCode(ButtonCode_t code)
{
#if !defined ( _X360 )
	return ((code >= NOVINT_FIRST) && (code <= NOVINT_LAST));
#else
	return false;
#endif
}

inline bool IsNovintButtonCode(ButtonCode_t code)
{
#if !defined ( _X360 )
	return IsNovintCode(code);
#else
	return false;
#endif
}

inline bool IsJoystickCode(ButtonCode_t code)
{
	return (((code >= JOYSTICK_FIRST) && (code <= JOYSTICK_LAST)) || IsNovintCode(code));
}

inline bool IsJoystickButtonCode(ButtonCode_t code)
{
	return (((code >= JOYSTICK_FIRST_BUTTON) && (code <= JOYSTICK_LAST_BUTTON)) || IsNovintButtonCode(code));
}

inline bool IsJoystickPOVCode(ButtonCode_t code)
{
	return (code >= JOYSTICK_FIRST_POV_BUTTON) && (code <= JOYSTICK_LAST_POV_BUTTON);
}

inline bool IsJoystickAxisCode(ButtonCode_t code)
{
	return (code >= JOYSTICK_FIRST_AXIS_BUTTON) && (code <= JOYSTICK_LAST_AXIS_BUTTON);
}

inline ButtonCode_t GetBaseButtonCode(ButtonCode_t code)
{
	if (IsJoystickButtonCode(code))
	{
		int offset = (code - JOYSTICK_FIRST_BUTTON) % JOYSTICK_MAX_BUTTON_COUNT;
		return (ButtonCode_t)(JOYSTICK_FIRST_BUTTON + offset);
	}

	if (IsJoystickPOVCode(code))
	{
		int offset = (code - JOYSTICK_FIRST_POV_BUTTON) % JOYSTICK_POV_BUTTON_COUNT;
		return (ButtonCode_t)(JOYSTICK_FIRST_POV_BUTTON + offset);
	}

	if (IsJoystickAxisCode(code))
	{
		int offset = (code - JOYSTICK_FIRST_AXIS_BUTTON) % JOYSTICK_AXIS_BUTTON_COUNT;
		return (ButtonCode_t)(JOYSTICK_FIRST_AXIS_BUTTON + offset);
	}

	return code;
}

inline int GetJoystickForCode(ButtonCode_t code)
{
	if (!IsJoystickCode(code))
		return 0;

	if (IsJoystickButtonCode(code))
	{
		int offset = (code - JOYSTICK_FIRST_BUTTON) / JOYSTICK_MAX_BUTTON_COUNT;
		return offset;
	}
	if (IsJoystickPOVCode(code))
	{
		int offset = (code - JOYSTICK_FIRST_POV_BUTTON) / JOYSTICK_POV_BUTTON_COUNT;
		return offset;
	}
	if (IsJoystickAxisCode(code))
	{
		int offset = (code - JOYSTICK_FIRST_AXIS_BUTTON) / JOYSTICK_AXIS_BUTTON_COUNT;
		return offset;
	}

	return 0;
}

inline ButtonCode_t ButtonCodeToJoystickButtonCode(ButtonCode_t code, int nDesiredJoystick)
{
	if (!IsJoystickCode(code) || nDesiredJoystick == 0)
		return code;

	nDesiredJoystick = ::clamp<int>(nDesiredJoystick, 0, MAX_JOYSTICKS - 1);

	code = GetBaseButtonCode(code);

	if (IsJoystickButtonCode(code))
	{
		int nOffset = code - JOYSTICK_FIRST_BUTTON;
		return JOYSTICK_BUTTON(nDesiredJoystick, nOffset);
	}

	if (IsJoystickPOVCode(code))
	{
		int nOffset = code - JOYSTICK_FIRST_POV_BUTTON;
		return JOYSTICK_POV_BUTTON(nDesiredJoystick, nOffset);
	}

	if (IsJoystickAxisCode(code))
	{
		int nOffset = code - JOYSTICK_FIRST_AXIS_BUTTON;
		return JOYSTICK_AXIS_BUTTON(nDesiredJoystick, nOffset);
	}

	return code;
}

#endif  