#include "ubinstall-gtk.h"

int yon_grub_install_save(main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->GrubInstallDevicesTree)),&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->GrubInstallDevicesTree));
        return 0;
    }
    char *cur_device;
    gtk_tree_model_get(model,&iter,0,&cur_device,-1);
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->GrubInstallPartitionTree)),&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->GrubInstallPartitionTree));
        return 0;
    }
    char *partition;
    gtk_tree_model_get(model,&iter,0,&partition,-1);
    yon_config_register(device_parameter,device_parameter_command,cur_device);
    yon_config_register(part_parameter,part_parameter_command,partition);
    yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_TYPE_INSTALL_command,"grub_install");

    if (!main_config.configure_mode){
        int size;
        config_str parameters = yon_config_get_save_parameters_by_key(&size,device_parameter,part_parameter,AUTOINSTALL_TYPE_INSTALL,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);
        }
    }
    return 1;
}

int yon_grub_update_save(main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->GrubUpdateDevicesTree)),&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->GrubUpdateDevicesTree));
        return 0;
    }
    char *cur_device;
    gtk_tree_model_get(model,&iter,0,&cur_device,-1);
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->GrubUpdatePartitionTree)),&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->GrubUpdatePartitionTree));
        return 0;
    }
    char *partition;
    gtk_tree_model_get(model,&iter,0,&partition,-1);
    yon_config_register(device_parameter,device_parameter_command,cur_device);
    yon_config_register(part_parameter,part_parameter_command,partition);
    yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_TYPE_INSTALL_command,"grub_update");
    
    if (!main_config.configure_mode){
        yon_debug_output("%s\n",yon_config_save_args_simple(main_config.config_load_path,device_parameter,part_parameter,AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_TYPE_INSTALL,NULL));
    }
    return 1;
}