#include "simulation_world.h"

#include <string.h>
#include <stdio.h>
#include <assert.h>

#define SIM_COMMANDS_PER_PLAYER 4

Simulation_World simulation_create_world(uint32_t seed, bool online, uint8_t max_players, uint16_t width, uint16_t height) {
    Simulation_World w;
    memset(&w, 0, sizeof(Simulation_World));

    w.tick = 0;
    
    w.online = online;
    
    w.game_world    = game_world_create(seed, max_players, width, height);
    assert(w.game_world != NULL);
    
    w.match_state   = Simulation_Match_State_Waiting_To_Start;
    
    w.players       = (Simulation_Player *)calloc(max_players, sizeof(Simulation_Player));
    w.max_players   = max_players;
    
    w.max_commands  = w.max_players * SIM_COMMANDS_PER_PLAYER;
    w.command_count = 0;
    w.commands      = (Simulation_Command *)calloc(w.max_commands, sizeof(Simulation_Command));

    return w;
}

void simulation_destroy_world(Simulation_World *simulation_world) {
    assert(simulation_world != NULL);
    
    game_world_destroy(simulation_world->game_world);
    simulation_world->game_world = NULL;
    
    simulation_world->match_state = Simulation_Match_State_Waiting_To_Start;
    
    free(simulation_world->players);
    simulation_world->players = NULL;
    
    free(simulation_world->commands);
    simulation_world->commands = NULL;
}

void simulation_world_tick(Simulation_World *simulation_world) {
    assert(simulation_world != NULL);

    // printf("TICK: %lu.\n", simulation_world->tick);

    Game_World *gw = simulation_world->game_world;
    assert(gw != NULL);

    for(uint16_t i = 0; i < simulation_world->command_count; i++) {
        Simulation_Command *cmd = &simulation_world->commands[i];
        // printf("Command type: %i, player id: %i.\n", cmd->type, cmd->player_id);

        Simulation_Player *player = &simulation_world->players[cmd->player_id];
        assert(player != NULL);

        switch(cmd->type) {
            case Simulation_Command_Type_Add_Player: {
                // printf("Simulation_Command_Type_Add_Player\n");
                assert(!player->active);
                player->active = true;

                assert(game_world_add_player(gw, &player->game_world_player));

                Game_World_Player *gwp = player->game_world_player;

                uint16_t spawn_x = 0;
                uint16_t spawn_y = 0;
                assert(game_world_find_position_to_spawn(gw, &spawn_x, &spawn_y));
                
                assert(game_world_create_entity(
                    gw,
                    Entity_Type_Snake_Head,
                    spawn_x, spawn_y,
                    &gwp->entity_id
                ));

                Entity *player_entity = game_world_try_get_entity_by_id(gw, gwp->entity_id);

                // Set initial move-direction for player-entities.
                player_entity->move_direction = game_world_choose_initial_move_direction_based_on_coords(
                    gw,
                    spawn_x, spawn_y
                );

                break;
            }
            case Simulation_Command_Type_Remove_Player: {
                // printf("Simulation_Command_Type_Remove_Player\n");
                assert(player->active);
                player->active = false;

                assert(game_world_remove_player(gw, player->game_world_player));                
                break;
            }
            case Simulation_Command_Type_Set_Player_Input: {
                // printf("Simulation_Command_Type_Set_Player_Input\n");
                assert(player->active);
                player->input = cmd->player_input;
                
                break;
            }
        }
    }
    simulation_world->command_count = 0;

    switch(simulation_world->match_state) {
        case Simulation_Match_State_Waiting_To_Start: {
            if(!simulation_world->online) { // If not online, just go to Counting_Down.
                simulation_world->match_state = Simulation_Match_State_Counting_Down;
            }
            else {
                if(simulation_world_amount_of_active_players(simulation_world) > 1) {
                    simulation_world->match_state = Simulation_Match_State_Counting_Down;
                }
            }
            
            break;
        }
        case Simulation_Match_State_Counting_Down: {
            if(simulation_world->tick > 20) { // TEMP: SS - Hardcoded number and condition to break out of this state.
                simulation_world->match_state = Simulation_Match_State_Active;
            }
            
            break;
        }
        case Simulation_Match_State_Active: {
            if(simulation_world->tick > 500) { // TEMP: SS - Hardcoded number and condition to break out of this state.
                simulation_world->match_state = Simulation_Match_State_Ended;
            }
            else {
                Game_World *gw = simulation_world->game_world;
        
                // Loop over all players in the simulation.
                for(uint16_t i = 0; i < simulation_world->max_players; i++) {
                    Simulation_Player *sim_player = &simulation_world->players[i];
                    if(!sim_player->active) {
                        continue;
                    }

                    Game_World_Player *gw_player = sim_player->game_world_player;
                    assert(gw_player != NULL);
                    if(!gw_player->active) {
                        continue;
                    }
                    
                    // NOTE: SS - Not sure if this should be moved to game-world.. This is input that comes from the simulation.
    
                    // printf("* Input for player %i - right: %i, left: %i.\n",
                    //     i,
                    //     player->input.right, player->input.left
                    // );
    
                    Entity *player_entity = game_world_try_get_entity_by_id(gw, gw_player->entity_id);
                    if(player_entity == NULL) {
                        // No entity to control. No entity to apply input on.
                        continue;
                    }

                    // Snakes can't go backwards. They have to turn.

                    // We only have input 'right' and 'up'.
                    Simulation_Game_Input input = sim_player->input;

                    // Turn relative to the player's current move_direction.
                    switch(player_entity->move_direction) {
                        case Entity_Movement_Direction_None: {
                            break;
                        }
                        case Entity_Movement_Direction_Up: {
                            if(input.right) {
                                player_entity->move_direction = Entity_Movement_Direction_Right;
                            }
                            else if(input.left) {
                                player_entity->move_direction = Entity_Movement_Direction_Left;
                            }
                            
                            break;
                        }
                        case Entity_Movement_Direction_Down: {
                            if(input.right) {
                                player_entity->move_direction = Entity_Movement_Direction_Left;
                            }
                            else if(input.left) {
                                player_entity->move_direction = Entity_Movement_Direction_Right;
                            }
                            
                            break;
                        }
                        case Entity_Movement_Direction_Right: {
                            if(input.right) {
                                player_entity->move_direction = Entity_Movement_Direction_Down;
                            }
                            else if(input.left) {
                                player_entity->move_direction = Entity_Movement_Direction_Up;
                            }

                            break;
                        }
                        case Entity_Movement_Direction_Left: {
                            if(input.right) {
                                player_entity->move_direction = Entity_Movement_Direction_Up;
                            }
                            else if(input.left) {
                                player_entity->move_direction = Entity_Movement_Direction_Down;
                            }

                            break;
                        }
                    }
                }
                
                game_world_tick(gw);
            }
            
            break;
        }
        case Simulation_Match_State_Ended: {
            printf("Match over!\n");
            
            break;
        }
    }

    simulation_world->tick += 1; // TODO: SS - Stop ticking if match has ended.
}

bool simulation_world_enqueue_command(Simulation_World *simulation_world, Simulation_Command command) {
    assert(simulation_world != NULL);

    if(simulation_world->command_count >= simulation_world->max_commands) {
        return false;
    }
    
    simulation_world->commands[simulation_world->command_count] = command;
    simulation_world->command_count += 1;
    
    return true;
}

uint16_t simulation_world_amount_of_active_players(Simulation_World *simulation_world) {
    assert(simulation_world != NULL);
    
    uint16_t count = 0;

    for(uint16_t i = 0; i < simulation_world->max_players; i++) {
        Simulation_Player *player = &simulation_world->players[i];
        if(player->active) {
            count += 1;
        }
    }
    
    return count;
}