#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "menu.h"
#include "widget.h"
#include "../interface/katconfig.h"
#include "../core/sio.h"
#include "modal.h"
#include "guitop.h"
#include "filebrowser.h"
#include "theme.h"

extern struct theme g_theme;
extern struct modal_config g_modal_cfg;

/* Static variables for media creation settings */
static int disk_type_index = 0;
static int disk_sectors_index = 0;
static char cassette_description[64] = "";

/* Disk type options */
static const char *disk_type_options[] = {
    "128 / 128",
    "256 / 128",
    "256 / 256",
    "256 / SIO2PC"
};
static const int disk_type_values[] = {
    SIO_DSK_128,
    SIO_DSK_256_BOOT_128,
    SIO_DSK_256_BOOT_256,
    SIO_DSK_256_BOOT_SIO2PC
};
#define NUM_DISK_TYPES 4

/* Disk sectors options */
static const char *disk_sectors_options[] = {
    "720",
    "1040",
    "1440"
};
static const int disk_sectors_values[] = {
    720,
    1040,
    1440
};
#define NUM_DISK_SECTORS 3

/* Get string for disk type dropdown */
static char *get_disk_type_string(struct option_widget *wgt, char *label)
{
    int index = *(int *)wgt->data;
    if (index >= 0 && index < NUM_DISK_TYPES)
        strcpy(label, disk_type_options[index]);
    else
        strcpy(label, disk_type_options[0]);
    return label;
}

/* Get string for disk sectors dropdown */
static char *get_disk_sectors_string(struct option_widget *wgt, char *label)
{
    int index = *(int *)wgt->data;
    if (index >= 0 && index < NUM_DISK_SECTORS)
        strcpy(label, disk_sectors_options[index]);
    else
        strcpy(label, disk_sectors_options[0]);
    return label;
}

/* Post-show callback for disk creation */
static void media_create_disk_post(struct modal_config *cfg)
{
    char *filename;

    if (cfg->result == 0) {
        /* User cancelled */
        return;
    }

    filename = filebrowser_result();
    if (filename && strlen(filename) > 0) {
        int sectors = disk_sectors_values[disk_sectors_index];
        int type = disk_type_values[disk_type_index];
        sio_create_disk(filename, sectors, type);
    }
}

/* Post-show callback for cassette creation */
static void media_create_cassette_post(struct modal_config *cfg)
{
    char *filename;

    if (cfg->result == 0) {
        /* User cancelled */
        return;
    }

    filename = filebrowser_result();
    if (filename && strlen(filename) > 0) {
        sio_create_cassette(filename, cassette_description);
    }
}

/* Task to open file browser for disk creation */
static void media_disk_create_task(struct option_widget *wgt, void *data)
{
    (void)wgt;
    (void)data;

    memset(&g_modal_cfg, 0, sizeof(g_modal_cfg));
    g_modal_cfg.modal_type = MODAL_FB_SAVE;
    g_modal_cfg.modal_active = 1;
    strcpy(g_modal_cfg.title, "Save Disk (Use .atr extension)");
    strcpy(g_modal_cfg.accept_btn_label, "Save");
    strcpy(g_modal_cfg.cancel_btn_label, "Cancel");
    g_modal_cfg.post_show = media_create_disk_post;

    filebrowser_init(&g_modal_cfg);
}

/* Task to open file browser for cassette creation */
static void media_cassette_create_task(struct option_widget *wgt, void *data)
{
    (void)wgt;
    (void)data;

    memset(&g_modal_cfg, 0, sizeof(g_modal_cfg));
    g_modal_cfg.modal_type = MODAL_FB_SAVE;
    g_modal_cfg.modal_active = 1;
    strcpy(g_modal_cfg.title, "Save Cassette (Use .cas extension)");
    strcpy(g_modal_cfg.accept_btn_label, "Save");
    strcpy(g_modal_cfg.cancel_btn_label, "Cancel");
    g_modal_cfg.post_show = media_create_cassette_post;

    filebrowser_init(&g_modal_cfg);
}

/* Helper function to initialize widget array */
static void init_widget_array(struct option_widget *wid_ary, int menu_prev)
{
    int i;
    for (i = 0; i < MAX_OPTIONS; i++) {
        wid_ary[i].widget_type = WIDGET_TYPE_LABEL;
        wid_ary[i].data_type = WIDGET_DATA_TYPE_INT;
        wid_ary[i].menu_prev = menu_prev;
        wid_ary[i].menu_next = 0;
        wid_ary[i].divider = 0;
        wid_ary[i].mask_value = 0;
        wid_ary[i].p_mask = NULL;
        wid_ary[i].p_source_mask = NULL;
        wid_ary[i].data = NULL;
        wid_ary[i].get_string = NULL;
        wid_ary[i].run_task = NULL;
        wid_ary[i].accept_input = 1;
    }
}

int menu_options_media_create(struct option_widget *wid_ary)
{
    int i;

    init_widget_array(wid_ary, KAT_MENU_OPTIONS_MISC);

    i = -1;

    /* Create Disk */
    wid_ary[++i].widget_type = WIDGET_TYPE_LABEL;
    strcpy(wid_ary[i].label, "Create Disk");
    wid_ary[i].menu_next = KAT_MENU_OPTIONS_MEDIA_DISK;

    /* Create Cassette */
    wid_ary[++i].widget_type = WIDGET_TYPE_LABEL;
    strcpy(wid_ary[i].label, "Create Cassette");
    wid_ary[i].menu_next = KAT_MENU_OPTIONS_MEDIA_CASSETTE;

    return i + 1;
}

int menu_options_media_disk(struct option_widget *wid_ary)
{
    int i;

    init_widget_array(wid_ary, KAT_MENU_OPTIONS_MEDIA_CREATE);

    i = -1;

    /* Sector Size / Boot Type */
    wid_ary[++i].widget_type = WIDGET_TYPE_SELECT;
    strcpy(wid_ary[i].label, "Sector Size / Boot Type");
    wid_ary[i].data = &disk_type_index;
    wid_ary[i].min = 0;
    wid_ary[i].max = NUM_DISK_TYPES - 1;
    wid_ary[i].get_string = get_disk_type_string;

    /* Sectors */
    wid_ary[++i].widget_type = WIDGET_TYPE_SELECT;
    strcpy(wid_ary[i].label, "Sectors");
    wid_ary[i].data = &disk_sectors_index;
    wid_ary[i].min = 0;
    wid_ary[i].max = NUM_DISK_SECTORS - 1;
    wid_ary[i].get_string = get_disk_sectors_string;

    /* Create button - divider before this item */
    wid_ary[++i].widget_type = WIDGET_TYPE_LABEL;
    strcpy(wid_ary[i].label, "Create");
    wid_ary[i].run_task = media_disk_create_task;
    wid_ary[i].divider = 1;

    return i + 1;
}

int menu_options_media_cassette(struct option_widget *wid_ary)
{
    int i;

    init_widget_array(wid_ary, KAT_MENU_OPTIONS_MEDIA_CREATE);

    i = -1;

    /* Description */
    wid_ary[++i].widget_type = WIDGET_TYPE_STRING;
    strcpy(wid_ary[i].label, "Description");
    wid_ary[i].data = cassette_description;
    wid_ary[i].data_type = WIDGET_DATA_TYPE_STRING;
    wid_ary[i].max = sizeof(cassette_description) - 1;

    /* Create button - divider before this item */
    wid_ary[++i].widget_type = WIDGET_TYPE_LABEL;
    strcpy(wid_ary[i].label, "Create");
    wid_ary[i].run_task = media_cassette_create_task;
    wid_ary[i].divider = 1;

    return i + 1;
}
