#include "ubinstall-gtk.h"

config_str yon_os_components_get_modules(int *size){
    (*size) = 0;
    int modules_size = main_config.modules_size;
    config_str modules_list = main_config.modules;
    config_str module_names = NULL;
    for (int i=1;i<modules_size;i++){
        int parsed_size;
        config_str parsed = yon_char_parse(modules_list[i],&parsed_size,";");
        yon_char_parsed_add_or_create_if_exists(module_names,size,parsed[1]);
        yon_char_parsed_free(parsed,parsed_size);
    }
    return module_names;
}

int yon_installation_check_packages_size(main_window *widgets){
    char *packages = config(packages_parameter);
    char *modules = config(modules_parameter);
    char *install_mode = config(AUTOINSTALL_TYPE_INSTALL);
    long part_size = 0;
    if (yon_char_is_empty(modules)||!strcmp(modules,"auto")){
        int modules_size;
        config_str module_names = yon_os_components_get_modules(&modules_size);
        modules = yon_char_parsed_to_string(module_names,modules_size,",");
    }
    if (yon_char_is_empty(install_mode)){
        return 0;
    }
    if (!strcmp(install_mode,"fast")){
        char *device = config(device_parameter);
        GtkTreeIter iter;
        for_iter(widgets->DevicesList,&iter){
            char *target;
            long disk_size;
            gtk_tree_model_get(GTK_TREE_MODEL(widgets->DevicesList),&iter,0,&target,7,&disk_size,-1);
            if (!strcmp(device,target)){
                part_size = disk_size;
            }
        }
    } else if (!strcmp(install_mode,"advanced")) {
        char *part = config(part_parameter);
        part = yon_char_new(part);
        char *sys_part = yon_char_divide_search(part,",",-1);
        free(part);
        part = sys_part;
        for (int i=0;i<main_config.part_size;i++){
            int parsed_size;
            config_str parsed = yon_char_parse(main_config.partitions[i],&parsed_size,";");
            if (parsed&&parsed_size>2&&!strcmp(parsed[2],part)&&parsed_size>3&&!yon_char_is_empty(parsed[3])){
                part_size = atol(parsed[3]);
                
                yon_char_parsed_free(parsed,parsed_size);
                break;
            }
            yon_char_parsed_free(parsed,parsed_size);
        }

    } else {
        char *part = config(part_parameter);
        if (!yon_char_is_empty(part)){
            for (int i=0;i<main_config.part_size;i++){
                int parsed_size=0;
                config_str parsed = yon_char_parse(main_config.partitions[i],&parsed_size,";");
                if (parsed&&parsed_size>2&&!strcmp(parsed[2],part)&&parsed_size>3&&!yon_char_is_empty(parsed[3])){
                    part_size = atol(parsed[3]);
                    
                    yon_char_parsed_free(parsed,parsed_size);
                    break;
                }
                yon_char_parsed_free(parsed,parsed_size);
            }
        }
    }
    long packages_size = 0;
    long modules_size = 0;

    if (!yon_char_is_empty(packages)){
        int parsed_size;
        config_str parsed = yon_char_parse(packages,&parsed_size,",");
        for (int i=0;i<parsed_size;i++){
            packages_size += yon_packages_get_installed_size(YON_PACKAGES_ALL,parsed[i]);
        }
        yon_char_parsed_free(parsed,parsed_size);
    }
    if (!yon_char_is_empty(modules)){
        int parsed_size;
        config_str parsed = yon_char_parse(modules,&parsed_size,",");
        for (int i=0;i<parsed_size;i++){
            modules_size += yon_packages_get_installed_size(YON_PACKAGES_ALL,parsed[i]);
        }
        yon_char_parsed_free(parsed,parsed_size);
    }
    long overall_size = packages_size + modules_size;
    if (overall_size>part_size){
        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),PACKAGE_NOT_ENOUGH_SPACE_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        return 0;
    }
    return 1;
}

int yon_configuration_hub_save(main_window *widgets){
    if (!yon_installation_check_packages_size(widgets)) return 0;
    char *finish_button = (char *)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->ConfigHubRebootCombo));
    if (!yon_char_is_empty(finish_button)){
        yon_config_register(finish_parameter,finish_parameter_command,finish_button);
    } else {
        yon_config_remove_by_key(finish_parameter);
    }
    if (!main_config.configure_mode){
        int size;
        config_str parameters = yon_config_get_save_parameters_by_key(&size,
            finish_parameter,
            NULL);
        if (parameters){
            char *command_parameters = yon_char_parsed_to_string(parameters,size," -- ");
            char *command = yon_char_unite(ubconfig_dull_command,"--target system ",command_parameters,NULL);
            if (!system(yon_debug_output("%s\n",command))){}
            free(command);
            yon_char_parsed_free(parameters,size);
        }
        
        int users_size, save_size;
        config_str users = yon_config_find_keys(NETWORK_parameter_search,&users_size);
        parameters = yon_config_get_save_parameters_by_list(&save_size,users,users_size);
        if (parameters){
            char *command_parameters = yon_char_parsed_to_string(parameters,save_size," -- ");
            char *command = yon_char_unite(ubconfig_dull_command,"--target system ",command_parameters,NULL);
            if (!system(yon_debug_output("%s\n",command))){}
            free(command);
            yon_char_parsed_free(parameters,save_size);
        }
        if (users)yon_char_parsed_free(users,users_size);

        users = yon_config_find_keys(USERADD_parameter_search,&users_size);
        parameters = yon_config_get_save_parameters_by_list(&save_size,users,users_size);
        if (parameters){
            char *command_parameters = yon_char_parsed_to_string(parameters,size," -- ");
            char *command = yon_char_unite(ubconfig_dull_command,"--target system ",command_parameters,NULL);
            if (!system(yon_debug_output("%s\n",command))){}
            free(command);
            yon_char_parsed_free(parameters,save_size);
        }
        if (users)yon_char_parsed_free(users,users_size);
    }
    yon_debug_output("%s\n",yon_config_get_all_info());
    return 1;
}

void on_configuration_hub_selected(GtkWidget* self, main_window *widgets){
    GList *list = NULL;
    list = g_list_prepend(list,widgets->HubPackagesListBox);
    list = g_list_prepend(list,widgets->HubSystemListBox);
    list = g_list_prepend(list,widgets->HubPersonalListBox);
    list = g_list_reverse(list);
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        if (iter->data!=self){
            g_signal_handlers_block_by_func(G_OBJECT(iter->data),G_CALLBACK(on_configuration_hub_selected),widgets);
            GList *flowlist = gtk_flow_box_get_selected_children(GTK_FLOW_BOX(iter->data));
            if (flowlist){
                gtk_flow_box_unselect_child(GTK_FLOW_BOX(iter->data),GTK_FLOW_BOX_CHILD(flowlist->data));
                g_list_free(flowlist);
            }
            g_signal_handlers_unblock_by_func(G_OBJECT(iter->data),G_CALLBACK(on_configuration_hub_selected),widgets);
        }
    }
}

void yon_configuration_hub_section_get_max_size(GtkFlowBox *target, int *ret_width, int *ret_height){
    GList *list = gtk_container_get_children(GTK_CONTAINER(target));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        config_hub_icon *section = g_object_get_data(G_OBJECT(iter->data),"config_hub_icon");
        gtk_widget_realize(GTK_WIDGET(iter->data));
        gtk_widget_realize(GTK_WIDGET(section->Label));
        int width;
        int height;

        gtk_widget_get_preferred_width(section->Label,NULL,&width);
        gtk_widget_get_preferred_height(section->Label,NULL,&height);
        if ((*ret_width)<width) (*ret_width) = width;
        if ((*ret_height)<height) (*ret_height) = height;
    }
}

void yon_configuration_hub_section_set_max_size(GtkFlowBox *target, int width, int height){
    GList *list = gtk_container_get_children(GTK_CONTAINER(target));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        config_hub_icon *section = g_object_get_data(G_OBJECT(iter->data),"config_hub_icon");
        gtk_widget_set_size_request(section->Label,width,height);

    }
}

void yon_configuration_hub_resize(main_window *widgets){
    int max_width=0;
    int max_height=0;
    yon_configuration_hub_section_get_max_size(GTK_FLOW_BOX(widgets->HubPersonalListBox),&max_width,&max_height);
    yon_configuration_hub_section_get_max_size(GTK_FLOW_BOX(widgets->HubSystemListBox),&max_width,&max_height);
    yon_configuration_hub_section_get_max_size(GTK_FLOW_BOX(widgets->HubPackagesListBox),&max_width,&max_height);
    yon_configuration_hub_section_set_max_size(GTK_FLOW_BOX(widgets->HubPersonalListBox),max_width,max_height);
    yon_configuration_hub_section_set_max_size(GTK_FLOW_BOX(widgets->HubSystemListBox),max_width,max_height);
    yon_configuration_hub_section_set_max_size(GTK_FLOW_BOX(widgets->HubPackagesListBox),max_width,max_height);
}

void on_configuration_hub_open(GtkFlowBox *,GtkFlowBoxChild *child, main_window *widgets){
    config_hub_icon *cur_icon = g_object_get_data(G_OBJECT(child),"config_hub_icon");
    
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),cur_icon->page);
    yon_page_init(widgets,cur_icon->page);
    yon_page_update(widgets);
    switch(cur_icon->page){
        case YON_PAGE_OS_COMPONENTS:
        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;
    };
}

void yon_configuration_hub_add(GtkFlowBox *target, char *name, char *icon, enum YON_PAGES page){
    g_return_if_fail(GTK_IS_FLOW_BOX(target));
    config_hub_icon *cur_icon = malloc(sizeof(config_hub_icon));
    memset(cur_icon,0,sizeof(config_hub_icon));

    cur_icon->MainBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
    cur_icon->Label = gtk_label_new(name);
    cur_icon->Image = gtk_image_new_from_icon_name(icon,GTK_ICON_SIZE_BUTTON);
    gtk_image_set_pixel_size(GTK_IMAGE(cur_icon->Image),54);
    cur_icon->page = page;
    GtkWidget *flow = gtk_flow_box_child_new();
    gtk_style_context_add_class(gtk_widget_get_style_context(flow),"bggrey");
    gtk_widget_set_margin_end(cur_icon->Label,5);
    gtk_label_set_line_wrap(GTK_LABEL(cur_icon->Label),1);
    gtk_label_set_max_width_chars(GTK_LABEL(cur_icon->Label),15);
    gtk_label_set_xalign(GTK_LABEL(cur_icon->Label), GTK_ALIGN_FILL);
    gtk_label_set_line_wrap_mode(GTK_LABEL(cur_icon->Label),PANGO_WRAP_WORD);
    
    g_object_set_data(G_OBJECT(flow),"config_hub_icon",cur_icon);
    gtk_container_add(GTK_CONTAINER(flow),cur_icon->MainBox);
    gtk_widget_set_hexpand(flow,0);
    gtk_box_pack_start(GTK_BOX(cur_icon->MainBox),cur_icon->Image,0,0,0);
    gtk_box_pack_start(GTK_BOX(cur_icon->MainBox),cur_icon->Label,1,1,0);
    gtk_flow_box_insert(target,flow,-1);
    gtk_widget_show_all(flow);
}

void yon_flow_box_clear(GtkFlowBox *target){
    GList *list = gtk_container_get_children(GTK_CONTAINER(target));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        gtk_widget_destroy(GTK_WIDGET(iter->data));
    }
}

void yon_configuration_hub_init(main_window *widgets){
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->ConfigHubRebootCombo));
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->ConfigHubRebootCombo),NULL,DEFAULT_LABEL);
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->ConfigHubRebootCombo),"close",CLOSE_INSTALLER_LABEL);
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->ConfigHubRebootCombo),"reboot",REBOOT_LABEL);
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->ConfigHubRebootCombo),"shutdown",SHUTDOWN_LABEL);
    char *finish_string = config(finish_parameter);
    if (!yon_char_is_empty(finish_string)){
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->ConfigHubRebootCombo),finish_string);

    } else {
        gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->ConfigHubRebootCombo),0);
    }
    yon_flow_box_clear(GTK_FLOW_BOX(widgets->HubPersonalListBox));
    yon_flow_box_clear(GTK_FLOW_BOX(widgets->HubSystemListBox));
    yon_flow_box_clear(GTK_FLOW_BOX(widgets->HubPackagesListBox));
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubPersonalListBox),REGION_ICON_LABEL,"com.ublinux.ubinstall-system",YON_PAGE_REGION);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubPersonalListBox),LOCALE_LANGUAGE_LABEL,"com.ublinux.ubinstall-system",YON_PAGE_LANGUAGE);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubPersonalListBox),KEYBOARD_TAB_LABEL,"com.ublinux.ubinstall-keyboard",YON_PAGE_KEYBOARD);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubSystemListBox),USERS_TAB_LABEL,"com.ublinux.ubinstall-usergroups",YON_PAGE_USERS);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubSystemListBox),STARTUP_LABEL,"com.ublinux.ubinstall-info",YON_PAGE_STARTUP);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubSystemListBox),STARTUP_APP_LABEL,"com.ublinux.ubinstall-info",YON_PAGE_APP_STARTUP);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubSystemListBox),BOOTLOADER_LABEL,"com.ublinux.ubinstall-bootloader",YON_PAGE_BOOTLOADER);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubSystemListBox),NETWORK_LABEL,"com.ublinux.ubinstall-multiseat",YON_PAGE_NETWORK);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubPackagesListBox),KERNEL_LABEL,kernel_icon_path,YON_PAGE_KERNEL);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubPackagesListBox),KERNEL_ADDON_LABEL,kernel_icon_path,YON_PAGE_KERNEL_ADDON);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubPackagesListBox),PACKAGES_SECTION_LABEL,"com.ublinux.ubinstall-repomanager",YON_PAGE_PACKAGES);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubPackagesListBox),SOFTWARE_LABEL,"com.ublinux.ubinstall-update",YON_PAGE_SOFTWARE);
    yon_configuration_hub_add(GTK_FLOW_BOX(widgets->HubPackagesListBox),PACMAN_SOFTWARE_LABEL,"com.ublinux.ubinstall-repomanager",YON_PAGE_PACMAN_SOFTWARE);
    yon_configuration_hub_resize(widgets);
}