#include "LevelOverOnHitByCharacter.hpp"
#include "GameObject.hpp"
#include "PhysicsBody.hpp"
#include "Event.hpp"
#include "SceneManager.h"
#include "Logger.hpp"

class CharacterController;
LevelOverOnHitByCharacter::LevelOverOnHitByCharacter(GameObject* gameObject) : Behaviour(gameObject){
	gameObject->OnEnable += Delegate<void()>::Create<LevelOverOnHitByCharacter, &LevelOverOnHitByCharacter::registerEvents>(this);
	gameObject->OnDisable += Delegate<void()>::Create<LevelOverOnHitByCharacter, &LevelOverOnHitByCharacter::deregisterEvents>(this);
}

void LevelOverOnHitByCharacter::InitialiseSelf() {
	physicsBody = gameObject.GetComponents<PhysicsBody>().front();
	physicsBody->OnCollisionEnter += Delegate<void(GameObject&)>::Create<LevelOverOnHitByCharacter, &LevelOverOnHitByCharacter::OnCollisionEnter>(this);
}

void LevelOverOnHitByCharacter::OnCollisionEnter(GameObject& other) {
	if (other.GetName() == "Player Character" || other.GetComponents<CharacterController>().size() > 0) {
		Logger::Info("Character hit the level over block");
		SceneManager::GetInstance()->CurrentLevelOver();
	}
}

void LevelOverOnHitByCharacter::registerEvents() {
	//physicsBody->OnCollisionEnter += Delegate<void(GameObject&)>::Create<LevelOverOnHitByCharacter, &LevelOverOnHitByCharacter::OnCollisionEnter>(this);
}
void LevelOverOnHitByCharacter::deregisterEvents() {
	physicsBody->OnCollisionEnter -= Delegate<void(GameObject&)>::Create<LevelOverOnHitByCharacter, &LevelOverOnHitByCharacter::OnCollisionEnter>(this);
}
