#include "ubinstall-gtk.h"

int yon_install_common_save(main_window *widgets){
    GtkTreeModel *model;
    GtkTreeIter iter;
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->CommonInstallationDevicesTree)),&model,&iter)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NO_DEVICE_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->CommonInstallationDevicesTree));
        return 0;
    }
    char *device;
    yon_config_remove_by_key(part_size_parameter);
    yon_config_remove_by_key(part_parameter);
    gtk_tree_model_get(model,&iter,0,&device,-1);
    yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_TYPE_INSTALL_command,"fast");
    yon_config_register(device_parameter,device_parameter_command,device);

    char *device_name = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->CommonInstallationSectionNameEntry));
    if (!yon_char_is_empty(device_name)){
        yon_config_register(part_label_parameter,part_label_parameter_command,device_name);
    } else {
        yon_config_remove_by_key(part_label_parameter);
    }

    char *file_system_type = (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widgets->CommonInstallationFilesystemTypeCombo));
    if (!yon_char_is_empty(file_system_type)){
        yon_config_register(part_fs_type_parameter,part_fs_type_parameter_command,file_system_type);
    } else {
        yon_config_remove_by_key(part_fs_type_parameter);
    }

    if (!main_config.configure_mode){
        yon_debug_output("%s\n",yon_config_save_args_simple(main_config.config_load_path,AUTOINSTALL_TYPE_INSTALL,device_parameter,part_parameter,part_size_parameter,part_label_parameter,part_fs_type_parameter,NULL));
    }
    return 1;
}

int yon_install_separate_save(main_window *widgets){
    GtkTreeModel *model;
    GtkTreeIter iter;
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->NextInstallationSysDevicesTree)),&model,&iter)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NO_DEVICE_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->CommonInstallationDevicesTree));
        return 0;
    }
    char *device;
    gtk_tree_model_get(model,&iter,0,&device,-1);
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->NextInstallationSysSectionTree)),&model,&iter)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NO_PART_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->CommonInstallationDevicesTree));
        return 0;
    }
    
    char *part;
    glong size,free_space;
    gtk_tree_model_get(model,&iter,0,&part,5,&size,6,&free_space,-1);
    yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_TYPE_INSTALL_command,"next");
    yon_config_register(device_parameter,device_parameter_command,device);
    yon_config_register(part_parameter,part_parameter_command,part);
    if (gtk_switch_get_active(GTK_SWITCH(widgets->NextInstallationFormatSwitch))){
        yon_config_register(part_format_parameter,part_format_parameter_command,"yes");
        double part_size = gtk_spin_button_get_value(GTK_SPIN_BUTTON(widgets->NextInstallationSizeSpin));
        int mod = yon_get_size_get_from_letter(gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->NextInstallationSizeTypeSpin))[0]);
        if (yon_size_to_bytes(part_size,mod+1)>size){
            yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NOT_ENOUGH_SPACE_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->NextInstallationSizeSpin));
            return 0;
        } else if (yon_size_to_bytes(part_size,mod+1)>free_space){
            yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NOT_ENOUGH_FREE_SPACE_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->NextInstallationSizeSpin));
            return 0;
        }
        if (part_size){
            char *size_letter = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->NextInstallationSizeTypeSpin));
            char *size_final = yon_char_append(yon_char_from_long((long)part_size),size_letter);
            yon_config_register(part_size_parameter,part_size_parameter_command,size_final);
        } else {
            yon_config_remove_by_key(part_size_parameter);
        }

            char *device_name = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->NextInstallationSectionNameEntry));
        if (!yon_char_is_empty(device_name)){
            yon_config_register(part_label_parameter,part_label_parameter_command,device_name);
        } else {
            yon_config_remove_by_key(part_label_parameter);

        }

        char *file_system_type = (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widgets->NextInstallationFilesystemTypeCombo));
        if (!yon_char_is_empty(file_system_type)){
            yon_config_register(part_fs_type_parameter,part_fs_type_parameter_command,file_system_type);

        } else {
            yon_config_remove_by_key(part_fs_type_parameter);

        }
    } else {
            yon_config_register(part_format_parameter,part_format_parameter_command,"no");
            yon_config_remove_by_key(part_label_parameter);
            yon_config_remove_by_key(part_fs_type_parameter);
            yon_config_remove_by_key(part_format_parameter);
            yon_config_remove_by_key(part_fs_type_parameter);
            yon_config_remove_by_key(part_format_parameter);
    }

    if (!main_config.configure_mode){
        yon_debug_output("%s\n",yon_config_save_args_simple(main_config.config_load_path,part_format_parameter,part_label_parameter,part_fs_type_parameter,device_parameter,part_parameter,AUTOINSTALL_TYPE_INSTALL,NULL));
    }
    return 1;
}

int yon_install_same_partition_save(main_window *widgets){
    GtkTreeModel *model;
    GtkTreeIter iter;
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SamePlaceDeviceTree)),&model,&iter)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NO_DEVICE_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->CommonInstallationDevicesTree));
        return 0;
    }
    char *device;
    gtk_tree_model_get(model,&iter,0,&device,-1);
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SamePlacePartTree)),&model,&iter)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NO_PART_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->CommonInstallationDevicesTree));
        return 0;
    }
    yon_config_remove_by_key(part_size_parameter);
    char *part;
    gtk_tree_model_get(model,&iter,0,&part,-1);
        yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_TYPE_INSTALL_command,"part");
        yon_config_register(device_parameter,device_parameter_command,device);
        yon_config_register(part_parameter,part_parameter_command,part);

    if (gtk_switch_get_active(GTK_SWITCH(widgets->SameInstallationFormatSwitch))){
        yon_config_register(part_format_parameter,part_format_parameter_command,"yes");
        char *device_name = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->SameInstallationSectionNameEntry));
        if (!yon_char_is_empty(device_name)){
            yon_config_register(part_label_parameter,part_label_parameter_command,device_name);
        } else {
            yon_config_remove_by_key(part_label_parameter);
        }

        char *file_system_type = (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widgets->SameInstallationFilesystemTypeCombo));
        if (!yon_char_is_empty(file_system_type)){
            yon_config_register(part_fs_type_parameter,part_fs_type_parameter_command,file_system_type);
        } else {
            yon_config_remove_by_key(part_fs_type_parameter);

        }
    } else {
        yon_config_register(part_format_parameter,part_format_parameter_command,"no");
        yon_config_remove_by_key(part_label_parameter);
        yon_config_remove_by_key(part_format_parameter);
        yon_config_remove_by_key(part_fs_type_parameter);
    }
    if (!main_config.configure_mode){
        yon_debug_output("%s\n",yon_config_save_args_simple(main_config.config_load_path,part_format_parameter,part_size_parameter,part_label_parameter,part_fs_type_parameter,AUTOINSTALL_TYPE_INSTALL,device_parameter,part_parameter,NULL));
    }
    return 1;
}

void yon_size_changed(GtkSpinButton *self, main_window *widgets){
    int cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook));
    GtkWidget *tree = NULL;
    switch(cur_page){
        case YON_PAGE_INSTALL_SEPARATE:
            tree = widgets->NextInstallationSysSectionTree;
            break;
        case YON_PAGE_RECOVERY_OS_ONLY:
            tree = widgets->OSSysSectionTree;
            break;
        case YON_PAGE_RECOVERY_USRDATA_ONLY:
            tree = widgets->UserdataSysSectionTree;
            break;
    }
    if (!tree) return;

    GtkTreeModel *model;
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(tree)),&model, &iter)){
        long size;
        long free_space;
        gtk_tree_model_get(model,&iter,5,&size,6,&free_space,-1);
        double chosen_size = gtk_spin_button_get_value(self);
        int mod = yon_get_size_get_from_letter(gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->NextInstallationSizeTypeSpin))[0]);
        chosen_size = yon_size_to_bytes(chosen_size,mod+1);
        if (chosen_size>free_space&&size>chosen_size){
            yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NOT_ENOUGH_FREE_SPACE_LABEL,5,BACKGROUND_IMAGE_INFO_TYPE);
            return;
        } else if (chosen_size>free_space&&chosen_size>size){
            yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NOT_ENOUGH_SPACE_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            return;
        }
    }
}

void yon_gparted_update_unlock(main_window *widgets){
    gtk_widget_set_sensitive(widgets->CommonInstallationDevicesTree,1);
    gtk_widget_set_sensitive(widgets->NextInstallationSysDevicesTree,1);
    gtk_widget_set_sensitive(widgets->NextInstallationSysSectionTree,1);
    gtk_widget_set_sensitive(widgets->SamePlaceDeviceTree,1);
    gtk_widget_set_sensitive(widgets->SamePlacePartTree,1);
    gtk_widget_set_sensitive(widgets->GrubInstallDevicesTree,1);
    gtk_widget_set_sensitive(widgets->GrubInstallPartitionTree,1);
    gtk_widget_set_sensitive(widgets->GrubUpdateDevicesTree,1);
    gtk_widget_set_sensitive(widgets->GrubUpdatePartitionTree,1);
    gtk_widget_set_sensitive(widgets->OSDevicesTree,1);
    gtk_widget_set_sensitive(widgets->OSSysSectionTree,1);
    gtk_widget_set_sensitive(widgets->UserdataDevicesTree,1);
    gtk_widget_set_sensitive(widgets->UserdataSysSectionTree,1);
    gtk_widget_set_sensitive(widgets->AdvancedDeviceTree,1);
    gtk_widget_set_sensitive(widgets->AdvancedPartitionTree,1);

    gtk_widget_set_sensitive(widgets->CommonUpdateGpartedButton,1);
    gtk_widget_set_sensitive(widgets->NextInstallationUpdateGPartedButton,1);
    gtk_widget_set_sensitive(widgets->SameUpdateGpartedButton,1);
    gtk_widget_set_sensitive(widgets->GrubInstallUpdateGpartedButton,1);
    gtk_widget_set_sensitive(widgets->GrubUpdateUpdateGpartedButton,1);
    gtk_widget_set_sensitive(widgets->OSUpdateGpartedButton,1);
    gtk_widget_set_sensitive(widgets->UserdataUpdateGpartedButton,1);
    gtk_widget_set_sensitive(widgets->AdvancedUpdateGpartedButton,1);
    gtk_list_store_clear(widgets->PartitionsList);
    yon_devices_setup(widgets);
}

void *yon_partitions_list_load(main_window *widgets){
    main_config.partitions = yon_config_load(yon_debug_output("%s\n",get_parts_for_device_command),&main_config.part_size);
    for (int i=0;i<main_config.part_size;i++){
        yon_char_remove_last_symbol(main_config.partitions[i],'\n');
    }
    g_idle_add_once((GSourceOnceFunc)yon_gparted_update_unlock,widgets);
    g_thread_exit(NULL);
}

void *yon_devices_list_load(main_window *){
    main_config.devices = yon_config_load(yon_debug_output("%s\n",get_devices_command),&main_config.devices_size);
    for (int i=0;i<main_config.devices_size;i++){
        yon_char_remove_last_symbol(main_config.devices[i],'\n');
    }
    g_thread_exit(NULL);
}

void on_device_selection_changed(GtkWidget *self, main_window *widgets){
    gtk_list_store_clear(widgets->PartitionsList);
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(self)),&model,&iter)){
        char *disk_path;
        gtk_tree_model_get(model,&iter,0,&disk_path,-1);
        int size = main_config.part_size;
        config_str partitions;
        partitions = main_config.partitions;
        for (int i=0;i<size;i++){
            if(!strstr(partitions[i],disk_path)) continue;
            yon_char_remove_last_symbol(partitions[i],'\n');
            int parsed_size;
            config_str parsed = yon_char_parse(partitions[i],&parsed_size,";");
            if (!strcmp(parsed[1],"part")){
                char *name = parsed[2];
                if (strstr(name,disk_path)){
                    char *capacity = NULL;
                    long capacity_long = 0;
                    char *free_space = NULL;
                    // long free_space_long = 0;
                    char *fs_type = NULL;
                    char *label = NULL;
                    char *fs_label = NULL;
                    char *mount_point = NULL;
                    char *flags = NULL;
                    long used_cap=0;

                    if (parsed_size>2){
                        char sizemod='\0';
                        capacity_long = atol(parsed[3]);
                        
                        char *temp = yon_char_from_double(yon_size_long_convert_automatic(capacity_long,&sizemod));
                        char *size_str = yon_size_get_name_from_letter(sizemod);
                        capacity = yon_char_unite(temp," ", size_str,NULL);
                        free(temp);
                    }
                    if (parsed_size>4){
                        fs_type = parsed[4];
                    }
                    if (parsed_size>5){
                        label = parsed[5];
                    }
                    if (parsed_size>8&&!yon_char_is_empty(parsed[8])){
                        used_cap = capacity_long-atol(parsed[8]);
                        long capacity_persent = ((double)used_cap/(double)capacity_long)*100;
                        char sizemod='\0';
                        char *temp = yon_char_from_double(yon_size_long_convert_automatic(used_cap,&sizemod));
                        char *size_str = yon_size_get_name_from_letter(sizemod);
                            free_space = yon_char_unite(temp, " ",size_str," (",yon_char_from_long(capacity_persent),"%",")",NULL);
                        
                    }
                    if (parsed_size>6){
                        fs_label = parsed[6];
                    }
                    if (parsed_size>7){
                        mount_point = parsed[7];
                    }
                    if (parsed_size>10){
                        flags = parsed[10];
                    }

                    gtk_list_store_append(widgets->PartitionsList,&iter);
                    gtk_list_store_set(widgets->PartitionsList,&iter,0,name,1,capacity,2,free_space,3,fs_type,4,label,5,capacity_long,6,used_cap,9,disk_path,9,mount_point,10,fs_label,11,flags,-1);
                }
                yon_char_parsed_free(parsed,parsed_size);
            }
        }
    }
}

void yon_devices_setup(main_window *widgets){
    GtkTreeIter iter;
    gtk_list_store_clear(widgets->DevicesList);
    // gtk_list_store_clear(widgets->PartitionsList);
    for (int i=1;i<main_config.devices_size;i++){
        yon_char_remove_last_symbol(main_config.devices[i],'\n');
        int parsed_size;
        config_str parsed = yon_char_parse(main_config.devices[i],&parsed_size,";");
        char sizemod;
        double size_converted = yon_size_long_convert_automatic(atol(parsed[1]),&sizemod);
        char *size_string = yon_char_unite(yon_char_from_double(size_converted)," ", yon_size_get_name_from_letter(sizemod),NULL);

        gtk_list_store_append(widgets->DevicesList,&iter);
        gtk_list_store_set(widgets->DevicesList,&iter,
        0,yon_char_return_if_exist(parsed[0],""),
        1,yon_char_return_if_exist(parsed[2],""),
        2,yon_char_return_if_exist(parsed[4],""),
        3,yon_char_return_if_exist(size_string,""),
        4,yon_char_return_if_exist(parsed[3],""),
        6,1,
        7,atol(parsed[1]),
        -1);
        
        
        yon_char_parsed_free(parsed,parsed_size);
    }
}

void yon_os_password_open(GtkWidget *, main_window *widgets){
    yon_password_window *window = yon_password_open(GTK_ENTRY(widgets->OSFormatEncryptionEntry));
    yon_password_function_set(window,(double(*)(const char*))yon_password_check_func,(char *(*)(const char*))yon_password_check_error);

    yon_password_hash_list_set(window,NULL,NULL,0);
}

void yon_userdata_password_open(GtkWidget *, main_window *widgets){
    yon_password_window *window = yon_password_open(GTK_ENTRY(widgets->OSFormatEncryptionEntry));
    yon_password_hash_list_set(window,NULL,NULL,0);
    yon_password_function_set(window,(double(*)(const char*))yon_password_check_func,(char *(*)(const char*))yon_password_check_error);
}

void yon_install_init(main_window *widgets, enum YON_PAGES page){
    if (!main_config.configure_mode){
        yon_config_update_by_args(main_config.config_load_path, AUTOINSTALL_TYPE_INSTALL,
        device_parameter,part_label_parameter,part_fs_type_parameter,
        part_format_parameter,part_fs_label_parameter,part_parameter,
        part_size_parameter,part_crypt_parameter,
        NULL);
    }
    yon_devices_setup(widgets);
    
    GtkWidget *device_tree = NULL;
    GtkWidget *partition_tree = NULL;
    GtkWidget *format_switch = NULL;
    GtkWidget *device_label = NULL;
    GtkWidget *fs_type_combo = NULL;
    GtkWidget *partition_size_spin = NULL;  
    GtkWidget *partition_size_combo = NULL;
    GtkWidget *partition_mark_entry = NULL;
    GtkWidget *partition_fs_mark_entry = NULL;
    GtkWidget *partition_encryption_combo = NULL;
    GtkWidget *partition_encryption_entry = NULL;

    switch(page){
        case YON_PAGE_INSTALL_COMMON:
            device_tree = widgets->CommonInstallationDevicesTree;
            device_label = widgets->CommonInstallationSectionNameEntry;
            format_switch = widgets->CommonFormatSwitch;
            fs_type_combo = widgets->CommonInstallationFilesystemTypeCombo;
        break;
        case YON_PAGE_INSTALL_SAME_PARTITION:
            device_tree = widgets->SamePlaceDeviceTree;
            partition_tree = widgets->SamePlacePartTree;
            format_switch = widgets->SameInstallationFormatSwitch;
            partition_mark_entry = widgets->SameInstallationSectionNameEntry;
            fs_type_combo = widgets->SameInstallationFilesystemTypeCombo;
        break;
        case YON_PAGE_INSTALL_SEPARATE:
            device_tree = widgets->NextInstallationSysDevicesTree;
            partition_tree = widgets->NextInstallationSysSectionTree;
            format_switch = widgets->NextInstallationFormatSwitch;
            partition_size_spin = widgets->NextInstallationSizeSpin;
            partition_size_combo = widgets->NextInstallationSizeTypeSpin;
            partition_mark_entry = widgets->NextInstallationSectionNameEntry;
            fs_type_combo = widgets->NextInstallationFilesystemTypeCombo;

        break;
        case YON_PAGE_INSTALL_ADVANCED:
            // yon_install_advanced_init(widgets);
            return;
        break;
        case YON_PAGE_RECOVERY_GRUB_INSTALL:
            device_tree = widgets->GrubInstallDevicesTree;
            partition_tree = widgets->GrubInstallPartitionTree;

        break;
        case YON_PAGE_RECOVERY_GRUB_UPDATE:
            device_tree = widgets->GrubUpdateDevicesTree;
            partition_tree = widgets->GrubUpdatePartitionTree;

        break;
        case YON_PAGE_RECOVERY_OS_ONLY:
            device_tree = widgets->OSDevicesTree;
            partition_tree = widgets->OSSysSectionTree;
            format_switch = widgets->OSFormatSwitch;
            partition_size_spin = widgets->OSFormatSizeSpin;
            partition_size_combo = widgets->OSFormatSizeCombo;
            partition_mark_entry = widgets->OSFormatPartitionEntry;
            fs_type_combo = widgets->OSFilesystemTypeCombo;
            partition_fs_mark_entry = widgets->OSFormatFSMarkEntry;
            partition_encryption_combo = widgets->OSFormatEncryptionCombo;
            partition_encryption_entry = widgets->OSFormatEncryptionEntry;

        break;
        case YON_PAGE_RECOVERY_USRDATA_ONLY:
            device_tree = widgets->UserdataDevicesTree;
            partition_tree = widgets->UserdataSysSectionTree;
            format_switch = widgets->UserdataFormatSwitch;
            partition_size_spin = widgets->UserdataFormatSizeSpin;
            partition_size_combo = widgets->UserdataFormatSizeCombo;
            partition_mark_entry = widgets->UserdataFormatPartitionEntry;
            fs_type_combo = widgets->UserdataFilesystemTypeCombo;
            partition_fs_mark_entry = widgets->UserdataFormatFSMarkEntry;
            partition_encryption_combo = widgets->UserdataFormatEncryptionCombo;
            partition_encryption_entry = widgets->UserdataFormatEncryptionEntry;
        break;
        default:return;
    }

    char *device = config(device_parameter);
    char *part = config(part_parameter);

    if (!yon_char_is_empty(device)){
        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(widgets->DevicesList);
        for_iter(model,&iter){
            char *target;
            gtk_tree_model_get(model,&iter,0,&target,-1);
            if (!yon_char_is_empty(target)&&!strcmp(target,device)){
                gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(device_tree)),&iter);
                on_device_selection_changed(device_tree,widgets);
                break;
            }
        }
        if (partition_tree&&!yon_char_is_empty(part)){
            model = GTK_TREE_MODEL(widgets->PartitionsList);
            for_iter(model,&iter){
                char *target;
                gtk_tree_model_get(model,&iter,0,&target,-1);
                if (!yon_char_is_empty(target)&&!strcmp(target,part)){
                    gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(partition_tree)),&iter);
                    break;
                }
            }
        }
    }
    if (format_switch){
        if (fs_type_combo){
                gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(fs_type_combo));
                int size;
                config_str fs_types = yon_resource_open_file(fs_types_path,&size);
                for (int i=0;i<size;i++){
                    int parsed_size;
                    config_str parsed = yon_char_parse(fs_types[i],&parsed_size,";");
                    if (atoi(parsed[0])){
                        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(fs_type_combo),parsed[1],parsed[1]);
                    }
                    yon_char_parsed_free(parsed,parsed_size);
                }
                    yon_char_parsed_free(fs_types,size);
                char *parameter = config(part_fs_type_parameter);
                if (!yon_char_is_empty(parameter)){
                    if (!gtk_combo_box_set_active_id(GTK_COMBO_BOX(fs_type_combo),parameter)){
                        if(gtk_combo_box_get_has_entry(GTK_COMBO_BOX(fs_type_combo))){
                            gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(fs_type_combo))),parameter);
                        } 
                    }

                } else {
                    gtk_combo_box_set_active(GTK_COMBO_BOX(fs_type_combo),0);
                }
            }

        if (partition_encryption_combo && partition_encryption_entry){
            int size;
            gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(partition_encryption_combo));
                gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(partition_encryption_combo),NULL,yon_char_get_localised_from_lib(DEFAULT_LABEL));
            config_str encryptions = yon_char_parsed_new(&size,encryptions_list,NULL);
            for (int i=0;i<size;i++){
                gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(partition_encryption_combo),encryptions[i],encryptions[i]);
            }
            yon_char_parsed_free(encryptions,size);
            gtk_combo_box_set_active(GTK_COMBO_BOX(partition_encryption_combo),0);
        }
            if (device_label){
                char *parameter = config(part_label_parameter);
                if (!yon_char_is_empty(parameter)){
                    gtk_entry_set_text(GTK_ENTRY(device_label),parameter);
                } else {
                    gtk_entry_set_text(GTK_ENTRY(device_label),"");
                }
            }
            
            if (partition_size_spin&&partition_size_combo){
                gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(partition_size_combo));

                // gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(partition_size_combo),"K",_("Kb"));
                gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(partition_size_combo),"M",_("Mb"));
                gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(partition_size_combo),"G",_("Gb"));
                gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(partition_size_combo),"T",_("Tb"));
                gtk_combo_box_set_active(GTK_COMBO_BOX(partition_size_combo),0);
                char *parameter = yon_config_get(part_size_parameter);
                if (!yon_char_is_empty(parameter)){
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(partition_size_spin),atol(parameter));
                    gtk_combo_box_set_active(GTK_COMBO_BOX(partition_size_combo),yon_get_size_get_from_letter(parameter[strlen(parameter)-1])-1);
                } else {
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(partition_size_spin),16);
                    gtk_combo_box_set_active(GTK_COMBO_BOX(partition_size_combo),1);
                }
            }
            if (partition_mark_entry){
                char *parameter = config(part_label_parameter);
                if (!yon_char_is_empty(parameter)){
                    gtk_entry_set_text(GTK_ENTRY(partition_mark_entry),parameter);
                } else {
                    gtk_entry_set_text(GTK_ENTRY(partition_mark_entry),"");
                }
            }
            if (partition_fs_mark_entry){
                char *parameter = config(part_fs_label_parameter);
                if (!yon_char_is_empty(parameter)){
                    gtk_entry_set_text(GTK_ENTRY(partition_fs_mark_entry),parameter);
                } else {
                    gtk_entry_set_text(GTK_ENTRY(partition_fs_mark_entry),"");
                }
            }
            if (partition_encryption_combo && partition_encryption_entry){
                char *parameter = config(part_crypt_parameter);
                if (!yon_char_is_empty(parameter)&&!strstr(parameter,",")){
                    char *password = yon_char_new(parameter);
                    char *crypt = yon_char_divide_search(password,":",-1);
                    gtk_combo_box_set_active_id(GTK_COMBO_BOX(partition_encryption_combo),crypt);
                    gtk_entry_set_text(GTK_ENTRY(partition_encryption_entry),password);
                    free(password);
                    free(crypt);
                } else {
                    gtk_combo_box_set_active(GTK_COMBO_BOX(partition_encryption_combo),0);
                    gtk_entry_set_text(GTK_ENTRY(partition_encryption_entry),"");
                }
            }
        char *format = config(part_format_parameter);
        if ((!yon_char_is_empty(format)&&!strcmp(format,"yes"))||!gtk_widget_get_sensitive(format_switch)){
            gtk_switch_set_active(GTK_SWITCH(format_switch),1);
        } else if (gtk_widget_get_sensitive(format_switch)){
            gtk_switch_set_active(GTK_SWITCH(format_switch),0);
        }
    }
}