#include <imgui.h>
#include "pch.h"
#include "Game.h"

#include <iostream>
#include <ostream>

#include "colors.h"
#include "utils.h"
#include "WorldLevel.h"

Game::Game(const Window& window)
	: BaseGame { window }, m_Camera(Camera()), m_WorldLevel(WorldLevel(&m_Camera, GetViewPort()))
{
	Initialize();
}

Game::~Game() {
	Cleanup();
}

void Game::Initialize() {
	m_Camera.SetPosition(Point2f{GetViewPort().width / 2, GetViewPort().height / 2});
}

void Game::Cleanup() {
}

void Game::Update(float elapsedSec) {
	const Uint8* pStates = SDL_GetKeyboardState(nullptr);
	// if (pStates[SDL_SCANCODE_RIGHT]) {
	// 	m_CameraOffset.x += 200 * elapsedSec;
	// }
	// if (pStates[SDL_SCANCODE_LEFT]) {
	// 	m_CameraOffset.x -= 200 * elapsedSec;
	// }
	// if (pStates[SDL_SCANCODE_UP]) {
	// 	m_CameraOffset.y += 200 * elapsedSec;
	// }
	// if (pStates[SDL_SCANCODE_DOWN]) {
	// 	m_CameraOffset.y -= 200 * elapsedSec;
	// }

	if(m_IsRightMouseDown) {
		Point2f newCameraPos = m_MousePos + m_MouseOffset;
		m_Camera.SetPosition(newCameraPos);
	} else {
		m_MouseOffset = m_Camera.GetPosition();
	}

	m_WorldLevel.Update(elapsedSec);


}

void Game::Draw() const {
	utils::ClearBackground(Color4f(0.0f, 0.0f, 0.3f, 1.0f));
	m_WorldLevel.Draw();
}

void Game::ProcessKeyDownEvent(const SDL_KeyboardEvent& e) {
	//std::cout << "KEYDOWN event: " << e.keysym.sym << std::endl;
}

void Game::ProcessKeyUpEvent(const SDL_KeyboardEvent& e) {
}

void Game::ProcessMouseMotionEvent(const SDL_MouseMotionEvent& e) {
	m_MousePos = Point2f { float(e.x), float(e.y) };
	m_WorldLevel.MouseMove(m_MousePos);
}

void Game::ProcessMouseDownEvent(const SDL_MouseButtonEvent& e) {
	m_IsRightMouseDown = e.button == SDL_BUTTON_RIGHT;
	m_MouseOffset = Point2f{m_Camera.GetPosition().x - m_MousePos.x, m_Camera.GetPosition().y - m_MousePos.y};
}

void Game::ProcessMouseUpEvent(const SDL_MouseButtonEvent& e) {
	m_IsRightMouseDown = e.button == SDL_BUTTON_RIGHT ? false : m_IsRightMouseDown;
	//std::cout << "MOUSEBUTTONUP event: ";
	//switch ( e.button )
	//{
	//case SDL_BUTTON_LEFT:
	//	std::cout << " left button " << std::endl;
	//	break;
	//case SDL_BUTTON_RIGHT:
	//	std::cout << " right button " << std::endl;
	//	break;
	//case SDL_BUTTON_MIDDLE:
	//	std::cout << " middle button " << std::endl;
	//	break;
	//}
}
void Game::ProcessImGui() {

	

	m_WorldLevel.ProcessImGui();
}
