#pragma once

#include "dllinterface.h"

#define DIRECTINPUT_VERSION 0x0800

#include <dinput.h>
#include <d3d9.h>
#include <d3dx9.h>

#include "..\utilities\XString.h"
#include "DXVector2.h"

class D3DApp;
class DXTexture;

#define BUTTONRELEASED	0x0000
#define BUTTONPRESSED	0x0001 
#define BUTTONDOWN		0x0002
#define BUTTONUP		0x0004	

enum MOUSEBUTTON 
{
	MBTN_LEFT=0,
	MBTN_RIGHT,
	MBTN_MID,
	MBTN_4,
	MBTN_5,
	MBTN_6,
	MBTN_7,
	MBTN_COUNT
};

#define WHEELUP		0x0200
#define WHEELDOWN   0x0400
#define KEYBOARDBUFFER_SIZE 16
#define KEY_DOWN		0x01
#define KEY_PRESS	0x02
#define KEY_UP		0x04

class DXFUNCTIONS_API CDIFunctions
{
private:
	DXVector2 		m_CursorScreen;
	DXVector2		m_CursorWindow;
	short			xbmp,ybmp;
	short			dx,dy;
	unsigned char	m_MouseButtons[MBTN_COUNT];
	float			m_MouseSpeed;
	LPDIRECTINPUTDEVICE8	m_MouseDevice;
	LPDIRECTINPUTDEVICE8	m_KeyboardDevice;
	bool					m_MouseEnable;
	DWORD					m_DoubleClickTime;
	LPDIRECTINPUT8			m_DI; 
	int						m_KeyboardSpeed;
	int						m_KeyboardDelay;
	HWND					m_hWnd;
	HCURSOR					m_hCursor;

	unsigned char xl,yl,waitinput,art;
	bool		m_bMouseVisibility;
	bool		m_bMouseOver;
	int			m_MaxX,m_MaxY;
	int			wheel;

	int			m_DevCurHotX, m_DevCurHotY;
//	CImage		*cursors;

	IDirect3DSurface9* m_Surface;

	DXTexture* m_pDeviceCursor;

	D3DApp* m_pApp;

	unsigned char m_Keys[256];

public:

	CDIFunctions(D3DApp* const pApp);
	~CDIFunctions(void);
	void SetMouseVisible(bool vis);
	//void setcursor(char cur);
//	HRESULT draw(LPDIRECTDRAWSURFACE7 sur);
	bool Update(void);
	HRESULT InitMouse(int hwnd,int hinst);
	void Release(void);
	void SetAcquire(bool act);
	float GetScreenCursorX(void) const { return m_CursorScreen.x; }
	float GetScreenCursorY(void) const { return m_CursorScreen.y; }
	float GetCursorX(void) const { return m_CursorWindow.x; }
	float GetCursorY(void) const { return m_CursorWindow.y; }
	short GetMouseDZ(void) const { return (short)wheel; }
	short GetMouseDX(void) const { return dx; }
	short GetMouseDY(void) const { return dy; }
	void SetMousePos(int x,int y);
	void SetMaxMousePos(int mx,int my);
	unsigned char GetMouseButton(MOUSEBUTTON button){return m_MouseButtons[button];}
	bool GetMouseEnable(void){return m_MouseEnable;}
	void SetMouseEnable(bool enable){m_MouseEnable=enable;}
	float GetMouseSpeed(void){return m_MouseSpeed;}
	void SetMouseSpeed(float speed){m_MouseSpeed = speed;}
	DWORD GetMouseDoubleClickTime(void){return m_DoubleClickTime;}
	void SetMouseDoubleClickTime(DWORD time){m_DoubleClickTime = time;}
	//void x(int val){xpos=val;}
	//void y(int val){ypos=val;}
	HRESULT InitKeyboard(int hwnd,int hinst);
	//void DrawCursor(void);
	unsigned char GetKey(unsigned char key){return m_Keys[key];}
	int GetKeyboardDelay(void){return m_KeyboardDelay;}
	void SetKeyboardDelay(int kd){m_KeyboardDelay = kd;}
	int GetKeyboardSpeed(void){return m_KeyboardSpeed;}
	void SetKeyboardSpeed(int ks){m_KeyboardSpeed = ks;}
	XString GetKeyName(unsigned char key);
	XString KeyToXString(unsigned char key);
	unsigned GetVK(unsigned char key);

	void RestoreDeviceCursor(void);

	void SetCursor(HCURSOR hCursor) { m_hCursor = hCursor; }
	HCURSOR GetCursor(void) const { return m_hCursor; }

	void SetDeviceCursor(DXTexture* const cursor, const int hotx, const int hoty);
	DXTexture* GetDeviceCursor(void) const { return m_pDeviceCursor; }

	bool IsMouseOver(void) const { return m_bMouseOver; }
	bool IsMouseVisible(void) const { return m_bMouseVisibility; }

	virtual void OnKeyPress(const unsigned key) {}
	virtual void OnKeyUp(const unsigned key) {}
	virtual void OnKeyDown(const unsigned key) {}
};

