#pragma region include

#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include "time.h"
#include<Windows.h>
#include "Ball.h"
#include "Helper.h"
#include "LTexture.h"

#pragma endregion

#pragma region SDLFields

bool init()
{
	//Initialization flag
	bool success = true;

	//Initialize SDL
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
		success = false;
	}
	else
	{
		//Set texture filtering to linear
		if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
		{
			printf("Warning: Linear texture filtering not enabled!");
		}

		//Create window
		Helper::gWindow = SDL_CreateWindow("Kule Bilardowe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Helper::SCREEN_WIDTH, Helper::SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
		if (Helper::gWindow == NULL)
		{
			printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
			success = false;
		}
		else
		{
			//Create vsynced renderer for window
			Helper::gRenderer = SDL_CreateRenderer(Helper::gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
			if (Helper::gRenderer == NULL)
			{
				printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
				success = false;
			}
			else
			{
				//Initialize renderer color
				SDL_SetRenderDrawColor(Helper::gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);

				//Initialize PNG loading
				int imgFlags = IMG_INIT_PNG;
				if (!(IMG_Init(imgFlags) & imgFlags))
				{
					printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
					success = false;
				}
			}
		}
	}

	return success;
}

bool loadMedia()
{
	//Loading success flag
	bool success = true;

	//Load Ball texture
	if (!Helper::gBallTexture.loadFromFile("dot.bmp"))
	{
		printf("Failed to load Ball texture!\n");
		success = false;
	}

	return success;
}

void close()
{
	//Free loaded images
	Helper::gBallTexture.free();

	//Destroy window	
	SDL_DestroyRenderer(Helper::gRenderer);
	SDL_DestroyWindow(Helper::gWindow);
	Helper::gWindow = NULL;
	Helper::gRenderer = NULL;

	//Quit SDL subsystems
	IMG_Quit();
	SDL_Quit();
}

#pragma endregion init/load/close

int main(int argc, char* args[])
{
	int n = 10;

	srand(time(NULL));

	if (!init())
	{
		printf("Failed to initialize!\n");
	}
	else
	{
		if (!loadMedia())
		{
			printf("Failed to load media!\n");
		}
		else
		{
			bool quit = false;
			SDL_Event e;

			Ball *ballTab = new Ball[n];
			double poz = 20;
			for (int i = 0; i < n; i++)
			{
				ballTab[i].InitializeBall(15 + poz, 0 + poz);
				poz += 20;
			}
			while (!quit)
			{
				while (SDL_PollEvent(&e) != 0)
				{
					if (e.type == SDL_QUIT)
					{
						quit = true;
					}
				}

				for (int i = 0; i < n; i++)
				{
					ballTab[i].move();
					for (int j = i + 1; j < n; j++)
					{
						ballTab[i].checkCollision(ballTab[j]);
					}
				}

				for (int i = 0; i < n; i++)
				{
					ballTab[i].UpdateSpeed();
				}

				SDL_SetRenderDrawColor(Helper::gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
				SDL_RenderClear(Helper::gRenderer);
				for (int i = 0; i < n; i++)
				{
					ballTab[i].render();
				}
				SDL_RenderPresent(Helper::gRenderer);
			}
		}
	}

	close();
	return 0;
}