#include "Game.h"
#include "TextureManager.h"
#include "Map.h"
#include "ECS/Components.h"
#include "Vector2D.h"
#include "Collision.h"



Map* map;
Manager manager;


SDL_Renderer* Game::renderer = nullptr;
SDL_Event Game::event;

auto& player(manager.addEntity());
auto& wall(manager.addEntity());
auto& wall2(manager.addEntity());
auto& wall3(manager.addEntity());
auto& wall4(manager.addEntity());
auto& wall5(manager.addEntity());
auto& wall6(manager.addEntity());




Game::Game()
{}

Game::~Game()
{}

void Game::init(const char* title, int width, int height, bool fullscreen)
{
	int flags = 0;
	
	if (fullscreen)
	{
		flags = SDL_WINDOW_FULLSCREEN;
	}

	if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
	{
		window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
		renderer = SDL_CreateRenderer(window, -1, 0);
		std::cout << "Abriendo Programa" << std::endl;
		if (renderer)
		{
			SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
		}

		isRunning = true;
	}
	
	map = new Map();

	player.addComponent<TransformComponent>(2);
	player.addComponent<SpriteComponent>("assets/narutomovimiento.png", 4, 100);
	player.addComponent<KeyboardController>();
	player.addComponent<ColliderComponent>("player");

	
	wall.addComponent<TransformComponent>(500.0f, 130.0f, 400, 30, 1);
	wall.addComponent<SpriteComponent>("assets/dirt.png");
	wall.addComponent<ColliderComponent>("wall");

	wall2.addComponent<TransformComponent>(290.0f, 320.0f, 400, 30, 1);
	wall2.addComponent<SpriteComponent>("assets/dirt.png");
	wall2.addComponent<ColliderComponent>("wall");

	wall3.addComponent<TransformComponent>(0.0f, 320.0f, 30, 300, 1);
	wall3.addComponent<SpriteComponent>("assets/dirt.png");
	wall3.addComponent<ColliderComponent>("wall");

	wall4.addComponent<TransformComponent>(230.0f, 130.0f, 30, 700, 1);
	wall4.addComponent<SpriteComponent>("assets/dirt.png");
	wall4.addComponent<ColliderComponent>("wall");

	wall5.addComponent<TransformComponent>(0.0f, 130.0f, 30, 50, 1);
	wall5.addComponent<SpriteComponent>("assets/dirt.png");
	wall5.addComponent<ColliderComponent>("wall");

	wall6.addComponent<TransformComponent>(225.0f, 0.0f, 150, 30, 1);
	wall6.addComponent<SpriteComponent>("assets/dirt.png");
	wall6.addComponent<ColliderComponent>("wall");

} 

void Game::handleEvents()
{ 

	SDL_PollEvent(&event);

	switch (event.type)
	{
	case SDL_QUIT :
		isRunning = false;
		break;
	default:
		break;
	}
}

void Game::update()
{
	manager.refresh();
	manager.update();

	if (Collision::AABB(player.getComponent<ColliderComponent>().collider,
		wall.getComponent<ColliderComponent>().collider))
	{
		player.getComponent<TransformComponent>().velocity * -1;
		std::cout << "Wall collision" << std::endl;
	}
	if (Collision::AABB(player.getComponent<ColliderComponent>().collider,
		wall2.getComponent<ColliderComponent>().collider))
	{
		player.getComponent<TransformComponent>().velocity * -1;
		std::cout << "Wall collision" << std::endl;
	}

	if (Collision::AABB(player.getComponent<ColliderComponent>().collider,
		wall3.getComponent<ColliderComponent>().collider))
	{
		player.getComponent<TransformComponent>().velocity * -1;
		std::cout << "Wall collision" << std::endl;
	}

	if (Collision::AABB(player.getComponent<ColliderComponent>().collider,
		wall4.getComponent<ColliderComponent>().collider))
	{
		player.getComponent<TransformComponent>().velocity * -1;
		std::cout << "Wall collision" << std::endl;
	}

	if (Collision::AABB(player.getComponent<ColliderComponent>().collider,
		wall5.getComponent<ColliderComponent>().collider))
	{
		player.getComponent<TransformComponent>().velocity * -1;
		std::cout << "Wall collision" << std::endl;
	}

	if (Collision::AABB(player.getComponent<ColliderComponent>().collider,
		wall6.getComponent<ColliderComponent>().collider))
	{
		player.getComponent<TransformComponent>().velocity * -1;
		std::cout << "Wall collision" << std::endl;
	}
}

void Game::render()
{
	SDL_RenderClear(renderer);
	map->DrawMap();														
	manager.draw();
	SDL_RenderPresent(renderer);
}

void Game::clean()
{
	SDL_DestroyWindow(window);
	SDL_DestroyRenderer(renderer);
	SDL_Quit();
	std::cout << "cerrado" << std::endl;
}