#include <assert.h>
#include <utils/logger.h>
#include <utils/timer.h>

#include "input/input_system.h"

#include "render/render_shared.h"
#include "render/modelmanager.h"
#include "render/debugrender.h"

#include "game/player.h"
#include "game/camera.h"
#include "game/level.h"

IMPLEMENT_RTTI(Player, GameObject);

// Object factory
GameObject* Player::_Construct()
{
	return new Player();
}

Player::Player()
{
	Msg(" --- Creating Player ---");

	//m_weapon = (Ship_Weapon*)g_level->CreateEntityByRC( RUNTIME_CLASS( Ship_Weapon ) );
	//m_weapon->LoadModel("content/test/ship_weapon.obj");
}

Player::~Player()
{
}

void Player::Update(float dt)
{
	// Update camera

	int32_t x = 0, y = 0;
	g_inputSystem->GetMousePosition(&x, &y);

	cameraUpdateMouseLook((int)x, (int)y);

	const float kCameraOffset = 1.2f;
	const float kBackSpeed = 1.2f;
	float kPlayerSpeed = 2.0f;
	if (g_inputSystem->IsKeyPressed(KEY_LEFT_SHIFT))
		kPlayerSpeed = 9.0f;

	// Update player itself

	Vec3 dir = normalize(cameraGetDir());
//	dir.y = 0.0f;

	if (g_inputSystem->IsKeyPressed(KEY_W))
		m_position += kPlayerSpeed * dt * dir;
	if (g_inputSystem->IsKeyPressed(KEY_S))
		m_position -= kPlayerSpeed * dt * dir;
	if (g_inputSystem->IsKeyPressed(KEY_A))
		m_position -= kPlayerSpeed * dt * cross(normalize(dir), Vec3(0.0f, 1.0f, 0.0f));
	if (g_inputSystem->IsKeyPressed(KEY_D))
		m_position += kPlayerSpeed * dt * cross(normalize(dir), Vec3(0.0f, 1.0f, 0.0f));

	if (g_inputSystem->IsKeyPressed(KEY_P))
		Msg("\nPlayer Position:\nVec3(%.2f, %.2f, %.2f)\nPosition %.2f %.2f %.2f\n", m_position.x, m_position.y, m_position.z,
			m_position.x, m_position.y, m_position.z);

	Vec3 cameraPos = m_position;
	cameraPos.y += 0.4f;

	cameraUpdateLook_FPS(cameraPos);
	cameraPushToRender();

	g_debugRender->DrawEllipse(this->GetModelMatrix(), Vec3(0.4f, 0.4f, 1.0f));
}
