/*
	Ripatti Artem (c) 2011
*/

#define _CRT_SECURE_NO_WARNINGS

#include "ie_dinput8.h"
#include "log.h"

namespace ie
{
	InputEngineDInput8::InputEngineDInput8(HWND hWnd)
	{
		g_Input=0;
		g_Mouse=0;
		g_Keyboard=0;

		h_wnd = hWnd;

		if( FAILED( DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_Input, NULL) ))
			Error("IE", "Cannot create DInput8");

		if( FAILED( g_Input->CreateDevice(GUID_SysMouse, &g_Mouse, NULL)))
			Error("IE", "Cannot create mouse device");

		if( FAILED( g_Mouse->SetDataFormat(&c_dfDIMouse)))
			Error("IE", "Cannot set data format for mouse");

		if( FAILED( g_Mouse->SetCooperativeLevel (hWnd, DISCL_BACKGROUND|DISCL_NONEXCLUSIVE)))
			Error("IE", "Cannot set cooperative level for mouse");

		if( FAILED( g_Mouse->Acquire()))
			Error("IE", "Cannot acquire mouse");

		if( FAILED( g_Input->CreateDevice( GUID_SysKeyboard, &g_Keyboard, NULL ) ) )
			Error("IE", "Cannot create keyboard device");
	    
		if( FAILED( g_Keyboard->SetDataFormat( &c_dfDIKeyboard ) ) )
			Error("IE", "Cannot set data format for keyboard");
	    
		if( FAILED( g_Keyboard->SetCooperativeLevel( hWnd, DISCL_BACKGROUND|DISCL_NONEXCLUSIVE ) ) )
			Error("IE", "Cannot set cooperative level for keyboard");

		if( FAILED( g_Keyboard->Acquire() ) )
			Error("IE", "Cannot acquire keyboard");
	}

	InputEngineDInput8::~InputEngineDInput8()
	{
		if (g_Keyboard) g_Keyboard->Release();
		if (g_Mouse) g_Mouse->Release();
		if (g_Input) g_Input->Release();
	}

	void InputEngineDInput8::clear_states()
	{
		memset( &diks, 0, sizeof( diks ) );
		memset( &diks2, 0, sizeof( diks2 ) );
	}

	void InputEngineDInput8::read_states()
	{
		POINT pnt;

		GetCursorPos(&pnt);
		ScreenToClient(h_wnd,&pnt);
		mx = pnt.x;
		my = pnt.y;

		dms2 = dms;
		if( FAILED(g_Mouse->GetDeviceState(sizeof(DIMOUSESTATE), &dms)) )
			Error("IE", "Cannot get mouse device state");

		for (int a = 0; a < 256; a ++)
			diks2[a] = diks[a];
		if( FAILED(g_Keyboard->GetDeviceState(sizeof(diks), &diks )) )
			Error("IE", "Cannot get keyboard device state");
	}

	bool InputEngineDInput8::pressed(int key, bool before, bool now)
	{
		if (key<0 || key>=256) return false;
		return (diks2[key]!=0)==before && (diks[key]!=0)==now;
	}

	bool InputEngineDInput8::pressed_before(int key)
	{
		if (key<0 || key>=256) return false;
		return (diks2[key]!=0);
	}

	bool InputEngineDInput8::pressed_now(int key)
	{
		if (key<0 || key>=256) return false;
		return (diks[key]!=0);
	}

	int InputEngineDInput8::get_mouse_x()
	{
		return mx;
	}

	int InputEngineDInput8::get_mouse_y()
	{
		return my;
	}

	bool InputEngineDInput8::mouse_pressed(int button, bool before, bool now)
	{
		if (button<0 || button>=4) return false;
		return (dms2.rgbButtons[button]!=0)==before && (dms.rgbButtons[button]!=0)==now;
	}

	bool InputEngineDInput8::mouse_pressed_before(int button)
	{
		if (button<0 || button>=4) return false;
		return (dms2.rgbButtons[button]!=0);
	}

	bool InputEngineDInput8::mouse_pressed_now(int button)
	{
		if (button<0 || button>=4) return false;
		return (dms.rgbButtons[button]!=0);
	}
}