#include "Salamander.h"
#include "SDLPlatformManager.h"

#include "ButtonDownEvent.h"
#include "ButtonUpEvent.h"
#include "MotionEvent.h"
#include "PlatformEvent.h"
#include "SDLInclude.h"
#include "SDLKeyMapping.h"
#include "SDLMouseMapping.h"

namespace Salamander
{

defineUniqueId(SDLPlatformManager);
SDLPlatformManager::SDLPlatformManager()
{
}

SDLPlatformManager::~SDLPlatformManager()
{
}

bool SDLPlatformManager::startup()
{
	return true;
}

bool SDLPlatformManager::shutdown()
{
	return true;
}

void SDLPlatformManager::frame()
{
	SDL_Event event;

    while(SDL_PollEvent(&event))
    {
        // check for messages
        switch (event.type)
        {
            // exit if the window is closed
            case SDL_QUIT:
				events().send(PlatformEvent(PlatformEvent::QUIT));
                break;

            // check for keypresses
			case SDL_KEYDOWN:
				events().send(ButtonDownEvent(SDLKeyMapping(event.key.keysym.sym)));
				break;

			case SDL_KEYUP:
				events().send(ButtonUpEvent(SDLKeyMapping(event.key.keysym.sym)));
                break;

			case SDL_MOUSEMOTION:
				events().send(MotionEvent(Vec2(event.motion.x, event.motion.y)));
				break;

			case SDL_MOUSEBUTTONDOWN:
				events().send(MotionEvent(Vec2(event.motion.x, event.motion.y)));
				events().send(ButtonDownEvent(SDLMouseMapping(event.button.button)));
				break;
			
			case SDL_MOUSEBUTTONUP:
				events().send(MotionEvent(Vec2(event.motion.x, event.motion.y)));
				events().send(ButtonUpEvent(SDLMouseMapping(event.button.button)));
				break;
        }
    }
}

}
