#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: {
            char *install_mode = config(AUTOINSTALL_TYPE_INSTALL);
            if (main_config.configure_mode&&strcmp(install_mode,"system_only")&&strcmp(install_mode,"grub_install")&&strcmp(install_mode,"grub_update")){
                return YON_PAGE_CONFIG_HUB;
            } else if (strcmp(install_mode,"system_only")){
                return YON_PAGE_INSTALLATION_BEGIN;
            } else {
                return YON_PAGE_RECOVERY_BEGIN;
            }
        }
        case YON_PAGE_INSTALLATION_BEGIN: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_SOFTWARE: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_CONFIG_HUB: {
            char *install_mode = config(AUTOINSTALL_TYPE_INSTALL);
            if (main_config.configure_mode){
                return YON_PAGE_CONFIGURE_END;
            } else if (strcmp(install_mode,"data_only")){
                return YON_PAGE_INSTALLATION;
            } else {
                return YON_PAGE_RECOVERY_BEGIN;
            } 
        } break;
        case YON_PAGE_KERNEL: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_KERNEL_ADDON: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_PACMAN_SOFTWARE: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_REGION: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_LANGUAGE: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_KEYBOARD: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_USERS: return YON_PAGE_CONFIG_HUB; break; 
        case YON_PAGE_STARTUP: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_APP_STARTUP: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_BOOTLOADER: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_NETWORK: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_PACKAGES: return YON_PAGE_CONFIG_HUB;
        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 YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_RECOVERY_GRUB_UPDATE: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_RECOVERY_OS_ONLY: return YON_PAGE_OS_COMPONENTS; break;
        case YON_PAGE_RECOVERY_USRDATA_ONLY: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_RECOVERY_BEGIN: main_config.save_configured = 1; return YON_PAGE_RECOVERY_PROCESS; break;
        case YON_PAGE_RECOVERY_PROCESS: return YON_PAGE_RECOVERY_COMPLETION; break;
        case YON_PAGE_INSTALLATION: return YON_PAGE_COMPLETION; break;
        case YON_PAGE_CONFIGURE_END:
        return YON_PAGE_CONFIGURE_SAVE;
        break;
        case YON_PAGE_CONFIGURE_SAVE:
            gtk_main_quit();
            exit(0);
        break;
        case YON_PAGE_COMPLETED:
        case YON_PAGE_INSTALL_ERROR:
        case YON_PAGE_RECOVERY_COMPLETION:
        case YON_PAGE_COMPLETION:
            on_reboot_accepted(widgets);
            break;
        // default:return YON_PAGE_WELCOME;
    }   
    return -1;
}

enum YON_PAGES yon_page_get_prev(main_window *,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: {
            char *install_type = config(AUTOINSTALL_TYPE_INSTALL);
            if (!install_type){ install_type = "fast";}
            if (!strcmp(install_type,"fast")){
                return YON_PAGE_INSTALL_COMMON; break;
            } else if (!strcmp(install_type,"next")){
                return YON_PAGE_INSTALL_SEPARATE; break;
            } else if (!strcmp(install_type,"part")){
                return YON_PAGE_INSTALL_SAME_PARTITION; break;
            } else if (!strcmp(install_type,"custom")){
                return YON_PAGE_INSTALL_ADVANCED; break;
            } else if (!strcmp(install_type,"grub_install")){
                return YON_PAGE_RECOVERY_GRUB_INSTALL; break;
            } else if (!strcmp(install_type,"grub_update")){
                return YON_PAGE_RECOVERY_GRUB_UPDATE; break;
            } else if (!strcmp(install_type,"system_only")){
                return YON_PAGE_RECOVERY_OS_ONLY; break;
            } else if (!strcmp(install_type,"data_only")){
                return YON_PAGE_RECOVERY_USRDATA_ONLY; break;
            }
        } break;
        case YON_PAGE_RECOVERY_BEGIN:{
            char *install_type = config(AUTOINSTALL_TYPE_INSTALL);
            if (!strcmp(install_type,"grub_install")){
                return YON_PAGE_RECOVERY_GRUB_INSTALL; break;
            } else if (!strcmp(install_type,"grub_update")){
                return YON_PAGE_RECOVERY_GRUB_UPDATE; break;
            } else if (!strcmp(install_type,"system_only")){
                return YON_PAGE_RECOVERY_OS_ONLY; break;
            } else if (!strcmp(install_type,"data_only")){
                return YON_PAGE_RECOVERY_USRDATA_ONLY; break;
            }
        } break;
        case YON_PAGE_INSTALLATION_BEGIN: return YON_PAGE_OS_COMPONENTS; break;
        case YON_PAGE_SOFTWARE: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_KERNEL: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_KERNEL_ADDON: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_PACMAN_SOFTWARE: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_REGION: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_KEYBOARD: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_USERS: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_STARTUP: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_BOOTLOADER: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_NETWORK: return YON_PAGE_CONFIG_HUB; break;
        case YON_PAGE_PACKAGES: return YON_PAGE_CONFIG_HUB;
        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_set_sensitive(widgets->LoadLocalConfigurationMenuItem,1);
            gtk_widget_set_sensitive(widgets->LoadExternalConfigurationMenuItem,1);
            gtk_widget_hide(widgets->BackButton);
            gtk_widget_hide(widgets->SourceButton);
            gtk_widget_show(widgets->NextButton);
            gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),EXIT_LABEL);
        break;
        case YON_PAGE_LICENCE:
            gtk_widget_show(widgets->BackButton);
            gtk_widget_set_sensitive(widgets->BackButton,1);
            gtk_widget_set_sensitive(widgets->LoadLocalConfigurationMenuItem,1);
            gtk_widget_set_sensitive(widgets->LoadExternalConfigurationMenuItem,1);
            if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->LicenseAgreeRadio))){
                gtk_widget_set_sensitive(widgets->NextButton,1);
            } else {
                gtk_widget_set_sensitive(widgets->NextButton,0);
            }
            gtk_widget_set_sensitive(widgets->CancelInstallButton,1);
            gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),EXIT_LABEL);
        break;
        case YON_PAGE_INSTALLATION:
            gtk_widget_set_sensitive(widgets->NextButton,0);
            gtk_widget_set_sensitive(widgets->BackButton,0);
            gtk_widget_set_sensitive(widgets->CancelInstallButton,1);
            gtk_widget_set_sensitive(widgets->LoadLocalConfigurationMenuItem,0);
            gtk_widget_set_sensitive(widgets->LoadExternalConfigurationMenuItem,0);
        break;
        case YON_PAGE_RECOVERY_PROCESS:
            gtk_widget_set_sensitive(widgets->NextButton,0);
            gtk_widget_set_sensitive(widgets->BackButton,0);
            gtk_widget_set_sensitive(widgets->CancelInstallButton,1);
            break;
        case YON_PAGE_CONFIGURE_END:
            gtk_button_set_label(GTK_BUTTON(widgets->NextButton),SAVE_LABEL);
            gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),EXIT_LABEL);

        break;
        case YON_PAGE_COMPLETED:
        case YON_PAGE_RECOVERY_COMPLETION:
        case YON_PAGE_INSTALL_ERROR:
        case YON_PAGE_CONFIGURE_SAVE:
        case YON_PAGE_COMPLETION:
            gtk_widget_set_sensitive(widgets->LoadLocalConfigurationMenuItem,0);
            gtk_widget_set_sensitive(widgets->LoadExternalConfigurationMenuItem,0);
            gtk_button_set_label(GTK_BUTTON(widgets->NextButton),RESTART_LABEL);
            gtk_widget_hide(widgets->BackButton);
            gtk_widget_set_sensitive(widgets->CancelInstallButton,1);
            gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),EXIT_LABEL);
            gtk_widget_set_sensitive(widgets->NextButton,1);
        break;
        case YON_PAGE_SECTIONS:
            gtk_widget_show(widgets->SourceButton);
            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);
            gtk_widget_set_sensitive(widgets->LoadLocalConfigurationMenuItem,1);
            gtk_widget_set_sensitive(widgets->LoadExternalConfigurationMenuItem,1);
            gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),EXIT_LABEL);
            if (!yon_char_is_empty(config(AUTOINSTALL_TYPE_INSTALL))&&!main_config.configure_mode){
                gtk_widget_show(widgets->StartScenarioButton);
            } else {
                gtk_widget_hide(widgets->StartScenarioButton);
            }
        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_INSTALL_RECOVERY:
        case YON_PAGE_RECOVERY_GRUB_INSTALL:
        case YON_PAGE_RECOVERY_GRUB_UPDATE:
        case YON_PAGE_RECOVERY_OS_ONLY:
        case YON_PAGE_RECOVERY_USRDATA_ONLY:
        {
            gtk_widget_set_sensitive(widgets->LoadLocalConfigurationMenuItem,0);
            gtk_widget_set_sensitive(widgets->LoadExternalConfigurationMenuItem,0);
            gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),CANCEL_LABEL);
        } break;
        default:
            gtk_widget_set_sensitive(widgets->BackButton,1);
            gtk_widget_set_sensitive(widgets->NextButton,1);
            gtk_widget_set_sensitive(widgets->CancelInstallButton,1);
            gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),CANCEL_LABEL);
        break;
        
    }
    switch(page){
            case YON_PAGE_KERNEL:
            case YON_PAGE_SOFTWARE:
            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_APP_STARTUP:
            case YON_PAGE_LANGUAGE:
            case YON_PAGE_BOOTLOADER:
            case YON_PAGE_PACKAGES:
            case YON_PAGE_NETWORK:{
                gtk_widget_set_sensitive(widgets->LoadLocalConfigurationMenuItem,0);
                gtk_widget_set_sensitive(widgets->LoadExternalConfigurationMenuItem,0);
                gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),CANCEL_LABEL);
                gtk_widget_show(widgets->NextButton);
                gtk_widget_hide(widgets->BackButton);
                gtk_button_set_label(GTK_BUTTON(widgets->NextButton),BACK_LABEL);
                GtkWidget *image = gtk_button_get_image(GTK_BUTTON(widgets->NextButton));
                gtk_image_set_from_icon_name(GTK_IMAGE(image),arrow_left_icon_path,GTK_ICON_SIZE_BUTTON);
                } break;
            case YON_PAGE_CONFIG_HUB:{
                gtk_widget_set_sensitive(widgets->LoadLocalConfigurationMenuItem,0);
                gtk_widget_set_sensitive(widgets->LoadExternalConfigurationMenuItem,0);
                gtk_widget_show(widgets->NextButton);
                gtk_widget_hide(widgets->BackButton);
                gtk_button_set_label(GTK_BUTTON(widgets->NextButton),NEXT_LABEL);
                GtkWidget *image = gtk_button_get_image(GTK_BUTTON(widgets->NextButton));
                gtk_image_set_from_icon_name(GTK_IMAGE(image),arrow_right_icon_path,GTK_ICON_SIZE_BUTTON);
                gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),CANCEL_LABEL);
                } break;
                default: break;
        }
}

int yon_page_save(main_window *widgets, enum YON_PAGES page){
    switch (page){
        case YON_PAGE_CONFIG_HUB:
            return yon_configuration_hub_save(widgets);
        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_LANGUAGE: 
            return yon_language_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_APP_STARTUP: 
            return yon_startup_app_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_PACKAGES:
            return yon_packages_tab_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_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:
        yon_installation_start(widgets);
        // [[fallthrough]];
        break;
        case YON_PAGE_CONFIGURE_END:
            int status = yon_config_save(widgets);
            if (status){
                const char *endaction = gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->ConfigHubRebootCombo));
                if (!yon_char_is_empty(endaction)&&!strcmp(endaction,"close")){
                    on_exit_accepted(widgets);
                }
            }
            return status;
        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_RECOVERY_GRUB_INSTALL:
        case YON_PAGE_RECOVERY_GRUB_UPDATE:
        case YON_PAGE_RECOVERY_OS_ONLY:
        case YON_PAGE_RECOVERY_USRDATA_ONLY:
        case YON_PAGE_RECOVERY_BEGIN:
            page = 2;
            break;
        case YON_PAGE_CONFIG_HUB:
        case YON_PAGE_KERNEL:
        case YON_PAGE_KERNEL_ADDON:
        case YON_PAGE_SOFTWARE:
        case YON_PAGE_PACMAN_SOFTWARE:
        case YON_PAGE_REGION:
        case YON_PAGE_LANGUAGE:
        case YON_PAGE_KEYBOARD:
        case YON_PAGE_USERS:
        case YON_PAGE_BOOTLOADER:
        case YON_PAGE_STARTUP:
        case YON_PAGE_APP_STARTUP:
        case YON_PAGE_NETWORK:
        case YON_PAGE_PACKAGES:
            page = 3;
            break;
        case YON_PAGE_INSTALLATION:
        case YON_PAGE_RECOVERY_PROCESS:
            page = 4;
            break;
        case YON_PAGE_RECOVERY_COMPLETION:
        case YON_PAGE_COMPLETED:
        case YON_PAGE_COMPLETION:
        case YON_PAGE_INSTALL_ERROR:
        case YON_PAGE_CONFIGURE_END:
        case YON_PAGE_CONFIGURE_SAVE:
            page=5;
            break;
    }

    if (widgets){};
    GtkContainer *parent = GTK_CONTAINER(gtk_widget_get_parent(widgets->WelcomeToggle));
    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_container_start_loading(GtkWidget *target){
    GtkWidget *parent = gtk_widget_get_parent(target);
    g_object_ref(target);
    gtk_container_remove(GTK_CONTAINER(parent),target);
    GtkWidget *overlay = gtk_overlay_new();
    g_object_set_data(G_OBJECT(parent),"loader_overlay",overlay);
    g_object_set_data(G_OBJECT(target),"parent",parent);
    gtk_overlay_add_overlay(GTK_OVERLAY(overlay),target);
    gtk_widget_show(overlay);
}

void yon_container_stop_loading(GtkWidget *target){
    g_object_ref(target);
    GtkWidget *parent = g_object_get_data(G_OBJECT(target),"loader_overlay");
    GtkWidget *overlay = g_object_get_data(G_OBJECT(parent),"loader_overlay");
    gtk_container_remove(GTK_CONTAINER(overlay),target);
    gtk_container_add(GTK_CONTAINER(parent),target);
    gtk_widget_destroy(overlay);
    gtk_widget_show(overlay);
    g_object_set_data(G_OBJECT(parent),"loader_overlay",NULL);
    g_object_set_data(G_OBJECT(target),"parent",NULL);
}

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&&!main_config.force_ini&&!getuid()){
        gtk_widget_set_sensitive(widgets->ConfigurationModeMenuItem,1);
    } else {
        gtk_widget_set_sensitive(widgets->ConfigurationModeMenuItem,0);
    }
}

void yon_page_update(main_window *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_WELCOME:
            yon_set_locales_list(widgets);
        break;
        case YON_PAGE_CONFIG_HUB:
            yon_configuration_hub_init(widgets);
            break;
        case YON_PAGE_OS_COMPONENTS: 
            // on_status_update(NULL,widgets);
            yon_os_components_init(widgets);
            break;
        case YON_PAGE_KERNEL: 
            // on_status_update(NULL,widgets);
            yon_kernel_init(widgets);
            break;
        case YON_PAGE_KERNEL_ADDON: 
            // on_status_update(NULL,widgets);
            yon_kernel_addon_init(widgets);
            break;
        case YON_PAGE_SOFTWARE: 
            // on_status_update(NULL,widgets);
            yon_software_init(widgets);
            break;
        case YON_PAGE_PACMAN_SOFTWARE: 
            // on_status_update(NULL,widgets);
            yon_pacman_init(widgets);
            break;
        case YON_PAGE_REGION: 
            yon_region_init(widgets);
            break;
        case YON_PAGE_LANGUAGE: 
            yon_language_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_init(widgets);
            break;
        case YON_PAGE_APP_STARTUP: 
            yon_startup_apps_init(widgets);
            break;
        case YON_PAGE_BOOTLOADER: 
            yon_bootloader_init(widgets);
        break;
        case YON_PAGE_NETWORK: 
            yon_network_init(widgets);
        break;
        case YON_PAGE_PACKAGES: 
            // on_status_update(NULL,widgets);
            yon_packages_tab_init(widgets);
        break;
        case YON_PAGE_INSTALL_ADVANCED:
            yon_advanced_init(widgets);
        break;
        case YON_PAGE_COMPLETION:
            const char *endaction = gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->ConfigHubRebootCombo));
            if (!yon_char_is_empty(endaction)&&!strcmp(endaction,"close")){
                on_yon_exit(NULL,NULL,widgets);
            }
            break;
        case YON_PAGE_RECOVERY_PROCESS:{
            yon_installation_start(widgets);
        } break;
        case YON_PAGE_INSTALL_COMMON: 
        case YON_PAGE_INSTALL_SEPARATE: 
        case YON_PAGE_INSTALL_SAME_PARTITION: 
        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);
            {
                int size=0;
                char *command = yon_config_parameter_prepare_command(AUTOINSTALL_TYPE_INSTALL_command,"system",NULL,NULL);
                config_str autoinstall_param = yon_config_load(command,&size);
                if (size&&!yon_char_is_empty(autoinstall_param[0])&&strcmp(autoinstall_param[0],"(null)\n")&&!main_config.configure_mode){
                    gtk_widget_show(widgets->StartScenarioButton);
                } else {
                    gtk_widget_hide(widgets->StartScenarioButton);
                }
                yon_char_parsed_free(autoinstall_param,size);
            }
        break;
        case YON_PAGE_INSTALLATION:
            yon_install_slider_init(widgets);
            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);
            } 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);
        switch(page){
            case YON_PAGE_KERNEL:
            case YON_PAGE_KERNEL_ADDON:
            case YON_PAGE_PACMAN_SOFTWARE:
            case YON_PAGE_SOFTWARE:
            case YON_PAGE_PACKAGES:
                on_status_button_update(NULL,widgets);
            break;
            default: break;
        };
    }
    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(widgets,page);
    if ((int)page!=-1){
        gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),page);
    }
    yon_page_update(widgets); 
}

// #include <signal.h>

void on_page_cancel_clicked(GtkWidget *, main_window *widgets){
    enum YON_PAGES cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook));
    switch (cur_page){
        case YON_PAGE_CONFIGURE_END:
        case YON_PAGE_COMPLETED:
        case YON_PAGE_RECOVERY_COMPLETION:
        case YON_PAGE_INSTALL_ERROR:
        case YON_PAGE_CONFIGURE_SAVE:
        case YON_PAGE_COMPLETION:
            yon_config_restore(widgets);

            gtk_main_quit();
            break;
        case YON_PAGE_SECTIONS:
        case YON_PAGE_WELCOME:
        case YON_PAGE_LICENCE:
            on_yon_exit(NULL,NULL,widgets);
            break;
        default: {
            if (main_config.install_thread){
                dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
                data->action_text=INTERRUPT_TEXT_LABEL;
                data->title=WARNING_TITLE_LABEL;
                if (yon_confirmation_dialog_call(widgets->MainWindow,data)==GTK_RESPONSE_ACCEPT ){
                    pthread_cancel(main_config.install_thread);
                    kill(main_config.install_thread,SIGKILL);
                    if (main_config.autoinstall) exit(0);
                    g_signal_handlers_disconnect_by_func(G_OBJECT(widgets->install_info_monitor),yon_installation_progress_update,widgets);
                    g_file_monitor_cancel(widgets->install_info_monitor);
                    widgets->install_info_monitor = NULL;
                    gtk_button_set_label(GTK_BUTTON(widgets->NextButton),NEXT_LABEL);
                    GtkWidget *image = gtk_button_get_image(GTK_BUTTON(widgets->NextButton));
                    gtk_image_set_from_icon_name(GTK_IMAGE(image),arrow_right_icon_path,GTK_ICON_SIZE_BUTTON);
                    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->LicenseAgreeRadio))){
                        gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_SECTIONS);
                    } else {
                        gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_WELCOME);
                    }
                    yon_page_update(widgets);
                    main_config.save_done=0;
                    main_config.install_complete=0;
                    main_config.save_configured=0;
                    main_config.install_complete=0;
                    main_config.install_thread=0;
                }
            } else {
                gtk_button_set_label(GTK_BUTTON(widgets->NextButton),NEXT_LABEL);
                GtkWidget *image = gtk_button_get_image(GTK_BUTTON(widgets->NextButton));
                gtk_image_set_from_icon_name(GTK_IMAGE(image),arrow_right_icon_path,GTK_ICON_SIZE_BUTTON);
                if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->LicenseAgreeRadio))){
                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_SECTIONS);
                } else {
                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_WELCOME);
                }
                yon_page_update(widgets);
                main_config.save_done=0;
                main_config.install_complete=0;
                main_config.save_configured=0;
                main_config.install_complete=0;
                main_config.install_thread=0;
            }
        }
    }
}

void on_additional_page_clicked(GtkWidget *, main_window *widgets){
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_SOFTWARE);
    yon_page_init(widgets,YON_PAGE_SOFTWARE);
    yon_page_update(widgets);
}

void on_kernel_components_page_clicked(GtkWidget *, main_window *widgets){
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_KERNEL_ADDON);
    yon_page_init(widgets,YON_PAGE_KERNEL_ADDON);
    yon_page_update(widgets);
}

void on_kernel_page_clicked(GtkWidget *, main_window *widgets){
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_KERNEL);
    yon_page_init(widgets,YON_PAGE_KERNEL);
    yon_page_update(widgets);
}

void on_additional_packages_page_clicked(GtkWidget *, main_window *widgets){
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_PACMAN_SOFTWARE);
    yon_page_init(widgets,YON_PAGE_PACMAN_SOFTWARE);
    yon_page_update(widgets);
}
