#include "legorace.h"

#include "isle.h"
#include "legocontrolmanager.h"
#include "legonavcontroller.h"
#include "misc.h"
#include "mxmisc.h"
#include "mxnotificationmanager.h"

DECOMP_SIZE_ASSERT(LegoRace, 0x144)
DECOMP_SIZE_ASSERT(RaceState::Entry, 0x06)
DECOMP_SIZE_ASSERT(RaceState, 0x2c)

// Defined in legopathstruct.cpp
extern MxBool g_triggerHandlingIgnoreDirection;

// FUNCTION: LEGO1 0x10015aa0
LegoRace::LegoRace()
{
	m_playerLaps = 0;
	m_opponent1Laps = 0;
	m_opponent2Laps = 0;
	m_playerLastPathStruct = 0;
	m_opponent1LastPathStruct = 0;
	m_opponent2LastPathStruct = 0;
	m_raceState = NULL;
	m_mapsLocators[0] = NULL;
	m_mapsLocators[1] = NULL;
	m_mapsLocators[2] = NULL;
	m_opponent1Locator = 0;
	m_opponent2Locator = 0;
	m_pathActor = 0;
	m_act1State = NULL;
	m_destLocation = LegoGameState::e_undefined;
	NotificationManager()->Register(this);
}

// FUNCTION: LEGO1 0x10015ce0
// FUNCTION: BETA10 0x100c7a71
MxResult LegoRace::Create(MxDSAction& p_dsAction)
{
	MxResult result = LegoWorld::Create(p_dsAction);

	if (result == SUCCESS) {
		m_act1State = (Act1State*) GameState()->GetState("Act1State");
		ControlManager()->Register(this);
		m_pathActor = UserActor();
		m_pathActor->SetWorldSpeed(0);
		SetUserActor(NULL);
	}

	return result;
}

// FUNCTION: LEGO1 0x10015d40
// FUNCTION: BETA10 0x100c7ab5
LegoRace::~LegoRace()
{
	g_triggerHandlingIgnoreDirection = FALSE;
	if (m_pathActor) {
		SetUserActor(m_pathActor);
		NavController()->ResetMaxLinearVel(m_pathActor->GetMaxLinearVel());
		m_pathActor = NULL;
	}

	ControlManager()->Unregister(this);
	NotificationManager()->Unregister(this);
}

// FUNCTION: LEGO1 0x10015e00
// FUNCTION: BETA10 0x100c7b3d
MxLong LegoRace::Notify(MxParam& p_param)
{
	LegoWorld::Notify(p_param);
	MxNotificationParam& param = (MxNotificationParam&) p_param;

	MxLong result = 0;
	if (m_worldStarted) {
		switch (param.GetNotification()) {
		case c_notificationType0:
			HandleType0Notification((MxNotificationParam&) p_param);
			break;
		case c_notificationEndAction:
			result = HandleEndAction((MxEndActionNotificationParam&) p_param);
			break;
		case c_notificationControl:
			result = HandleControl((LegoControlManagerNotificationParam&) p_param);
			break;
		case c_notificationPathStruct:
			result = HandlePathStruct((LegoPathStructNotificationParam&) p_param);
			break;
		case c_notificationTransitioned:
			GameState()->SwitchArea(m_destLocation);
			break;
		}
	}

	return result;
}

// FUNCTION: LEGO1 0x10015ed0
// FUNCTION: BETA10 0x100c7c3f
void LegoRace::Enable(MxBool p_enable)
{
	if (NoDisabledObjects() != p_enable && !p_enable) {
		Remove(UserActor());

		MxU8 oldActorId = GameState()->GetActorId();
		GameState()->RemoveActor();
		GameState()->SetActorId(oldActorId);
	}

	LegoWorld::Enable(p_enable);
}

// FUNCTION: LEGO1 0x10015f30
RaceState::RaceState()
{
	m_entries[0].m_id = LegoActor::c_pepper;
	m_entries[0].m_lastScore = 0;
	m_entries[0].m_score = 0;
	m_entries[1].m_id = LegoActor::c_mama;
	m_entries[1].m_lastScore = 0;
	m_entries[1].m_score = 0;
	m_entries[2].m_id = LegoActor::c_papa;
	m_entries[2].m_lastScore = 0;
	m_entries[2].m_score = 0;
	m_entries[3].m_id = LegoActor::c_nick;
	m_entries[3].m_lastScore = 0;
	m_entries[3].m_score = 0;
	m_entries[4].m_id = LegoActor::c_laura;
	m_entries[4].m_lastScore = 0;
	m_entries[4].m_score = 0;
	m_state = RaceState::e_carrace;
}

// FUNCTION: LEGO1 0x10016140
// FUNCTION: BETA10 0x100c7d9f
MxResult RaceState::Serialize(LegoStorage* p_storage)
{
	LegoState::Serialize(p_storage);

	for (MxS16 i = 0; i < 5; i++) {
		m_entries[i].Serialize(p_storage);
	}

	return SUCCESS;
}

// FUNCTION: LEGO1 0x10016280
// FUNCTION: BETA10 0x100c7dfd
RaceState::Entry* RaceState::GetState(MxU8 p_id)
{
	for (MxS16 i = 0;; i++) {
		if (i >= 5) {
			return NULL;
		}

		if (m_entries[i].m_id == p_id) {
			return m_entries + i;
		}
	}
}
