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

typedef struct
{
	Rectangle rect;
	int texture_frame;
	float speed;
	float is_dead;
	int up_key;
	int down_key;
	int score;
	int score_display_x;
} player;

player player_1;
player player_2;

void setup_players()
{
	player_1 = (player){
		(Rectangle){399,720,56,32},
		0,
		250,
		0,
		KEY_W,
		KEY_S,
		0,
		377
	};

	player_2 = (player){
		(Rectangle){830,720, 56,32},
		0,
		250,
		0,
		KEY_UP,
		KEY_DOWN,
		0,
		804
	};
}

void check_and_handle_collision(player * player_n, int car_start, int car_end)
{
	for (int i = car_start; i < car_end + 1; i++)
	{
		if ( CheckCollisionRecs(cars[i].rect, player_n->rect) && !player_n->is_dead && cars[i].is_active)
		{
			player_n->is_dead = 2;
			if (gore)
			{
				player_n->texture_frame = GetRandomValue(2,4);
			}
			else
			{
				player_n->texture_frame = 5;
			}
			play_explosion_sound();
		}
	}
}

void update_player(player * player_n)
{
	// -------------------------------------------------------------- Draw score
	DrawTexture(number_textures[player_n->score / 10], player_n->score_display_x   , 0, WHITE);
	DrawTexture(number_textures[player_n->score % 10], player_n->score_display_x+52, 0, WHITE);
	DrawRectangle(732,584,536,28, (Color){144,144,144,255});
	// ------------------------------------------------------------- player dead
	if (player_n->is_dead)
	{
		player_n->is_dead -= delta;
		if (player_n->is_dead < 1)
		{
			player_n->is_dead = 0;
			player_n->rect.y = 720;
		}
		DrawTexture(player_textures[player_n->texture_frame], player_n->rect.x, player_n->rect.y, WHITE);
		return;
	}
	// ------------------------------------------------------------------ Up key

	if   (IsKeyDown(player_n->up_key) && player_n->rect.y < 690)
	{
		player_n->rect.y -= player_n->speed * delta;
		player_n->texture_frame = ((int)ticks / 100) % 2;
	}
	// ---------------------------------------------------------------- Down key
	else if (IsKeyDown(player_n->down_key) && player_n->rect.y <= 688 )
	{
		player_n->rect.y += player_n->speed * delta;
		player_n->texture_frame = ((int)ticks / 100) % 2;
	}
	else 
	{
		player_n->texture_frame = 0;
	}

	// ------------------------------------------------------------- Top boundry
	if (player_n->rect.y < 44)
	{
		play_pickup_coin_sound();
		if (player_n->score < 99)
		{
			player_n->score += 1;
		}
		player_n->rect.y = 720;
	}
	// ---------------------------------------------------------- Bottom boundry
	else if      (player_n->rect.y > 684)
	{
		player_n->rect.y -= 1;
	}

	// -------------------------------------------------------------- Collisions
    switch ((int)player_n->rect.y) 
    {
        case 40 ... 138:
        	check_and_handle_collision(player_n, 0,5);
            break;
        case 139 ... 198:
        	check_and_handle_collision(player_n, 3,8);
            break;
        case 199 ... 258:
        	check_and_handle_collision(player_n, 6,11);
            break;
        case 259 ... 318:
        	check_and_handle_collision(player_n, 9,14);
            break;
        case 319 ... 382:
        	check_and_handle_collision(player_n, 12,17);
            break;
        case 383 ... 446:
        	check_and_handle_collision(player_n, 15,20);
            break;
        case 447 ... 506:
        	check_and_handle_collision(player_n, 18,23);
            break;
        case 507 ... 566:
        	check_and_handle_collision(player_n, 21,26);
            break;
        case 567 ... 626:
        	check_and_handle_collision(player_n, 24,29);
            break;
        case 627 ... 686:
        	check_and_handle_collision(player_n, 27,29);
            break;
	};
	// ------------------------------------------------------------- Draw player
	DrawTexture(player_textures[player_n->texture_frame], player_n->rect.x, player_n->rect.y, WHITE);
}	

void process_players(void)
{
	if (IsKeyPressed(KEY_ESCAPE))
	{
		setup_players();
		setup_cars();
		state = 0;
		return;
	}
	update_player(&player_1);
	update_player(&player_2);
}
