#include "ubinstall-gtk.h"

enum YON_PAGES yon_page_get_next(main_window *widgets, enum YON_PAGES page){
    if (!yon_page_save(widgets,page)){
        return -1;
    }
    switch (page){
        case YON_PAGE_WELCOME: return YON_PAGE_LICENCE; break;
        case YON_PAGE_LICENCE: return YON_PAGE_SECTIONS; break;
        case YON_PAGE_SECTIONS: return yon_sections_get_next_page(widgets); break;
        case YON_PAGE_OS_COMPONENTS: return main_config.configure_mode? YON_PAGE_KERNEL : YON_PAGE_INSTALLATION_BEGIN; break;
        case YON_PAGE_INSTALLATION_BEGIN: return YON_PAGE_KERNEL; break;
        case YON_PAGE_KERNEL: return YON_PAGE_KERNEL_ADDON; break;
        case YON_PAGE_KERNEL_ADDON: return YON_PAGE_PACMAN_SOFTWARE; break;
        case YON_PAGE_PACMAN_SOFTWARE: return YON_PAGE_REGION;
        case YON_PAGE_SOFTWARE: return YON_PAGE_PACMAN_SOFTWARE; break;
        case YON_PAGE_REGION: return YON_PAGE_KEYBOARD; break;
        case YON_PAGE_KEYBOARD: return YON_PAGE_USERS; break;
        case YON_PAGE_USERS: return YON_PAGE_STARTUP; break;
        case YON_PAGE_STARTUP: return YON_PAGE_BOOTLOADER; break;
        case YON_PAGE_BOOTLOADER: return YON_PAGE_NETWORK; break;
        case YON_PAGE_NETWORK: return main_config.configure_mode? YON_PAGE_CONFIGURE_END : YON_PAGE_INSTALLATION; break;
        case YON_PAGE_INSTALL_COMMON: return YON_PAGE_OS_COMPONENTS; break;
        case YON_PAGE_INSTALL_SEPARATE: return YON_PAGE_OS_COMPONENTS; break;
        case YON_PAGE_INSTALL_SAME_PARTITION: return YON_PAGE_OS_COMPONENTS; break;
        case YON_PAGE_INSTALL_ADVANCED: return YON_PAGE_OS_COMPONENTS; break;
        case YON_PAGE_INSTALL_RECOVERY: return yon_recovery_get_next(widgets); break;
        case YON_PAGE_RECOVERY_GRUB_INSTALL: return main_config.configure_mode?YON_PAGE_INSTALLATION:YON_PAGE_INSTALLATION_BEGIN; break;
        case YON_PAGE_RECOVERY_GRUB_UPDATE: return main_config.configure_mode?YON_PAGE_INSTALLATION:YON_PAGE_INSTALLATION_BEGIN; break;
        case YON_PAGE_RECOVERY_OS_ONLY: return main_config.configure_mode?YON_PAGE_INSTALLATION:YON_PAGE_INSTALLATION_BEGIN; break;
        case YON_PAGE_RECOVERY_USRDATA_ONLY: return main_config.configure_mode?YON_PAGE_INSTALLATION:YON_PAGE_INSTALLATION_BEGIN; break;
        default:return YON_PAGE_WELCOME;
    }   
    return YON_PAGE_WELCOME;
}

enum YON_PAGES yon_page_get_prev(enum YON_PAGES page){
    switch (page){
        case YON_PAGE_WELCOME: return YON_PAGE_WELCOME; break;
        case YON_PAGE_LICENCE: return YON_PAGE_WELCOME; break;
        case YON_PAGE_SECTIONS: return YON_PAGE_LICENCE; break;
        case YON_PAGE_OS_COMPONENTS: return YON_PAGE_SECTIONS; break;
        case YON_PAGE_INSTALLATION_BEGIN: return YON_PAGE_SECTIONS; break;
        case YON_PAGE_KERNEL: return YON_PAGE_SECTIONS; break;
        case YON_PAGE_KERNEL_ADDON: return YON_PAGE_KERNEL; break;
        case YON_PAGE_PACMAN_SOFTWARE: return YON_PAGE_KERNEL_ADDON; break;
        case YON_PAGE_SOFTWARE: return YON_PAGE_PACMAN_SOFTWARE; break;
        case YON_PAGE_REGION: return YON_PAGE_SOFTWARE; break;
        case YON_PAGE_KEYBOARD: return YON_PAGE_REGION; break;
        case YON_PAGE_USERS: return YON_PAGE_KEYBOARD; break;
        case YON_PAGE_STARTUP: return YON_PAGE_USERS; break;
        case YON_PAGE_BOOTLOADER: return YON_PAGE_STARTUP; break;
        case YON_PAGE_NETWORK: return YON_PAGE_BOOTLOADER; break;
        case YON_PAGE_INSTALLATION: return YON_PAGE_INSTALLATION; break;
        case YON_PAGE_CONFIGURE_END: return YON_PAGE_CONFIGURE_END; break;
        case YON_PAGE_INSTALL_COMMON: return YON_PAGE_SECTIONS; break;
        case YON_PAGE_INSTALL_SEPARATE: return YON_PAGE_SECTIONS; break;
        case YON_PAGE_INSTALL_SAME_PARTITION: return YON_PAGE_SECTIONS; break;
        case YON_PAGE_INSTALL_ADVANCED: return YON_PAGE_SECTIONS; break;
        case YON_PAGE_INSTALL_RECOVERY: return YON_PAGE_SECTIONS; break;
        case YON_PAGE_RECOVERY_GRUB_INSTALL: return YON_PAGE_INSTALL_RECOVERY; break;
        case YON_PAGE_RECOVERY_GRUB_UPDATE: return YON_PAGE_INSTALL_RECOVERY; break;
        case YON_PAGE_RECOVERY_OS_ONLY: return YON_PAGE_INSTALL_RECOVERY; break;
        case YON_PAGE_RECOVERY_USRDATA_ONLY: return YON_PAGE_INSTALL_RECOVERY; break;
        default:return YON_PAGE_WELCOME;
    }
    return YON_PAGE_WELCOME;
}

enum YON_PAGES yon_page_get_current(GtkNotebook *target){
    enum YON_PAGES page = gtk_notebook_get_current_page(target);
    return page;
}

void yon_navigation_buttons_set_sensetiveness(main_window *widgets){
    enum YON_PAGES page = yon_page_get_current(GTK_NOTEBOOK(widgets->Notebook));

    gtk_widget_hide(widgets->SourceButton);
    gtk_widget_hide(widgets->StartScenarioButton);
    switch(page){
        case YON_PAGE_WELCOME:
            yon_load_proceed(YON_CONFIG_LOCAL);
            gtk_widget_hide(widgets->BackButton);
            gtk_widget_set_sensitive(widgets->NextButton,1);
            gtk_widget_set_sensitive(widgets->CancelInstallButton,0);
            gtk_widget_hide(widgets->BackButton);
            gtk_widget_hide(widgets->SourceButton);
            gtk_widget_hide(widgets->SkipInstallationButton);
            gtk_widget_show(widgets->NextButton);
            if (!yon_char_is_empty(config(AUTOINSTALL_TYPE_INSTALL))){
                gtk_widget_show(widgets->StartScenarioButton);
            } else {
                gtk_widget_hide(widgets->StartScenarioButton);
            }
        break;
        case YON_PAGE_LICENCE:
            gtk_widget_show(widgets->BackButton);
            gtk_widget_set_sensitive(widgets->BackButton,1);
            gtk_widget_set_sensitive(widgets->NextButton,1);
            gtk_widget_set_sensitive(widgets->CancelInstallButton,1);
        break;
        case YON_PAGE_INSTALLATION:
            gtk_widget_set_sensitive(widgets->NextButton,0);
            gtk_widget_set_sensitive(widgets->BackButton,0);
            gtk_widget_hide(widgets->SkipInstallationButton);
        break;
        case YON_PAGE_KERNEL:
            gtk_widget_set_sensitive(widgets->BackButton,0);
        break;
        case YON_PAGE_CONFIGURE_END:
            gtk_button_set_label(GTK_BUTTON(widgets->NextButton),SAVE_AND_EXIT_LABEL);
            gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),EXIT_LABEL);

        break;
        case YON_PAGE_COMPLETED:
        case YON_PAGE_INSTALL_ERROR:
        case YON_PAGE_CONFIGURE_SAVE:
        case YON_PAGE_COMPLETION:
            gtk_button_set_label(GTK_BUTTON(widgets->NextButton),RESTART_LABEL);
            gtk_widget_set_sensitive(widgets->BackButton,0);
        break;
        case YON_PAGE_SECTIONS:
            gtk_widget_show(widgets->SourceButton);
        break;
        default:
            gtk_widget_set_sensitive(widgets->BackButton,1);
            gtk_widget_set_sensitive(widgets->NextButton,1);
            gtk_widget_set_sensitive(widgets->CancelInstallButton,1);
        break;
        
    }
    switch(page){
            case YON_PAGE_KERNEL:
            case YON_PAGE_KERNEL_ADDON:
            case YON_PAGE_PACMAN_SOFTWARE:
            case YON_PAGE_REGION:
            case YON_PAGE_KEYBOARD:
            case YON_PAGE_USERS:
            case YON_PAGE_STARTUP:
            case YON_PAGE_BOOTLOADER:
            case YON_PAGE_NETWORK:
                gtk_widget_show(widgets->SkipInstallationButton);
            break;
            default: 
                gtk_widget_hide(widgets->SkipInstallationButton);
        }
}

int yon_page_save(main_window *widgets, enum YON_PAGES page){
    switch (page){
        case YON_PAGE_OS_COMPONENTS: 
            return yon_os_components_save(widgets);
        break;
        case YON_PAGE_KERNEL: 
            return yon_kernel_save(widgets);
        break;
        case YON_PAGE_KERNEL_ADDON: 
            return yon_kernel_addon_save(widgets);
        break;
        case YON_PAGE_SOFTWARE: 
            return yon_software_save(widgets);
        break;
        case YON_PAGE_PACMAN_SOFTWARE: 
            return yon_pacman_software_save(widgets);
        break;
        case YON_PAGE_REGION: 
            return yon_region_save(widgets);
        break;
        case YON_PAGE_KEYBOARD: 
            return yon_keyboard_save(widgets);
        break;
        case YON_PAGE_USERS: 
            return yon_users_save(widgets);
        break;
        case YON_PAGE_STARTUP: 
            return yon_startup_save(widgets);
        break;
        case YON_PAGE_BOOTLOADER: 
            return yon_bootloader_save(widgets);
        break;
        case YON_PAGE_NETWORK: 
            return yon_network_save(widgets);
        break;
        case YON_PAGE_INSTALL_COMMON: 
            return yon_install_common_save(widgets);
        break;
        case YON_PAGE_INSTALL_SEPARATE: 
            return yon_install_separate_save(widgets);
        break;
        case YON_PAGE_INSTALL_SAME_PARTITION: 
            return yon_install_same_partition_save(widgets);
        break;
        case YON_PAGE_INSTALL_ADVANCED: 
            return yon_install_advanced_save(widgets);
        break;
        case YON_PAGE_RECOVERY_GRUB_INSTALL: 
            return yon_grub_install_save(widgets);
        break;
        case YON_PAGE_RECOVERY_GRUB_UPDATE: 
            return yon_grub_update_save(widgets);
        break;
        case YON_PAGE_RECOVERY_OS_ONLY: 
            return yon_install_options_save(widgets->OSDevicesTree,widgets->OSSysSectionTree,"system_only",widgets);
        break;
        case YON_PAGE_RECOVERY_USRDATA_ONLY: 
            return yon_install_options_save(widgets->UserdataDevicesTree,widgets->UserdataSysSectionTree,"data_only",widgets);
        break;
        case YON_PAGE_INSTALLATION_BEGIN:
            return yon_installation_start(widgets);
        break;
        case YON_PAGE_CONFIGURE_END:
            return yon_config_save(widgets);
        default:return 1;
    }
    return 1;
}

void yon_switch_page_render(main_window *widgets){
    enum YON_PAGES page = yon_page_get_current(GTK_NOTEBOOK(widgets->Notebook));
    switch(page){
        case YON_PAGE_WELCOME:
            page = 0;
            break;
        case YON_PAGE_LICENCE:
            page = 1;
            break;
        case YON_PAGE_SECTIONS:
        case YON_PAGE_OS_COMPONENTS:
        case YON_PAGE_INSTALL_COMMON:
        case YON_PAGE_INSTALL_SAME_PARTITION:
        case YON_PAGE_INSTALL_SEPARATE:
        case YON_PAGE_INSTALL_ADVANCED:
        case YON_PAGE_INSTALL_RECOVERY:
        case YON_PAGE_INSTALLATION_BEGIN:
        case YON_PAGE_KERNEL:
        case YON_PAGE_KERNEL_ADDON:
        case YON_PAGE_SOFTWARE:
        case YON_PAGE_PACMAN_SOFTWARE:
        case YON_PAGE_RECOVERY_GRUB_INSTALL:
        case YON_PAGE_RECOVERY_GRUB_UPDATE:
        case YON_PAGE_RECOVERY_OS_ONLY:
        case YON_PAGE_RECOVERY_USRDATA_ONLY:
            page = 2;
            break;
        case YON_PAGE_REGION:
            page = 3;
            break;
        case YON_PAGE_KEYBOARD:
            page = 4;
            break;
        case YON_PAGE_USERS:
            page = 5;
            break;
        case YON_PAGE_BOOTLOADER:
        case YON_PAGE_STARTUP:
        case YON_PAGE_NETWORK:
            page = 6;
            break;
        case YON_PAGE_INSTALLATION:
            page = 7;
            break;
        case YON_PAGE_COMPLETED:
        case YON_PAGE_COMPLETION:
        case YON_PAGE_INSTALL_ERROR:
        case YON_PAGE_CONFIGURE_END:
        case YON_PAGE_CONFIGURE_SAVE:
            page=8;
            break;
    }

    if (widgets){};
    GtkContainer *parent = GTK_CONTAINER(gtk_widget_get_parent(widgets->UsersToggle));
    GList *list = gtk_container_get_children(parent);
    for (guint i=0;i<g_list_length(list);i++){
        if (i!=(guint)page){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(g_list_nth_data(list,i)),0);
                } else { 
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(g_list_nth_data(list,i)),1);

        }
    }
    g_list_free(list);
}


void yon_configuration_mode_check(main_window *widgets){
    enum YON_PAGES page = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook)); 
    if (page==YON_PAGE_WELCOME){
        gtk_widget_set_sensitive(widgets->ConfigurationModeMenuItem,1);
    } else {
        gtk_widget_set_sensitive(widgets->ConfigurationModeMenuItem,0);
    }
}

void yon_page_update(main_window *widgets){
    yon_resize_images_update(widgets);
    yon_navigation_buttons_set_sensetiveness(widgets);
    yon_switch_page_render(widgets);
    yon_configuration_mode_check(widgets);
}

enum YON_PAGES yon_sections_get_next_page(main_window *widgets){
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->InstallationRadio)))
        return YON_PAGE_INSTALL_COMMON;
    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->InstallationNearRadio)))
        return YON_PAGE_INSTALL_SEPARATE;
    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->InstallationLinuxRadio)))
        return YON_PAGE_INSTALL_SAME_PARTITION;
    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->InstallationWindowsRadio)))
        return YON_PAGE_INSTALL_ADVANCED;
    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->InstallationOptionsRadio)))
        return YON_PAGE_INSTALL_RECOVERY;
    return YON_PAGE_SECTIONS;
}

enum YON_PAGES yon_recovery_get_next(main_window *widgets){
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->GrubInstallRadio)))
        return YON_PAGE_RECOVERY_GRUB_INSTALL;
    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->GrubUpdateRadio)))
        return YON_PAGE_RECOVERY_GRUB_UPDATE;
    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->OSRadio)))
        return YON_PAGE_RECOVERY_OS_ONLY;
    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->UserDataOnlyRadio)))
        return YON_PAGE_RECOVERY_USRDATA_ONLY;
    return YON_PAGE_SECTIONS;
}

void yon_page_init(main_window *widgets, enum YON_PAGES page){
    switch(page){
        case YON_PAGE_OS_COMPONENTS: 
            yon_os_components_init(widgets);
            break;
        case YON_PAGE_KERNEL: 
            yon_kernel_setup(widgets);
            break;
        case YON_PAGE_KERNEL_ADDON: 
            yon_kernel_addon_setup(widgets);
            break;
        case YON_PAGE_SOFTWARE: 
            yon_software_init(widgets);
            break;
        case YON_PAGE_PACMAN_SOFTWARE: 
            yon_pacman_init(widgets);
            break;
        case YON_PAGE_REGION: 
            yon_region_init(widgets);
            break;
        case YON_PAGE_KEYBOARD: 
            yon_keyboard_init(widgets);
            break;
        case YON_PAGE_USERS: 
            yon_user_init(widgets);
            break;
        case YON_PAGE_STARTUP: 
            yon_startup_services_setup(widgets);
            break;
        case YON_PAGE_BOOTLOADER: 
            yon_bootloader_init(widgets);
        break;
        case YON_PAGE_NETWORK: 
            yon_network_init(widgets);
        break;
        case YON_PAGE_INSTALL_COMMON: 
        case YON_PAGE_INSTALL_SEPARATE: 
        case YON_PAGE_INSTALL_SAME_PARTITION: 
        case YON_PAGE_INSTALL_ADVANCED: 
        case YON_PAGE_RECOVERY_GRUB_INSTALL: 
        case YON_PAGE_RECOVERY_GRUB_UPDATE: 
        case YON_PAGE_RECOVERY_OS_ONLY: 
        case YON_PAGE_RECOVERY_USRDATA_ONLY: 
        case YON_PAGE_INSTALLATION_BEGIN:
            yon_install_init(widgets,page);
        break;
        case YON_PAGE_INSTALLATION:
            main_config.save_configured=1;
            g_mutex_lock(&main_config.install_mutex);
            if (main_config.install_complete){
                g_mutex_unlock(&main_config.install_mutex);
                g_thread_new("install_thread",(GThreadFunc)on_setup_system_configuration,widgets);
                on_page_next_clicked(NULL,widgets);
            } else {
                g_mutex_unlock(&main_config.install_mutex);
            }
        break;
        default: break;
    }
}
 
void on_page_next_clicked(GtkWidget *, main_window *widgets){
    enum YON_PAGES page = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook));
    page = yon_page_get_next(widgets,page);
    if ((int)page!=-1){
        gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),page);
        yon_page_init(widgets,page);
    }
    yon_page_update(widgets);
}

void on_page_prev_clicked(GtkWidget *, main_window *widgets){
    enum YON_PAGES page = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook));
    page = yon_page_get_prev(page);
    if ((int)page!=-1){
        gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),page);
    }
    yon_page_update(widgets);
}

void on_page_cancel_clicked(GtkWidget *, main_window *widgets){
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_WELCOME);
    gtk_widget_hide(widgets->BackButton);
    gtk_widget_hide(widgets->SourceButton);
    gtk_widget_hide(widgets->SkipInstallationButton);
    gtk_widget_show(widgets->NextButton);
    gtk_widget_show(widgets->StartScenarioButton);
    gtk_widget_set_sensitive(widgets->CancelInstallButton,0);
    gtk_widget_set_sensitive(widgets->BackButton,1);
    gtk_widget_set_sensitive(widgets->NextButton,1);
    gtk_widget_set_sensitive(widgets->ConfigurationModeMenuItem,1);
        yon_page_update(widgets);

}

void on_system_setup_pass(GtkWidget *, main_window *widgets){
    if (main_config.configure_mode){
        gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_CONFIGURE_END);
    } else {
        gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_INSTALLATION);
    }
    yon_page_update(widgets);
}