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

typedef struct
{
	Rectangle rect;
	int texture_frame;
	int speed;
	int speed_min;
	int speed_max;
	int direction;
	Color colour;
	int is_active;
} car;

#define CAR_COUNT 30
car cars[CAR_COUNT];

int random_speed;

void setup_cars(void)
{
	Rectangle rect_values[30] = {
		{ 960, 88,64,40},
		{1088, 88,64,40},
		{1217, 88,64,40},

		{ 960,148,64,40},
		{1088,148,64,40},
		{1217,148,64,40},

		{ 960,208,64,40},
		{1088,208,64,40},
		{1217,208,64,40},

		{ 960,268,64,40},
		{1088,268,64,40},
		{1217,268,64,40},

		{ 960,328,64,40},
		{1088,328,64,40},
		{1217,328,64,40},

		{ 240,396,64,40},
		{ 120,396,64,40},
		{   0,396,64,40},

		{ 240,456,64,40},
		{ 120,456,64,40},
		{   0,456,64,40},

		{ 240,516,64,40},
		{ 120,516,64,40},
		{   0,516,64,40},

		{ 240,576,64,40},
		{ 120,576,64,40},
		{   0,576,64,40},

		{ 240,636,64,40},
		{ 120,636,64,40},
		{   0,636,64,40}
	};


	int speed_values[10] = {200,400,800,1000,1500,1500,1000,800,400,200};

	Color colour_values[10] = {
		{156, 32, 32,255},
		{180,228,144,255},
		{100,100, 16,255},
		{224,136,136,255},
		{ 28, 32,156,255},
		{152, 92, 40,255},
		{104,116,208,255},
		{176, 60, 60,255},
		{132,180,104,255},
		{208,208, 80,255}
	};

	// -------------------------------------------------- Sets speed members
	for ( int car_index = 0, value_index = 0;  car_index < CAR_COUNT;   car_index+=3, value_index++)
	{
		// ----------------------------------------------------------- Set rects
		cars[car_index  ].rect = rect_values[car_index  ];
		cars[car_index+1].rect = rect_values[car_index+1];
		cars[car_index+2].rect = rect_values[car_index+2];

		// ------------------------------------------------------- Set max speed
		cars[car_index  ].speed_max = speed_values[value_index];
		cars[car_index+1].speed_max = speed_values[value_index];
		cars[car_index+2].speed_max = speed_values[value_index];

		// ------------------------------------------------------- Set min speed
		cars[car_index  ].speed_min = speed_values[value_index]-100;
		cars[car_index+1].speed_min = speed_values[value_index]-100;
		cars[car_index+2].speed_min = speed_values[value_index]-100;

		// ----------------------------------------------------------- Set speed
		random_speed = GetRandomValue( cars[car_index].speed_min, cars[car_index].speed_max);
		cars[car_index  ].speed = random_speed;
		cars[car_index+1].speed = random_speed;
		cars[car_index+2].speed = random_speed;		

		// ----------------------------------- Set texture frames and directions
		if (car_index < 15)
		{
			cars[car_index  ].texture_frame = 0;
			cars[car_index+1].texture_frame = 0;
			cars[car_index+2].texture_frame = 0;
			cars[car_index].direction = 1;
			cars[car_index+1].direction = 1;
			cars[car_index+2].direction = 1;
		}
		else
		{
			cars[car_index  ].texture_frame = 1;
			cars[car_index+1].texture_frame = 1;
			cars[car_index+2].texture_frame = 1;
			cars[car_index  ].direction = -1;
			cars[car_index+1].direction = -1;
			cars[car_index+2].direction = -1;
		}

		// --------------------------------------------------------- Set colours
		cars[car_index  ].colour = colour_values[value_index];
		cars[car_index+1].colour = colour_values[value_index];
		cars[car_index+2].colour = colour_values[value_index];

		// ----------------------------------------------------------- Is active
		cars[car_index  ].is_active = 0;
		cars[car_index+1].is_active = 0;
		cars[car_index+2].is_active = 0;
	}
}

void process_cars(void)
{
	if (state != 1) // This is to stop a single frame from being processed when going from state 1-0 in process_players
	{
		return;
	}
	// ------------------------------------------------------- Movement loop
	for (int i = 0; i < CAR_COUNT; i+=3)
	{
		// --------------------------------- left border boundry check and reset
		if (cars[i].rect.x < -320)
		{
			cars[i].rect.x   = 1280;
			cars[i+1].rect.x = 1400;
			cars[i+2].rect.x = 1520;

			random_speed = GetRandomValue(cars[i].speed_min,cars[i].speed_max);

			cars[i].speed   = random_speed;
			cars[i+1].speed = random_speed;
			cars[i+2].speed = random_speed;
		}
		// -------------------------------- right border boundry check and reset
		else if (cars[i].rect.x > 1600)
		{
			cars[i].rect.x = -64;
			cars[i+1].rect.x = -184;
			cars[i+2].rect.x = -304;

			random_speed = GetRandomValue(cars[i].speed_min,cars[i].speed_max);

			cars[i].speed   = random_speed;
			cars[i+1].speed = random_speed;
			cars[i+2].speed = random_speed;
		}
		// -------------------------------------------------- movement increment
		else
		{
		 	cars[i].rect.x   -= (cars[i].speed   * delta) * cars[i].direction;
		 	cars[i+1].rect.x -= (cars[i+1].speed * delta) * cars[i+1].direction;
		 	cars[i+2].rect.x -= (cars[i+2].speed * delta) * cars[i+2].direction;
		}	

		// ----------------------------------------------------------- Draw cars
		if (cars[i].is_active)
		{
			DrawTexture(car_textures[cars[i].texture_frame], cars[i].rect.x, cars[i].rect.y, cars[i].colour);
		}

		if (cars[i + 1].is_active)
		{
			DrawTexture(car_textures[cars[i + 1].texture_frame], cars[i + 1].rect.x, cars[i + 1].rect.y, cars[i + 1].colour);
		}

		if (cars[i + 2].is_active)
		{
			DrawTexture(car_textures[cars[i + 2].texture_frame], cars[i + 2].rect.x, cars[i + 2].rect.y, cars[i + 2].colour);
		}
	}
}
