#include "CMyEventReceiver.h"

// We'll create a struct to record info on the mouse state
//struct SMouseState MouseState;


CMyEventReceiver::CMyEventReceiver(SAppContext &a) : App(a)
{
	for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
                KeyIsDown[i] = false;

            LEFTBUTTONCLICKED = RIGHTBUTTONCLICKED = false;
}

bool CMyEventReceiver::IsKeyDown(EKEY_CODE keyCode) const { return KeyIsDown[keyCode]; }
bool CMyEventReceiver::IsLMBDown() const { return LEFTBUTTONCLICKED; }
bool CMyEventReceiver::IsRMBDown() const { return RIGHTBUTTONCLICKED; }

bool CMyEventReceiver::OnEvent(const SEvent& event)
{
    // Remember the mouse state
    if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
    {
        printf("event.EventType ==EET_MOUSE_INPUT_EVENT\n");
        switch(event.MouseInput.Event)
        {
        case EMIE_LMOUSE_PRESSED_DOWN:
            MouseState.LeftButtonDown = true;
            break;

        case EMIE_LMOUSE_LEFT_UP:
            MouseState.LeftButtonDown = false;
            break;

        case EMIE_MOUSE_MOVED:
            MouseState.Position.X = event.MouseInput.X;
            MouseState.Position.Y = event.MouseInput.Y;
            break;

        default:
            // We won't use the wheel
            break;
        }
    }

    if (event.EventType == EET_GUI_EVENT)
	{
		s32 id = event.GUIEvent.Caller->getID();
        printf("onEvent\n");
		switch(id)
		{

		case SAppContext::GUI_ID_CONSTRUCTION_BUTTON: //Btn Construction
			if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED )
			{
				//printf("New button clicked \n");

                App.windowConstruction = App.Device->getGUIEnvironment()->addWindow(core::rect<s32>(5,70,400,250),true,L"Choisir une construction",0,SAppContext::GUI_ID_WINDOW_CONSTRUCTION);
                App.newRMButton =  App.Device->getGUIEnvironment()->addButton(core::rect<s32>(5, 5, 390, 35),App.windowConstruction,SAppContext::GUI_ID_BUTTON_NOUVEAU_RM,L"Nouvelle remontee");
                App.newPisteButton =  App.Device->getGUIEnvironment()->addButton(core::rect<s32>(5, 40, 390, 70),App.windowConstruction,SAppContext::GUI_ID_BUTTON_NOUVELLE_PISTE,L"Nouvelle piste");
                App.newArbreButton =  App.Device->getGUIEnvironment()->addButton(core::rect<s32>(5, 70, 390, 105),App.windowConstruction,SAppContext::GUI_ID_BUTTON_ARBRES,L"Ajouter un arbre");

				//MenuDevice->closeDevice();
				//start = true;
			}
			break;

        case SAppContext::GUI_ID_BUTTON_NOUVEAU_RM: //Btn Construction
            printf("Nouvelle RM\n");
            break;
        }
	}

    return false;
}

SMouseState & CMyEventReceiver::GetMouseState()
{
    //printf("getMouseState\n");
    return MouseState;
}

CMyEventReceiver::~CMyEventReceiver()
{


}
