#include "input_class.h"


input_class::input_class()
{
	m_directInput = 0;
	m_keyboard = 0;
	m_mouse = 0;
	m_previousTildeState = false;
}


input_class::input_class(const input_class& other)
{
}


input_class::~input_class()
{
}

bool input_class::Initialize(HINSTANCE hinstance, HWND hwnd, int screenWidth, int screenHeight)
{
	LOG_INIT("Initializing input class");

	HRESULT result;
	int i;

	// Initialize all the keys to being released and not pressed.
	for (i = 0; i < 256; i++)
	{
		m_keys[i] = false;
	}

	// Store the screen size which will be used for positioning the mouse cursor.
	m_screenWidth = screenWidth;
	m_screenHeight = screenHeight;

	// Initialize the location of the mouse on the screen.
	m_mouseX = 0;
	m_mouseY = 0;

	// Initialize the main direct input interface.
	result = DirectInput8Create(hinstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_directInput, NULL);
	if (FAILED(result))
	{
		LOG_ERROR("Failed to create direct input interface");
		R_FALSE
	}

	// Initialize the direct input interface for the keyboard.
	result = m_directInput->CreateDevice(GUID_SysKeyboard, &m_keyboard, NULL);
	if (FAILED(result))
	{
		LOG_ERROR("Failed to create direct input interface for the keyboard");
		R_FALSE
	}

	// Set the data format. In this case since it is a keyboard we can use the predefined data format.
	result = m_keyboard->SetDataFormat(&c_dfDIKeyboard);
	if (FAILED(result))
	{
		LOG_ERROR("Failed to set data format for the keyboard");
		R_FALSE
	}

	// Set the cooperative level of the keyboard to share with other programs.
	result = m_keyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
	if (FAILED(result))
	{
		LOG_ERROR("Failed to set cooperative level of the keyboard");
		R_FALSE
	}

	// Now acquire the keyboard.
	result = m_keyboard->Acquire();
	if (FAILED(result))
	{
		LOG_ERROR("Failed to acquire the keyboard");
		R_FALSE
	}

	// Initialize the direct input interface for the mouse.
	result = m_directInput->CreateDevice(GUID_SysMouse, &m_mouse, NULL);
	if (FAILED(result))
	{
		LOG_ERROR("Failed to create direct input interface for the mouse");
		R_FALSE
	}

	// Set the data format for the mouse using the pre-defined mouse data format.
	result = m_mouse->SetDataFormat(&c_dfDIMouse);
	if (FAILED(result))
	{
		LOG_ERROR("Failed to set data format for the mouse");
		R_FALSE
	}

	// Set the cooperative level of the mouse to share with other programs.
	result = m_mouse->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
	if (FAILED(result))
	{
		LOG_ERROR("Failed to set cooperative level of the mouse");
		R_FALSE
	}

	// Acquire the mouse.
	result = m_mouse->Acquire();
	if (FAILED(result))
	{
		LOG_ERROR("Failed to acquire the mouse");
		R_FALSE
	}

	LOG_INIT("Input class initialized");

	R_TRUE
}


void input_class::KeyDown(unsigned int input)
{
	// If a key is pressed then save that state in the key array.
	LOG("Key down: " + std::to_string(input), Logger::LogLevel::Input);
	m_keys[input] = true;
	return;
}


void input_class::KeyUp(unsigned int input)
{
	// If a key is released then clear that state in the key array.
	m_keys[input] = false;
	return;
}


bool input_class::IsKeyDown(unsigned int key) const
{
	// Return what state the key is in (pressed/not pressed).
	return m_keys[key];
}

void input_class::Shutdown()
{
	LOG_SHUTDOWN("Shutting down input class");

	// Release the mouse.
	if (m_mouse)
	{
		m_mouse->Unacquire();
		m_mouse->Release();
		m_mouse = 0;
	}

	// Release the keyboard.
	if (m_keyboard)
	{
		m_keyboard->Unacquire();
		m_keyboard->Release();
		m_keyboard = 0;
	}

	// Release the main interface to direct input.
	if (m_directInput)
	{
		m_directInput->Release();
		m_directInput = 0;
	}

	LOG_SHUTDOWN("Input class shut down");

	return;
}

bool input_class::Frame()
{
	bool result;

	// Read the current state of the keyboard.
	result = ReadKeyboard();
	if (!result)
	{
		LOG_ERROR("Failed to read keyboard state");
		R_FALSE
	}

	// Read the current state of the mouse.
	result = ReadMouse();
	if (!result)
	{
		LOG_ERROR("Failed to read mouse state");
		R_FALSE
	}

	// Process the changes in the mouse and keyboard.
	ProcessInput();

	R_TRUE
}

bool input_class::ReadKeyboard()
{
	HRESULT result;


	// Read the keyboard device.
	result = m_keyboard->GetDeviceState(sizeof(m_keyboardState), (LPVOID)&m_keyboardState);
	if (FAILED(result))
	{
		// If the keyboard lost focus or was not acquired then try to get control back.
		if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
		{
			m_keyboard->Acquire();
		}
		else
		{
			LOG_ERROR("Failed to get keyboard device state");
			R_FALSE
		}
	}

	R_TRUE
}

bool input_class::ReadMouse()
{
	HRESULT result;


	// Read the mouse device.
	result = m_mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&m_mouseState);
	if (FAILED(result))
	{
		// If the mouse lost focus or was not acquired then try to get control back.
		if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
		{
			m_mouse->Acquire();
		}
		else
		{
			LOG_ERROR("Failed to get mouse device state");
			R_FALSE
		}
	}

	R_TRUE
}

void input_class::ProcessInput()
{
	// Update the location of the mouse cursor based on the change of the mouse location during the frame.
	m_mouseX += m_mouseState.lX;
	m_mouseY += m_mouseState.lY;

	//// Ensure the mouse location doesn't exceed the screen width or height.
	//if (m_mouseX < 0) { m_mouseX = 0; }
	if (m_mouseY < -m_screenHeight) { m_mouseY = -m_screenHeight; }

	//if (m_mouseX > m_screenWidth) { m_mouseX = m_screenWidth; }
	if (m_mouseY > m_screenHeight) { m_mouseY = m_screenHeight; }

	return;
}

bool input_class::IsEscapePressed() const
{
	// Do a bitwise and on the keyboard state to check if the escape key is currently being pressed.
	if (m_keyboardState[DIK_ESCAPE] & 0x80)
	{
		R_TRUE
	}

	R_FALSE
}

bool input_class::IsLeftArrowPressed() const
{
	if (m_keyboardState[DIK_LEFT] & 0x80)
	{
		R_TRUE
	}

	R_FALSE
}


bool input_class::IsRightArrowPressed() const
{
	if (m_keyboardState[DIK_RIGHT] & 0x80)
	{
		R_TRUE
	}

	R_FALSE
}

bool input_class::IsUpArrowPressed() const
{
	if (m_keyboardState[DIK_UP] & 0x80)
	{
		R_TRUE
	}

	R_FALSE
}


bool input_class::IsDownArrowPressed() const
{
	if (m_keyboardState[DIK_DOWN] & 0x80)
	{
		R_TRUE
	}

	R_FALSE
}

///////////////////////////////////////////////////
// Les touches correspondent aux claviers QWERTY //
///////////////////////////////////////////////////

void input_class::GetMouseLocation(int& mouseX, int& mouseY) const
{
	mouseX = m_mouseX;
	mouseY = m_mouseY;
	return;
}

bool input_class::IsLeftMousePressed() const
{
	// Check the left mouse button state.
	if (m_mouseState.rgbButtons[0] & 0x80)
	{
		R_TRUE
	}

	R_FALSE
}

bool input_class::IsRightMousePressed() const
{
	// Check the left mouse button state.
	if (m_mouseState.rgbButtons[1] & 0x80)
	{
		R_TRUE
	}

	R_FALSE
}

bool input_class::IsScrollUp() const
{
	if (m_mouseState.lZ > 0)
	{
		R_TRUE
	}

	R_FALSE
}

bool input_class::IsScrollDown() const
{
	if (m_mouseState.lZ < 0)
	{
		R_TRUE
	}

	R_FALSE
}

bool input_class::is_key_pressed(const unsigned int key_code)
{
	return m_keyboardState[key_code] & 0x80;
}
