/*
PolyGun

Copyright (c) 2023 kacperks https://kacperks.cubesoftware.xyz
Copyright (c) 2023 Looki2000 https://looki2000.cubesoftware.xyz
Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "game/engine/player_camera.hpp"

#include <glm/gtc/matrix_transform.hpp>

#include "game/engine/engine.hpp"
#include "game/window/event.hpp"

using namespace polygun::engine;

float pitch_cos;

PlayerCamera::PlayerCamera(glm::vec3 position, glm::vec3 up, float yaw, float pitch) :
	m_position(position),
	m_front(glm::vec3(0.0f, 0.0f, -1.0f)),
	m_movement_front(),
	m_up(),
	m_right(),
	m_worldup(up),
	m_yaw(yaw),
	m_pitch(pitch),
	m_movement_speed(SPEED),
	m_mouse_sensitivity(SENSITIVITY),
	m_fov(FOV),
	m_x(Engine::get_screen_width() / 2.0f),
	m_y(Engine::get_screen_height() / 2.0f),
	m_last_x(Engine::get_screen_width() / 2.0f),
	m_last_y(Engine::get_screen_height() / 2.0f),
	m_first_mouse(true)
{
	update_camera_vectors();
}

PlayerCamera::PlayerCamera(float pos_x, float pos_y, float pos_z, float up_x, float up_y, float up_z, float yaw, float pitch) :
	m_position(pos_x, pos_y, pos_z),
	m_front(glm::vec3(0.0f, 0.0f, -1.0f)),
	m_movement_front(),
	m_up(),
	m_right(),
	m_worldup(up_x, up_y, up_z),
	m_yaw(yaw),
	m_pitch(pitch),
	m_movement_speed(SPEED),
	m_mouse_sensitivity(SENSITIVITY),
	m_fov(FOV),
	m_x(Engine::get_screen_width() / 2.0f),
	m_y(Engine::get_screen_height() / 2.0f),
	m_last_x(Engine::get_screen_width() / 2.0f),
	m_last_y(Engine::get_screen_height() / 2.0f),
	m_first_mouse(true)
{
	update_camera_vectors();
}

void PlayerCamera::update(bool reset) {
	if (m_first_mouse || reset) {
		m_last_x = m_x;
		m_last_y = m_y;
		m_first_mouse = false;
	}

	float x_offset = m_x - m_last_x;
	float y_offset = m_last_y - m_y;

	m_last_x = m_x;
	m_last_y = m_y;

	// only if mouse moved
	if (x_offset != 0.0f || y_offset != 0.0f)
		process_mouse_movement(x_offset, y_offset);

}

void PlayerCamera::event(const window::Event& ev) {
	switch(ev.m_type) {
	case window::EventType::EVENT_TYPE_MOUSE_MOVE:
		m_x = static_cast<float>(ev.m_data.m_mouse_event.m_x);
		m_y = static_cast<float>(ev.m_data.m_mouse_event.m_y);
		break;
	default:
		break;
	}
}

void PlayerCamera::process_movement(camera_movement direction, float delta_time) {
	float velocity = m_movement_speed * delta_time;
	if (direction == FORWARD)
		m_position += glm::vec3(m_movement_front.x, 0.0f, m_movement_front.y) * velocity;
	if (direction == BACKWARD)
		m_position -= glm::vec3(m_movement_front.x, 0.0f, m_movement_front.y) * velocity;
	if (direction == LEFT)
		m_position -= glm::vec3(m_right.x, 0.0f, m_right.z) * velocity;
	if (direction == RIGHT)
		m_position += glm::vec3(m_right.x, 0.0f, m_right.z) * velocity;
	if (direction == UP)
		m_position += glm::vec3(0.0f, 1.0f, 0.0f) * velocity;
	if (direction == DOWN)
		m_position += glm::vec3(0.0f, -1.0f, 0.0f) * velocity;

	m_view = glm::lookAt(m_position, m_position + m_front, m_up);
}

void PlayerCamera::process_mouse_movement(float x_offset, float y_offset, bool constrain_pitch) {
	x_offset *= m_mouse_sensitivity;
	y_offset *= m_mouse_sensitivity;

	m_yaw   += x_offset;
	m_pitch += y_offset;

	if (constrain_pitch) {
		if (m_pitch > 89.9f)
			m_pitch = 89.9f;
		else if (m_pitch < -89.9f)
			m_pitch = -89.9f;
	}

	update_camera_vectors();
}

void PlayerCamera::process_mouse_scroll(float y_offset) {
	m_fov -= y_offset;
	if (m_fov < 1.0f)
		m_fov = 1.0f;
	if (m_fov > 45.0f)
		m_fov = 45.0f;
}

void PlayerCamera::update_camera_vectors() {
	glm::vec3 front;
	pitch_cos = cos(glm::radians(m_pitch));

	m_movement_front.x = cos(glm::radians(m_yaw));
	m_movement_front.y = sin(glm::radians(m_yaw));

	front.x = m_movement_front.x * pitch_cos;
	front.y = sin(glm::radians(m_pitch));
	front.z = m_movement_front.y * pitch_cos;

	m_front = glm::normalize(front);
	m_right = glm::normalize(glm::cross(m_front, m_worldup));
	m_up    = glm::normalize(glm::cross(m_right, m_front));

	m_view = glm::lookAt(m_position, m_position + m_front, m_up);
	m_projection = glm::perspective(glm::radians(m_fov), (float)engine::Engine::get_screen_width() / (float)engine::Engine::get_screen_height(), 0.01f, 100.0f);
}
