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

#include <algorithm>
#include <iostream>
#include <ostream>

#include "Collision.h"
#include "colors.h"
#include "utils.h"


WorldLevel::WorldLevel(Camera* camera, Rectf viewport):
	Level(camera),
	m_player(Player { Point2f { 0, 100 } }),
	m_mousePos { 0, 0 },
	m_viewport(viewport)
{
	// The grid is 34 x 50 big, the top center is 0,0 in world coords
	for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
		for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
			int actualX = x - WORLD_WIDTH / 2;
			m_worldTiles[x][y] = new WorldTile{ Point2f{ float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT}, GroundTileTypes::Dirt, TextureManager::GetInstance()};
		}
	}

}
WorldLevel::~WorldLevel() {
	//delete m_pTextTexture;
	
}
void WorldLevel::Update(float elapsedSec) {
	int mouseX, mouseY;
	SDL_GetMouseState(&mouseX, &mouseY);
	m_mousePos = Point2f{ float(mouseX), float(mouseY) };
	m_mousePos = m_pCamera->TransformMouse(m_mousePos);
	//m_player.Update(elapsedSec, *this);


	WorldTile* selectedTile{ nullptr };
	
	for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
		for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
			if(m_worldTiles[x][y]->GetCollisionRect().Contains(m_mousePos)) {
				selectedTile = m_worldTiles[x][y];
			}
		}
	}
	if(selectedTile != nullptr) {
		if(utils::isMouseDown(SDL_BUTTON_LEFT)) {
			selectedTile->SetTileType(GroundTileTypes::Air);
		}
	}

	m_player.Update(elapsedSec, *this);
}
void WorldLevel::Draw() const {
	m_pCamera->BeginRendering();

	for (Collision::CollisionRect rect : m_Rects) {
		utils::DrawRect(rect.pos, rect.size.x, rect.size.y);
	}

	Point2f RayPoint = Point2f{m_pCamera->Viewport.width, m_pCamera->Viewport.height};
	Point2f RayDir = Point2f{m_mousePos.x - RayPoint.x, m_mousePos.y - RayPoint.y};

	utils::SetColor(Colors::WHITE);
	utils::DrawLine(RayPoint, m_mousePos);

	utils::FillEllipse(m_mousePos, 20, 20);

	for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
		for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
			m_worldTiles[x][y]->Draw();
		}
	}

	//loop over worldtiles
	for (int x { 0 }; x < WORLD_WIDTH; ++x) {
		for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
			if(m_worldTiles[x][y]->GetTileType() == GroundTileTypes::Dirt) {
				Collision::CollisionRect rect = m_worldTiles[x][y]->GetCollisionRect().getCollisionRect();
				utils::SetColor(Colors::GREEN);
				utils::DrawRect(rect.pos, rect.size.x, rect.size.y);
			}
		}
	}

	utils::SetColor(Colors::MAGENTA);
	utils::FillEllipse(0, 0, 5, 5);

	m_player.Draw();
	m_pCamera->EndRendering();
}
void WorldLevel::MouseMove(const Point2f& mousePos) {
	m_mousePos = mousePos;
}

void WorldLevel::ProcessImGui() {
	if (ImGui::BeginMainMenuBar()) {
		if (ImGui::BeginMenu("Properties")) {
			if (ImGui::MenuItem("TextureManager Info")) {
				m_ShowTextureManagerWindow = !m_ShowTextureManagerWindow;
			}
			if (ImGui::MenuItem("Camera Info")) {
				m_ShowCameraWindow = !m_ShowCameraWindow;
			}
			if(ImGui::MenuItem("Player Info")) {
				m_ShowPlayerInfo = !m_ShowPlayerInfo;
			}
			ImGui::EndMenu();
		}
		ImGui::EndMainMenuBar();
	}

	if(m_ShowTextureManagerWindow) {
		ImGui::Begin("Texture Manager", &m_ShowTextureManagerWindow, ImGuiWindowFlags_AlwaysAutoResize);
		ImGui::Text("Texture loading Count:");
		ImGui::SameLine();
		ImGui::TextColored(ImVec4(1.0f, 0.0f, 1.0f, 1.0f), std::to_string(Texture::m_TextureCounter).c_str());
		ImGui::End();
	}

	if(m_ShowCameraWindow) {
		ImGui::Begin("Camera", &m_ShowCameraWindow, ImGuiWindowFlags_AlwaysAutoResize);
		ImGui::Text("Camera Position: (%f, %f)", m_pCamera->GetPosition().x,  m_pCamera->GetPosition().y);
		if(ImGui::Button("Reset Camera")) {
			m_pCamera->SetPosition(Point2f{m_viewport.width / 2, m_viewport.height / 2});
		}
		ImGui::End();
	}

	if(m_ShowPlayerInfo) {
		ImGui::Begin("Player Info", &m_ShowPlayerInfo, ImGuiWindowFlags_AlwaysAutoResize);
		ImGui::Text("Player Position: (%f, %f)", m_player.GetPosition().x, m_player.GetPosition().y);
		ImGui::Text("Player Velocity: (%f, %f)", m_player.GetVelocity().x, m_player.GetVelocity().y);
		//PLAYER COLLISIONS
		ImGui::Text("Player Collisions:");
		// for (std::pair<Collision::CollisionDirection, Collision::CollisionRect*> contact : m_player.GetContactMap()) {
		// 	ImGui::Text("Direction: %d", contact.first);
		// 	ImGui::Text("Position: (%f, %f)", contact.second->pos.x, contact.second->pos.y);
		// 	ImGui::Text("Size: (%f, %f)", contact.second->size.x, contact.second->size.y);
		// }
		ImGui::End();
	}
}
WorldTile* WorldLevel::GetTileAt(const Point2f& pos) const {
	return m_worldTiles[pos.x][pos.y];
}
void WorldLevel::SetTileAt(const Point2f& pos, WorldTile* tile) {
}
std::array<std::array<WorldTile*, WorldLevel::WORLD_WIDTH>, WorldLevel::WORLD_HEIGHT> WorldLevel::GetAllTiles() const {
	return m_worldTiles;
}



