#include "raylib.h"
#include "image_loading.h"
#include "main.h"
#include "cars.h"

int menu_y = 0;
int difficulty = 1;

char * difficulty_labels[] = {
	"Difficulty <  easy  >",
	"Difficulty < medium >",
	"Difficulty <  hard  >"
};

char * gore_labels[] = {
	"Gore       <   off  >",
	"Gore       <   on   >"
};

int capped_increment(int value, int amount, int min, int max)
{
	value += amount;
	if      (value > max)
	{
		return min;
	}
	else if (value < min)
	{
		return max;
	}
	else
	{
		return value;
	}
}

void process_menu(void)
{

	if (IsKeyPressed(KEY_ESCAPE))
	{
		engine_running = 0;
		return;
	}

	// --------------------------------------------------- move menu up and down
	if (IsKeyPressed(KEY_UP))
	{
		menu_y = capped_increment(menu_y, -1, 0, 3);
	}
	else if (IsKeyPressed(KEY_DOWN))
	{
		menu_y = capped_increment(menu_y,  1, 0, 3);
	}

	// ----------------------------------------------------------- menu on start
	if      (menu_y == 0 && (IsKeyDown(KEY_ENTER) || IsKeyDown(KEY_KP_ENTER)) )
	{
		switch (difficulty)
		{
			case 0:
				for (int i = 2; i < CAR_COUNT; i+=3)
				{
					cars[i].is_active = 1;
				}
				break;

			case 1:
				for (int i = 2; i < CAR_COUNT; i+=3)
				{
					cars[i-1].is_active = 1;
					cars[i].is_active = 1;
				}
				break;

			case 2:
				for (int i = 0; i < CAR_COUNT; i++)
				{
					cars[i].is_active = 1;
				}
				break;
		}
		state = 1;
	}
	// ----------------------------------------------- menu on difficulty select
	else if ( menu_y == 1 )
	{
		if (IsKeyPressed(KEY_RIGHT))
		{
			difficulty = capped_increment(difficulty,  1, 0, 2);
		}
		else if (IsKeyPressed(KEY_LEFT))
		{
			difficulty = capped_increment(difficulty, -1, 0, 2);
		}
	}
	// ----------------------------------------------------- menu on gore select
	else if ( menu_y == 2 && ( IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_RIGHT) ))
	{
		gore = !gore;
	}
	// ------------------------------------------------------------ Menu on quit
	else if ( menu_y == 3 && (IsKeyDown(KEY_ENTER) || IsKeyDown(KEY_KP_ENTER)))
	{
		 engine_running = 0;
	}

	// ---------------------------------------------------------- Draw text menu
	DrawText(">",          340, 82+(60 * menu_y), 50, WHITE);
	DrawText("Start",                       395, 82, 50, WHITE);	
	DrawText(difficulty_labels[difficulty], 395, 142, 50, WHITE);	
	DrawText(gore_labels[gore],             395, 202, 50, WHITE);	
	DrawText("Quit",                        395, 262, 50, WHITE);	

	// --------------------------------------------------------------- Draw cars
	switch (difficulty)
	{
		case 0:
			for (int i = 2; i < CAR_COUNT; i+=3)
			{
				DrawTexture(car_textures[cars[i].texture_frame], cars[i].rect.x, cars[i].rect.y, cars[i].colour);
			}
			break;

		case 1:
			for (int i = 2; i < CAR_COUNT; i+=3)
			{
				DrawTexture(car_textures[cars[i-1].texture_frame], cars[i-1].rect.x, cars[i-1].rect.y, cars[i-1].colour);
				DrawTexture(car_textures[cars[i  ].texture_frame], cars[i  ].rect.x, cars[i  ].rect.y, cars[i  ].colour);
			}
			break;

		case 2:
			for (int i = 0; i < CAR_COUNT; i++)
			{
				DrawTexture(car_textures[cars[i].texture_frame], cars[i].rect.x, cars[i].rect.y, cars[i].colour);
			}
			break;
	}
}
