#include "pch.h"
#include "WorldLevel.h"

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

#include "Collision.h"
#include "colors.h"
#include "GameManager.h"
#include "../../GridSystem/GroundTileTypeManager.h"
#include "utils.h"
#include "GridSystem/WorldTile.h"
#include "Gui/Screens/ScreenManager.h"


class GroundTileType;
WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
                                                        m_GridManager(WorldGridManager()),
                                                        m_Player(Vector2f { 0, 100 }, TextureManager::GetInstance()),
                                                        m_MousePos { 0, 0 },
                                                        m_Viewport(viewport),
                                                        m_ScreenManager(ScreenManager::GetInstance()) {
	InitializeGroundTiles();
	// The grid is 34 x 50 big, the top center is 0,0 in world coords
	for (int x { 0 }; x < WORLD_WIDTH; ++x) {
		for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
			const int actualX = x - WORLD_WIDTH / 2;
			Vector2f pos = Vector2f { float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT };
			GroundTileType* type = getRandomGroundTile();
			m_GridManager.SetTileAtIndex(x, y, new WorldTile { pos, type, TextureManager::GetInstance(), &m_GridManager });
		}
	}
	for (int x { 0 }; x < WORLD_WIDTH; ++x) {
		m_GridManager.GetTileAtIndex(x, 0)->SetTileType(GroundTileTypeManager::GetInstance()->AIR);
		m_GridManager.GetTileAtIndex(x, 1)->SetTileType(GroundTileTypeManager::GetInstance()->GRASS);
	}

	//Add to walls to the side of stone
	for (int y { 0 }; y < WORLD_HEIGHT - 1; ++y) {
		m_GridManager.GetTileAtIndex(0, y)->SetTileType(GroundTileTypeManager::GetInstance()->STONE);
		m_GridManager.GetTileAtIndex(WORLD_WIDTH - 1, y)->SetTileType(GroundTileTypeManager::GetInstance()->STONE);
	}

	Building* fuelBuilding = new Building { "buildings/fuelStation.png", Vector2f { -700, -52 }, Rectf { 0, 0, 50, 50 }, TextureManager::GetInstance() };
	fuelBuilding->SetOnEnterHitbox([]()
	{
		ScreenManager::GetInstance()->OpenScreen(ScreenManager::m_FuelScreen);
	});
	m_Buildings.push_back(fuelBuilding);
	m_GridManager.GetTileAtWorldPos(Vector2f { -750, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_LEFT);
	m_GridManager.GetTileAtWorldPos(Vector2f { -700, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { -650, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { -600, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_RIGHT);

	Building* mineralBuilding = new Building { "buildings/mineralStation.png", Vector2f { -350, -52 }, Rectf { 0, 0, 100, 100 }, TextureManager::GetInstance() };
	mineralBuilding->SetOnEnterHitbox([]()
	{
		ScreenManager::GetInstance()->OpenScreen(ScreenManager::m_SellScreen);
	});
	m_Buildings.emplace_back(mineralBuilding);
	m_GridManager.GetTileAtWorldPos(Vector2f { -400, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_LEFT);
	m_GridManager.GetTileAtWorldPos(Vector2f { -350, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { -300, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { -250, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { -200, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { -150, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_RIGHT);

	m_Buildings.emplace_back(new Building { "buildings/junkStation.png", Vector2f { 250, -52 }, Rectf { 0, 0, 100, 100 }, TextureManager::GetInstance() });
	m_GridManager.GetTileAtWorldPos(Vector2f { 200, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_LEFT);
	m_GridManager.GetTileAtWorldPos(Vector2f { 250, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { 300, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { 350, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { 400, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { 450, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_RIGHT);

	m_Buildings.emplace_back(new Building { "buildings/repairStation.png", Vector2f { 700, -52 }, Rectf { 0, 0, 100, 100 }, TextureManager::GetInstance() });
	m_GridManager.GetTileAtWorldPos(Vector2f { 650, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_LEFT);
	m_GridManager.GetTileAtWorldPos(Vector2f { 700, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { 750, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE);
	m_GridManager.GetTileAtWorldPos(Vector2f { 800, -50 })->SetTileType(GroundTileTypeManager::GetInstance()->HARD_RIGHT);


	m_TopCover = TextureManager::GetInstance()->GetTexture("topBackground.png");

	m_MainScreen = new MainScreen(TextureManager::GetInstance());
	GameManager::GetInstance().SetMainScreen(m_MainScreen);

	m_Sun = new OrbitingObject(Vector2f { 0, -1000 }, 1200, 0.1f, TextureManager::GetInstance()->GetTexture("sun.png"));
	m_Moon = new OrbitingObject(Vector2f { 0, -1000 }, 1200, 0.1f, TextureManager::GetInstance()->GetTexture("moon.png"), (float)M_PI);

	GameManager::GetInstance().SetFuel(100);
	GameManager::GetInstance().SetHullIntegrity(100);

	GameManager::GetInstance().SetPlayer(&m_Player);

	m_BackgroundMusic = new SoundStream("sound/bgm.wav");
	m_BackgroundMusic->Play(true);

	GameManager::GetInstance().SetMoney(100);

}
WorldLevel::~WorldLevel() {
	delete m_MainScreen;

	delete m_Sun;
	delete m_Moon;

	delete m_BackgroundMusic;

	for (Building* building : m_Buildings) {
		delete building;
	}

	GameManager::DestroyInstance();

}

void WorldLevel::Update(float elapsedSec) {
	m_Fps = 1 / elapsedSec;

	int mouseX, mouseY;
	SDL_GetMouseState(&mouseX, &mouseY);
	m_MousePos = Vector2f { float(mouseX), float(mouseY) };
	m_MousePos = m_pCamera->TransformMouse(m_MousePos);


	// for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
	// 	for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
	// 		m_gridManager.GetTileAtIndex(x, y)->m_Hightlight = false;
	// 	}
	// }

	m_Sun->Update(elapsedSec);
	m_Moon->Update(elapsedSec);

	for (int x { 0 }; x < WORLD_WIDTH; ++x) {
		for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
			m_GridManager.GetTileAtIndex(x, y)->Update(m_pCamera);
			if (m_GridManager.GetTileAtIndex(x, y)->GetCollisionRect().Contains(m_MousePos)) {
				m_pSelectedTile = m_GridManager.GetTileAtIndex(x, y);
				//selectedTile->m_Hightlight = true;
			}
		}
	}

	for (Building* building : m_Buildings) {
		building->Update(elapsedSec, m_Player.GetCollisionRect().getRectf());
		// building.IsPlayerInHitbox(Rectf{m_Player.GetCollisionRect().pos, m_Player.GetCollisionRect().size});
	}


	if (m_pSelectedTile != nullptr) {
		if (utils::isMouseDown(SDL_BUTTON_LEFT)) {
			m_pSelectedTile->SetTileType(GroundTileTypeManager::GetInstance()->AIR);
		}
	}
	if (!m_ScreenManager->IsScreenOpen()) {
		m_Player.Update(elapsedSec, *this);
	}

	//Move the camera when the player gets to the edge
	//Also lock the camera from going below -800 and above -100
	Vector2f playerPos = m_Player.GetPosition();
	Vector2f newCameraPos = m_pCamera->GetPosition();
	if (playerPos.x < newCameraPos.x + 50) {
		newCameraPos.x = playerPos.x - 50;
	}
	if (playerPos.x > newCameraPos.x + m_Viewport.width - 100) {
		newCameraPos.x = playerPos.x - m_Viewport.width + 100;
	}
	if (playerPos.y < newCameraPos.y + 100) {
		newCameraPos.y = playerPos.y - 100;
	}
	if (playerPos.y > newCameraPos.y + m_Viewport.height - 150) {
		newCameraPos.y = playerPos.y - m_Viewport.height + 150;
	}
	newCameraPos.x = utils::clamp(newCameraPos.x, -800, -100);
	m_pCamera->SetPosition(newCameraPos);

	Screen* screen = m_ScreenManager->GetCurrentScreen();
	if (screen != nullptr) {
		screen->Update(elapsedSec);
	}

	GameManager::GetInstance().Update(elapsedSec);
	m_MainScreen->SetDepth(std::to_string((int)-m_Player.GetPosition().y - 50) + " ft.");
	m_MainScreen->SetScore(std::to_string(GameManager::GetInstance().GetScore()));
	// m_MainScreen->SetFuelMeterValue(GameManager::GetInstance().GetFuel());
	// m_MainScreen->SetHullMeterValue(GameManager::GetInstance().GetHullIntegrity());
	m_MainScreen->Update(elapsedSec);


	//Vector2f playerPos = m_player.GetPosition();
	//Vector2f newCameraPos = playerPos;
	//m_pCamera->SetPosition(newCameraPos);

	//place the player in the center of the camera
	//m_pCamera->SetPosition(Vector2f{playerPos.x - m_viewport.width / 2, playerPos.y - m_viewport.height / 2});
}
void WorldLevel::Draw() const {

	m_pCamera->BeginRendering();

	m_Sun->Draw();
	m_Moon->Draw();

	// m_topCover->Draw(Vector2f{-850,-70} );
	m_TopCover->Draw(Rectf { -850, -70, 850, -70 + 32 }, Rectf { 0, 0, WORLD_WIDTH * 50, 32 });

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

	// utils::SetColor(Colors::WHITE);
	// utils::FillEllipse(m_MousePos, 2, 2);

	for (int x { 0 }; x < WORLD_WIDTH; ++x) {
		for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
			m_GridManager.GetTileAtIndex(x, y)->Draw();
		}
	}

	// utils::SetColor(Colors::MAGENTA);
	// utils::FillEllipse(-5, -5, 5, 5);

	// if (m_pSelectedTile != nullptr) {
	// m_pSelectedTile->Draw();
	// }

	for (Building* building : m_Buildings) {
		building->Draw();
	}

	m_Player.Draw();

	// utils::SetColor(Colors::GREEN);
	// utils::DrawArrow(Vector2f{0, 0}, m_MousePos);

	m_pCamera->EndRendering();

	// utils::FillRect(utils::GetMousePos(), 10, 10);

	// utils::DrawRect(50, 50, m_Viewport.width - 100,  m_Viewport.height - 100);

	const Screen* screen = m_ScreenManager->GetCurrentScreen();
	if (screen != nullptr) {
		screen->Draw();
	}
	m_MainScreen->Draw();


}
void WorldLevel::MouseMove(const Vector2f& mousePos) {
	m_MousePos = mousePos;
}


WorldGridManager& WorldLevel::GetGridManager() {
	return m_GridManager;
}
