#include "pch.h"
#include "WorldTile.h"

#include <iostream>

#include "Camera.h"
#include "colors.h"
#include "../TextureManager.h"
#include "utils.h"
#include "WorldGridManager.h"


WorldTile::WorldTile(const Vector2f& position, GroundTileType* groundTileType, TextureManager* pTextureManager, WorldGridManager* pGridManager) : m_Position { position }, m_GroundTileType { groundTileType }, m_pGridManager { pGridManager } {
	// const std::string dirtPath =  + "tiles/dirt/dirt" + std::to_string(utils::randRange(1, 5))  + ".png";
	// m_pTexture = new Texture(dirtPath);
	m_pTexture = pTextureManager->GetTexture(groundTileType->getPath());

	m_SideTextures[TileDirection::TopLeft] = pTextureManager->GetTexture("tiles/dirt/sidepieces/topLeft.png");
	m_SideTextures[TileDirection::TopRight] = pTextureManager->GetTexture("tiles/dirt/sidepieces/topRight.png");
	m_SideTextures[TileDirection::BottomLeft] = pTextureManager->GetTexture("tiles/dirt/sidepieces/bottomLeft.png");
	m_SideTextures[TileDirection::BottomRight] = pTextureManager->GetTexture("tiles/dirt/sidepieces/bottomRight.png");

	m_SideTextures[TileDirection::TopMiddle] = pTextureManager->GetTexture("tiles/dirt/sidepieces/middleTop.png");
	m_SideTextures[TileDirection::BottomMiddle] = pTextureManager->GetTexture("tiles/dirt/sidepieces/middleBottom.png");
	m_SideTextures[TileDirection::MiddleLeft] = pTextureManager->GetTexture("tiles/dirt/sidepieces/middleLeft.png");
	m_SideTextures[TileDirection::MiddleRight] = pTextureManager->GetTexture("tiles/dirt/sidepieces/middleRight.png");
	

	m_pAllTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/all.png");
}
WorldTile::~WorldTile() {
	delete m_pTexture;
}
void WorldTile::Draw() {


	switch (m_GroundTileType->getType()) {
	case GroundTileTypes::Air: {
		//check if it's all around dirt
		bool allDirt = true;
		for (int i = 0; i < 8; i++) {
			const WorldTile* tile = m_SurroundingTiles.GetTile(static_cast<TileDirection>(i));
			if(tile != nullptr) { //Tile exists
				const GroundTileTypes type = tile->GetTileType()->getType();
				if(type != Tiles::DIRT->getType()) {
					allDirt = false;
					break;
				}
			}
		}
		if(allDirt) {
			m_pAllTexture->Draw(m_Position);
			return;
		}
		if(*m_GroundTileType == Tiles::AIR) {
			this->DrawSide(TileDirection::TopLeft);
			this->DrawSide(TileDirection::TopRight);
			this->DrawSide(TileDirection::BottomLeft);
			this->DrawSide(TileDirection::BottomRight);

			this->DrawSide(TileDirection::TopMiddle);
			this->DrawSide(TileDirection::BottomMiddle);
			this->DrawSide(TileDirection::MiddleLeft);
			this->DrawSide(TileDirection::MiddleRight);
			break;
		}
		
	}
	case GroundTileTypes::Dirt:
	case GroundTileTypes::Hard:
	case GroundTileTypes::Stone:
	case GroundTileTypes::Iron:
		m_pTexture->Draw(m_Position);
		break;
	default:
		break;
	}
	
	
	if (*m_GroundTileType != Tiles::AIR) {
		m_pTexture->Draw(m_Position);
		if (m_Hightlight) {
			utils::SetColor(Colors::GREEN);
			utils::FillRect(m_Position, 50, 50);
		}
	}

	

	
}

void WorldTile::Update(const Camera* camera) {
	m_pGridManager->GetIndexFromPosition(m_Position);
	m_SurroundingTiles = m_pGridManager->GetSurroundingTiles(this);
	const Vector2f mouse_pos = camera->TransformMouse(Vector2f{utils::GetMousePos().x, 500 - utils::GetMousePos().y});
	m_Hightlight = utils::IsPointInRect(mouse_pos, Rectf{GetCollisionRect().pos, GetCollisionRect().size});
}
Collision::TileCollisionRect WorldTile::GetCollisionRect() {
	return Collision::TileCollisionRect { m_Position, GetSize(), ( this ) };
}

void WorldTile::DrawSide(const TileDirection& direction) {
	const WorldTile* tile = m_SurroundingTiles.GetTile(direction);
	if(tile != nullptr) {
		const GroundTileTypes type = tile->GetTileType()->getType();
		if(type != Tiles::AIR->getType()) {
			m_SideTextures[direction]->Draw(m_Position);
		}
	}
}