#include "ubinstall-gtk.h"

int yon_kernel_save(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->KernelListBox));
    char *install_modules = "";
    char *enabled_module = NULL;
    for(GList *iter = list;iter;iter = iter->next){
        kernel_row *row = g_object_get_data(G_OBJECT(iter->data),"kernel_row");
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->InstallCheck))){
            char *temp = yon_char_unite(install_modules,!yon_char_is_empty(install_modules)?" ":"",row->modules,NULL);
            if (!yon_char_is_empty(install_modules)) free(install_modules);
            install_modules = temp;
            if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->EnableRadio))){
                enabled_module = row->package;
            }
        }
    }

    if (yon_char_is_empty(enabled_module)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),ENABLED_KERNEL_MISSING_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        return 0;
    }

    if (!yon_char_is_empty(install_modules)){
        yon_config_register(modules_extra_parameter,modules_extra_parameter_command,install_modules);
    } else {
        yon_config_remove_by_key(modules_extra_parameter);
    }
    yon_config_register(KERNEL_BOOT_parameter,KERNEL_BOOT_parameter_command,enabled_module);
    if (!main_config.configure_mode){
        int size;
        config_str parameters = yon_config_get_save_parameters_by_key(&size,modules_extra_parameter,KERNEL_BOOT_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(command)){}
            free(command);
            yon_char_parsed_free(parameters,size);
        }
    }

    return 1;
}

int yon_kernel_addon_save(main_window *widgets){
    // yon_kernel_save(widgets);
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->KernelAddonListBox));
    char *install_modules = "";
    for(GList *iter = list;iter;iter = iter->next){
        kernel_row *row = g_object_get_data(G_OBJECT(iter->data),"kernel_row");
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->InstallCheck))){
            char *temp = yon_char_unite(install_modules,!yon_char_is_empty(install_modules)?" ":"",row->modules,NULL);
            if (!yon_char_is_empty(install_modules)) free(install_modules);
            install_modules = temp;
        }
    }

    if (!yon_char_is_empty(install_modules)){
        char *parameter = config(modules_extra_parameter);
        char *parameter_new = yon_char_unite(!yon_char_is_empty(parameter)?parameter:"",!yon_char_is_empty(parameter)?" ":"",install_modules,NULL);
        yon_config_register(modules_extra_parameter,modules_extra_parameter_command,parameter_new);
        free(parameter_new);
    }
    if (!main_config.configure_mode){
        int size;
        config_str parameters = yon_config_get_save_parameters_by_key(&size,modules_extra_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(command)){}
            free(command);
            yon_char_parsed_free(parameters,size);
        }
    }
    return 1;
}

int yon_os_components_save(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->OSSoftwareListBox));
    GList *iter;
    int size;
    config_str modules = NULL;
    for(iter = list;iter;iter=iter->next){
        os_row *row = g_object_get_data(iter->data,"kernel_row");
        char *target = row->name;
        int status = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->InstallCheck));
        if (status){
            yon_char_parsed_add_or_create_if_exists(modules,&size,target);
        }
    }
    g_list_free(list);
    if (size){
        char *final = yon_char_parsed_to_string(modules,size,",");
        yon_config_register(modules_parameter,modules_parameter_command,final);
    } else {
        yon_config_remove_by_key(modules_parameter);
    }
    if (!main_config.configure_mode){
        int parameters_size;
        config_str parameters = yon_config_get_save_parameters_by_key(&parameters_size,modules_parameter,NULL);
        if (parameters){
            char *command_parameters = yon_char_parsed_to_string(parameters,parameters_size," -- ");
            char *command = yon_char_unite(ubconfig_dull_command,"--target system ",command_parameters,NULL);
            if (!system(command)){}
            free(command);
            yon_char_parsed_free(parameters,parameters_size);
        }
    }
    return 1;
}

os_row *yon_os_row_new(){
    os_row *row = new(os_row);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_os_row);
    row->RowBox = yon_gtk_builder_get_widget(builder,"TableRow");
    row->InstallCheck = yon_gtk_builder_get_widget(builder,"InstallCheck");
    row->NameLabel = yon_gtk_builder_get_widget(builder,"NameLabel");
    row->TagsBox = yon_gtk_builder_get_widget(builder,"TagsBox");
    row->VersionLabel = yon_gtk_builder_get_widget(builder,"VersionLabel");
    row->DescriptionLabel = yon_gtk_builder_get_widget(builder,"DescriptionLabel");
    row->name=NULL;
    row->modules=NULL;
    row->version=NULL;

    g_signal_connect(G_OBJECT(row->InstallCheck),"toggled",G_CALLBACK(on_kernel_install_enabled),row);

    row->row = gtk_list_box_row_new();
    gtk_container_add(GTK_CONTAINER(row->row),row->RowBox);
    gtk_widget_show(row->row);

    g_object_set_data(G_OBJECT(row->InstallCheck),"kernel_row",row);
    g_object_set_data(G_OBJECT(row->row),"kernel_row",row);

    return row;
}

void yon_os_row_setup(os_row *row, char *name, char *version,char *tags, char *description){
    row->name = yon_char_new(name);

    char *description_full = yon_char_new(description);
    if (strlen(description)>100){
        guint size;
        config_str description_wrapped = yon_char_wrap_to_lines(description,3,&size);
        description_full = yon_char_parsed_to_string(description_wrapped,size,"\n");
        yon_char_parsed_free(description_wrapped,size);
    }
    gtk_label_set_label(GTK_LABEL(row->NameLabel),name);
    gtk_label_set_label(GTK_LABEL(row->VersionLabel),version);
    gtk_label_set_label(GTK_LABEL(row->DescriptionLabel),description_full);

    yon_kernel_row_setup_tags((kernel_row*)row,tags);
    free(description_full);
}

void yon_os_components_init(main_window *widgets){
        int size = 0;
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->OSSoftwareListBox));
        GList *iter;
    if (!list){
        int base_size;
        config_str base = yon_config_load(get_modules_command,&base_size);
        for (int i=0;i<base_size;i++){
            yon_char_remove_last_symbol(base[i],'\n');
            int parsed_size;
            config_str parsed = yon_char_parse(base[i],&parsed_size,";");
            char *version = parsed[1];
            char *name = parsed[0];
            char *tags = yon_char_replace(parsed[2]," ",", ");
            char *description = parsed[3];
            os_row *row = yon_os_row_new();
            yon_os_row_setup(row,name,version,tags,description);
            gtk_list_box_insert(GTK_LIST_BOX(widgets->OSSoftwareListBox),row->row,-1);
            int min_size=0;
            gtk_widget_get_preferred_width(row->NameLabel,&min_size,NULL);
            if (min_size>size) size=min_size;


        }
        yon_char_parsed_free(base,base_size);
        while(gtk_events_pending()) gtk_main_iteration();
        list = gtk_container_get_children(GTK_CONTAINER(widgets->OSSoftwareListBox));
        for(iter = list;iter;iter=iter->next){
            os_row *row = g_object_get_data(iter->data,"kernel_row");
            gtk_widget_set_size_request(row->NameLabel,size,-1);
        }
    }

    char *modules = config(modules_parameter);
    if (!yon_char_is_empty(modules)){
        int parsed_size;
        config_str parsed = yon_char_parse(modules,&parsed_size,",");
        for(iter = list;iter;iter=iter->next){
            os_row *row = g_object_get_data(iter->data,"kernel_row");
            char *target = row->name;
            if (yon_char_parsed_check_exist(parsed,parsed_size,target)>-1){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->InstallCheck),1);
            } else {
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->InstallCheck),0);
            }
        }
        yon_char_parsed_free(parsed,parsed_size);
    }
}

int yon_software_save(main_window *widgets){

    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->AdditionalSoftwareList);
    int size = 0;
    config_str modules = NULL;
    for_iter(model,&iter){
        char *target;
        int status;
        gtk_tree_model_get(model,&iter,0,&status,1,&target,-1);
        if (status){
            yon_char_parsed_add_or_create_if_exists(modules,&size,target);
        }
    }
    if (size){
        char *final = yon_char_parsed_to_string(modules,size,",");
        char *parameter = config(modules_extra_parameter);
        char *parameter_new = yon_char_unite(!yon_char_is_empty(parameter)?parameter:"",!yon_char_is_empty(parameter)?",":"",final,NULL);
        yon_config_register(modules_extra_parameter,modules_extra_parameter_command,parameter_new);
        free(parameter_new);
    }
    if (!main_config.configure_mode){
        int size;
        config_str parameters = yon_config_get_save_parameters_by_key(&size,modules_extra_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(command)){}
            free(command);
            yon_char_parsed_free(parameters,size);
        }
    }
    return 1;
}

void yon_software_init(main_window *widgets){
    GtkTreeIter iter;
    if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(widgets->AdditionalSoftwareList),&iter)){
        int size;
        config_str parsed = NULL;
        parsed = yon_resource_open_file(additional_software_path,&size);
        for (int i=1;i<size;i++){
            int module_size;
            config_str module_parsed = yon_char_parse(parsed[i],&module_size,"|");
            if (module_size){
                gtk_list_store_append(widgets->AdditionalSoftwareList,&iter);
                gtk_list_store_set(widgets->AdditionalSoftwareList,&iter,0,1,1,module_parsed[0],2,module_parsed[1],3,module_parsed[2],-1); //2,module_parsed[2]
                yon_char_parsed_free(module_parsed,module_size);
            }
        }
        if (size) yon_char_parsed_free(parsed,size);
    }

    char *modules = config(modules_extra_parameter);

    int size;
    config_str parsed = yon_char_parse(modules,&size,",");
    for_iter(GTK_TREE_MODEL(widgets->AdditionalSoftwareList),&iter){
        char *target;
        gtk_tree_model_get(GTK_TREE_MODEL(widgets->AdditionalSoftwareList),&iter,1,&target);
        if (yon_char_parsed_check_exist(parsed,size,target)>-1){
            gtk_list_store_set(widgets->AdditionalSoftwareList,&iter,0,1,-1);
        } else {
            gtk_list_store_set(widgets->AdditionalSoftwareList,&iter,0,0,-1);
        }
    }
    yon_char_parsed_free(parsed,size);
}

int yon_pacman_software_save(main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->PacmanSoftwareChosenList);
    int size;
    config_str parameters = NULL;
    for_iter(model,&iter){
        char *target;
        gtk_tree_model_get(model,&iter,1,&target,-1);
        yon_char_parsed_add_or_create_if_exists(parameters,&size,target);        
    }
    char *final = yon_char_parsed_to_string(parameters,size,",");
    yon_char_parsed_free(parameters,size);
    if (!yon_char_is_empty(final)){
        yon_config_register(packages_parameter,packages_parameter_command,final);
    } else {
        yon_config_remove_by_key(packages_parameter);
    }
    if (!main_config.configure_mode){
        int size;
        config_str parameters = yon_config_get_save_parameters_by_key(&size,packages_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(command)){}
            free(command);
            yon_char_parsed_free(parameters,size);
        }
    }
    return 1;
}

void yon_pacman_init(main_window *widgets){
    gtk_list_store_clear(widgets->PacmanSoftwareChosenList);
    char *pacman_packages = config(packages_parameter);

    int size;
    config_str parsed = yon_char_parse(pacman_packages,&size,",");
    for (int i=0;i<size;i++){
        char *description = yon_packages_get_description(YON_PACKAGES_SYNC,parsed[i]);
        char *version = yon_packages_get_version(YON_PACKAGES_SYNC,parsed[i]);
        GtkTreeIter iter;
        gtk_list_store_append(widgets->PacmanSoftwareChosenList,&iter);
        gtk_list_store_set(widgets->PacmanSoftwareChosenList,&iter,0,1,1,parsed[i],2,version,3,description,-1);
    }
    yon_char_parsed_free(parsed,size);
}