#include "state_main_menu.h"

#include <stdio.h>
#include <stdbool.h>

#include "raylib.h"
#include "raygui.h"

static void state_enter(Presentation_State *state) {
    Presentation_State_Main_Menu_Context *ctx = (Presentation_State_Main_Menu_Context *)state->context;
    (void)ctx;

    ctx->mode = Main_Menu_Mode_Home;
}

static void state_tick(Presentation_State *state) {
    Presentation_State_Main_Menu_Context *ctx = (Presentation_State_Main_Menu_Context *)state->context;

    { // DEBUG
        if(IsKeyPressed(KEY_P)) {
            ctx->is_singleplayer = true;
            ctx->mode = Main_Menu_Mode_Game_Setup;
            game_session_init_default_settings(ctx->is_singleplayer, &ctx->session_settings);

            game_session_create(
                ctx->is_singleplayer,
                !ctx->is_singleplayer,
                ctx->session_settings
            );

            presentation_state_machine_go_to(&presentation_state_ingame);
        }
    }
}

static void state_render(Presentation_State *state) {
    Presentation_State_Main_Menu_Context *ctx = (Presentation_State_Main_Menu_Context *)state->context;

    ClearBackground((Color) {
        230, 204, 138, 255
    });

    #define BUTTON_HEIGHT 32
    
    switch(ctx->mode) {
        case Main_Menu_Mode_Home: {
            if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 0), 128, BUTTON_HEIGHT }, "#191#Singleplayer")) {
                ctx->mode = Main_Menu_Mode_Singleplayer;
            }
            if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 1), 128, BUTTON_HEIGHT }, "#191#Multiplayer")) {
                ctx->mode = Main_Menu_Mode_Multiplayer;
            }
            if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 2), 128, BUTTON_HEIGHT }, "#191#Quit")) {
                *(ctx->should_quit_game) = true;
            }

            break;
        }
        case Main_Menu_Mode_Singleplayer: {
            ctx->is_singleplayer = true;
            ctx->mode = Main_Menu_Mode_Game_Setup;
            game_session_init_default_settings(ctx->is_singleplayer, &ctx->session_settings);

            break;
        }
        case Main_Menu_Mode_Multiplayer: {
            if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 0), 128, BUTTON_HEIGHT }, "#191#Host")) {
                ctx->is_singleplayer = false;
                ctx->mode = Main_Menu_Mode_Game_Setup;
                game_session_init_default_settings(ctx->is_singleplayer, &ctx->session_settings);
            }
            if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 1), 128, BUTTON_HEIGHT }, "#191#Join")) {
                ctx->mode = Main_Menu_Mode_Multiplayer_Join;
            }
            if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 2), 128, BUTTON_HEIGHT }, "#191#Back to Main Menu")) {
                ctx->mode = Main_Menu_Mode_Home;
            }

            break;
        }
        case Main_Menu_Mode_Game_Setup: {
            // TODO: SS - Add options for the game here so players can customize..
            // - public or locked session (unlock with password?)
            // - max-players
            // - game-speed (tickrate)
            // - match-time
            // - map
            // - seed
            // - obstacles
            // - power-ups
            // etc..
            // Modify 'ctx->session_settings'.

            if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 0), 128, BUTTON_HEIGHT }, "#191#Play")) {
                // Set up the ingame-context and transition to the ingame-state.
                // NOTE: SS - We might need to wait before transitioning when playing multiplayer.
                game_session_create(
                    ctx->is_singleplayer,
                    !ctx->is_singleplayer,
                    ctx->session_settings
                );

                presentation_state_machine_go_to(&presentation_state_ingame);
            }
            if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 1), 128, BUTTON_HEIGHT }, "#191#Cancel")) {
                ctx->mode = ctx->is_singleplayer ? Main_Menu_Mode_Home : Main_Menu_Mode_Multiplayer;
            }

            break;
        }
        case Main_Menu_Mode_Multiplayer_Join: {
            // TODO: SS - Add text-input here so the player can specify what session to try connecting to.

            if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 1), 128, BUTTON_HEIGHT }, "#191#Connect")) {
                printf("TODO: SS - Connect to session id.\n");
            }

            if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 2), 128, BUTTON_HEIGHT }, "#191#Cancel")) {
                ctx->mode = Main_Menu_Mode_Multiplayer;
            }
            
            break;
        }
    }
}

static void state_exit(Presentation_State *state) {
    (void)state;
    printf("Exited main menu\n");
}

Presentation_State presentation_state_main_menu;

void presentation_state_main_menu_init(Presentation_State_Main_Menu_Context *ctx) {
    presentation_state_main_menu = (Presentation_State) {
        .name = "Main Menu",
        .context = (void *)ctx,
        .enter = state_enter,
        .tick = state_tick,
        .render = state_render,
        .exit = state_exit
    };
}