#pragma once
#include "Collision.h"
#include "Texture.h"
#include "WorldGridManager.h"
#include "../TextureManager.h"

class Camera;

enum class GroundTileTypes
{
	Air,
	Dirt,
	Hard,
	Stone,
	Iron
};

class GroundTileType
{
public:
	GroundTileType(const std::string&& filePath, GroundTileTypes type): m_filePath(filePath), m_type(type) {
	}
	virtual ~GroundTileType() = default;
	bool operator==(const GroundTileType& rhs) const {
		return m_type == rhs.m_type;
	}
	bool operator!=(const GroundTileType& rhs) const {
		return m_type != rhs.m_type;
	}

	virtual bool operator==(const GroundTileType* rhs) const {
		return rhs->m_type == m_type;
	}
	virtual bool operator!=(const GroundTileType* rhs) const {
		return rhs->m_type != m_type;
	}

	virtual std::string getPath() const {
		return m_filePath;
	}

	virtual GroundTileTypes getType() const {
		return m_type;
	}

protected:
	std::string m_filePath;

private:
	GroundTileTypes m_type;
};

class RandomGroundTile : public GroundTileType
{
public:
	RandomGroundTile(const std::string& filePath, GroundTileTypes type, int maxRandom): GroundTileType(std::move(filePath), type), m_maxRandom(maxRandom) {
	}

	~RandomGroundTile() override = default;

	bool operator==(const GroundTileType* rhs) const override {
		return rhs->getType() == this->getType();
	}

	bool operator!=(const GroundTileType* rhs) const override {
		return rhs->getType() != this->getType();
	}

	std::string getPath() const override {
		int variant = utils::randRange(1, m_maxRandom);
		std::string toReplace { "[0]" };
		std::string replacement = std::to_string(utils::randRange(1, m_maxRandom));

		size_t found = m_filePath.find(toReplace);
		std::string newFilePath { m_filePath };

		if (found != std::string::npos) {
			newFilePath.replace(found, 3, replacement);
		}

		return newFilePath;
	}

private:
	int m_maxRandom;
};

namespace Tiles
{
	static GroundTileType* AIR = new GroundTileType("", GroundTileTypes::Air);
	static GroundTileType* DIRT = new RandomGroundTile("tiles/dirt/dirt[0].png", GroundTileTypes::Dirt, 5);
	static GroundTileType* IRON = new GroundTileType("tiles/ores/Ore_Ironium.png", GroundTileTypes::Iron);
	
	namespace Special
	{
		static GroundTileType* HARD_LEFT = new GroundTileType("tiles/dirt/special/hardLeft.png", GroundTileTypes::Hard);
		static GroundTileType* HARD_RIGHT = new GroundTileType("tiles/dirt/special/hardRight.png", GroundTileTypes::Hard);
		static GroundTileType* HARD_MIDDLE = new GroundTileType("tiles/dirt/special/hardMiddle.png", GroundTileTypes::Hard);

		static GroundTileType* GRASS = new RandomGroundTile("tiles/dirt/special/grass[0].png", GroundTileTypes::Dirt, 2);
	}
}

class WorldTile
{
public:
	WorldTile() = default;
	WorldTile(const Vector2f& position, GroundTileType* groundTileType, TextureManager* pTextureManager, WorldGridManager* pGridManager);
	~WorldTile();

	void Draw();
	void Update(const Camera* camera); //TODO: no use

	Vector2f GetPosition() const {
		return m_Position;
	}
	void SetPosition(const Vector2f& position) {
		m_Position = position;
	}

	Vector2f GetSize() const {
		return Vector2f { 50, 50 };
	}

	GroundTileType * GetTileType() const {
		return m_GroundTileType;
	}
	void SetTileType(GroundTileType* type) {
		m_GroundTileType = type;
		m_pTexture = TextureManager::GetInstance()->GetTexture(type->getPath()); //Chage the texture when setting a new type
	}

	Collision::TileCollisionRect GetCollisionRect();

	bool m_Hightlight { false };

private:
	void DrawSide(const TileDirection& direction);
	
	Vector2f m_Position;
	GroundTileType* m_GroundTileType;

	Texture* m_pTexture;

	Collision::CollisionRect m_CollisionRect;

	WorldGridManager* m_pGridManager;

	surroundingTiles m_SurroundingTiles;

	std::vector<Texture*> m_SideTextures { 8, nullptr };

	Texture* m_pAllTexture;
};
