#include "Game.h"

#include "Controller.h"
#include "AsteroidsEvents.h"
#include "Model.h"

namespace asteroids {

int Controller::OnInit (int argc, char *argv[]) {
	Engine::ControllerBase::OnInit (argc, argv);

	mBindings[SDLK_UP] = "+forward";
	mBindings[SDLK_LEFT] = "+turnleft";
	mBindings[SDLK_RIGHT] = "+turnright";

	mBindings[SDLK_SPACE] = "attack";

	mBindings[SDLK_F8] = "toggleconsole";
	mBindings[SDLK_F9] = "set playerspeed 5.0";

	Engine::RegisterListener (this, EventChangeViewState);

	return 0;
}

bool Controller::OnReceiveEvent (const Engine::EventBasePtr &event) {
	if (event->mEventType == EventChangeViewState) {
		IMGUIClear();
	}
	return false;
}

void Controller::ResetPlayerEntity () {
	Engine::HaltSoundLoop(Engine::GetResourceFullPath("/data/sounds/thrust.wav"));

	// Reset the angular velocity of the player entity if one exists
	if (GetModel()->GetPlayerEntityId() != Engine::NullEntityId) {
		Engine::EntityBase *player_entity = GetModel()->GetEntity(GetModel()->GetPlayerEntityId());
		player_entity->mPhysicState->mAngleVelocity = 0.;

		player_entity->ResetControllerKeyState();
	}

	// We definitely have to reset the player entity id
	GetModel()->SetPlayerEntityId(Engine::NullEntityId);
}

void Controller::ResetLevel() {
	Engine::LogMessage ("Resetting level");

	GetModel()->ClearEntities();
	GetModel()->mAsteroids.clear();
	ResetPlayerEntity();
}

}
