#include "BreakoutLGCircle.h"

#include "SDL.h"
#include <math.h>

#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))

BreakoutLGCircle::BreakoutLGCircle()
{
}

BreakoutLGCircle::~BreakoutLGCircle()
{
}

BreakoutLevel* BreakoutLGCircle::GenerateLevel(BreakoutLocationArray* pAvailableLocations, const Vector2i &boxSize, const int &maxHealth)
{
	srand(SDL_GetTicks());
	bool bInwardIncreasingHealth = (rand() % 2) > 0 ? true : false;

	BreakoutLevel* pLevel = new BreakoutLevel();

	int width = pAvailableLocations->GetWidth();
	int height = pAvailableLocations->GetHeight();

	//So it fits while being somewhat circular within the area.
	int radius = height / 3;

	//Health values, also acts as a map of where objects are to prevent multiple
	//blocks being created at the same spot.
	int** locationUsedArray = new int*[width]; 
	for(int i = 0; i < width; i++)
		locationUsedArray[i] = new int[height];

	for(int x = 0; x < width; x++)
	{
		for(int y = 0; y < width; y++)
		{
			locationUsedArray[x][y] = 0;
		}
	}

	Vector2i centre = Vector2i(pAvailableLocations->GetWidth() / 2, pAvailableLocations->GetHeight() / 2);

	float fRad = (float)radius;
	for(int r = 0; r < radius; r++)
	{
		fRad = (float)r;
		for(int i = 0; i < 360; i++)
		{
			float degInRad = DEGTORAD(i);
			
			int x = radius - (int)(cos(degInRad) * fRad);
			int y = radius - (int)(sin(degInRad) * fRad);
			float pecentageOfTotalRadius = (float)r / (float)radius;
			int resultantHealthValue = static_cast<int>((pecentageOfTotalRadius * (float)maxHealth) + 0.5f) + 1;

			if(x < width && y < height && locationUsedArray[x][y] == 0)
			{
				if(bInwardIncreasingHealth)
				{
					int health = maxHealth - resultantHealthValue;
					locationUsedArray[x][y] = max(health, 1);
				}
 				else
 					locationUsedArray[x][y] = max(resultantHealthValue, 1);
			}
		}
	}

	for(int x = 0; x < width; x++)
	{
		for(int y = 0; y < width; y++)
		{
			if(locationUsedArray[x][y] > 0)
			{
				Vector2i pos = pAvailableLocations->GetLocation(x, y);
				int health = locationUsedArray[x][y];
				pLevel->push_back(new BreakoutBox(pos, boxSize, health));
			}
		}
	}

	return pLevel;
}
