#include "ubinstall-gtk.h"

void on_kernel_install_enabled(GtkWidget *, kernel_row *row){
    int active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->InstallCheck));
    gtk_widget_set_sensitive(row->EnableRadio,active);
    if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->InstallCheck))&&gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->EnableRadio))){
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(main_config.kernel_unchosen_radio),1);
    }
}

gboolean on_kernel_enabled_toggled(GtkWidget *self, GdkEventButton *, kernel_row *row){
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->EnableRadio))){
        g_signal_handlers_block_by_func(G_OBJECT(self),"toggled",row);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(main_config.kernel_unchosen_radio),1);
        g_signal_handlers_unblock_by_func(G_OBJECT(self),"toggled",row);
        return 1;
    }
    return 0;
}

void on_kernel_info(GtkWidget *, char *link, kernel_row *row){
    if (!yon_char_is_empty(link)){
        yon_package_info_window_create(link);
    } else {
        yon_package_info_window_create(row->package);
    }
}

kernel_row *yon_kernel_row_new(){
    kernel_row *row = new(kernel_row);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_kernel_row);

    row->RowBox = yon_gtk_builder_get_widget(builder,"TableRow");
    row->InstallCheck = yon_gtk_builder_get_widget(builder,"InstallCheck");
    row->EnableRadio = yon_gtk_builder_get_widget(builder,"EnableRadio");
    row->EnableSeparator = yon_gtk_builder_get_widget(builder,"EnableSeparator");
    row->NameLabel = yon_gtk_builder_get_widget(builder,"NameLabel");
    row->TagsBox = yon_gtk_builder_get_widget(builder,"TagsBox");
    row->TagsSeparator = yon_gtk_builder_get_widget(builder,"TagsSeparator");
    row->ModulesLabel = yon_gtk_builder_get_widget(builder,"ModulesLabel");
    row->DescriptionLabel = yon_gtk_builder_get_widget(builder,"DescriptionLabel");
    row->name=NULL;
    row->modules=NULL;
    row->package=NULL;

    g_signal_connect(G_OBJECT(row->InstallCheck),"toggled",G_CALLBACK(on_kernel_install_enabled),row);
    g_signal_connect(G_OBJECT(row->EnableRadio),"button-release-event",G_CALLBACK(on_kernel_enabled_toggled),row);
    g_signal_connect(G_OBJECT(row->NameLabel),"activate-link",G_CALLBACK(on_kernel_info),row);
    g_signal_connect(G_OBJECT(row->ModulesLabel),"activate-link",G_CALLBACK(on_kernel_info),row);
    g_signal_connect(G_OBJECT(row->NameLabel),"button-press-event",G_CALLBACK(on_kernel_addon_menu_block),row);
    g_signal_connect(G_OBJECT(row->ModulesLabel),"button-press-event",G_CALLBACK(on_kernel_addon_menu_block),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->EnableRadio),"kernel_row",row);
    g_object_set_data(G_OBJECT(row->row),"kernel_row",row);

    return row;
}

int yon_tag_add(GtkBox *target,char *tag_label, char *tag_style, char *icon_name){
    if (GTK_IS_BOX(target)&&!yon_char_is_empty(tag_label)&&!yon_char_is_empty(tag_style)){
        GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,2);
        GtkWidget *label = gtk_label_new(tag_label);
        PangoAttrList *attr = pango_attr_list_new();
        PangoAttribute *scale_attr = pango_attr_size_new_absolute(12 * PANGO_SCALE);
        gtk_label_set_attributes(GTK_LABEL(label),attr);

        g_object_set_data(G_OBJECT(box),"label",label);

        pango_attr_list_insert(attr,scale_attr);

        gtk_style_context_add_class(gtk_widget_get_style_context(label),tag_style);
        GtkWidget *icon = NULL;
        if (icon_name){
            icon = gtk_image_new_from_icon_name(icon_name,GTK_ICON_SIZE_BUTTON);
            gtk_box_pack_start(GTK_BOX(box),icon,0,0,0);
        }
        gtk_box_pack_start(GTK_BOX(box),label,1,1,0);
        gtk_widget_show(label);
        gtk_widget_show(box);
        gtk_box_pack_start(target,box,0,0,0);
        return 1;
    }
    return 0;
}

void yon_kernel_row_setup_tags(kernel_row *row, char *tags){
    int size;
    config_str parsed = yon_char_parse(tags,&size,", ");
    if (!size) return;
    for (int i=size-1;i>=0;i--){
        char *passw = parsed[i][0]==' '?yon_char_divide(parsed[i],0):NULL;
        if (passw) free(passw);
        char *tag_name = _(parsed[i]);
        char *tag_type = NULL;
        if (!strcmp(_(parsed[i]),LTS_TAG)){
            tag_type = "tag_blue";
        } else if (!strcmp(tag_name,STABLE_TAG)){
            tag_type = "tag_green";
        } else if (!strcmp(tag_name,REALTIME_TAG)){
            tag_type = "tag_orange";
        } else if (!strcmp(tag_name,HARDENED_TAG)){
            tag_type = "tag_yellow";
        } else if (!strcmp(tag_name,RECOMENDED_TAG)){
            tag_type = "tag_blue";
        } else if (!strcmp(tag_name,REACTIVE_TAG)){
            tag_type = "tag_purple";
        } else{
            tag_type = "tag_green";
        };
        yon_tag_add(GTK_BOX(row->TagsBox),tag_name,tag_type,NULL);
        while(gtk_events_pending()) gtk_main_iteration();
    }
}

void yon_kernel_row_setup(kernel_row *row, char *name, char *modules,char *package, char *tags, char *description){
    row->name = yon_char_new(name);
    row->modules = yon_char_new(modules);
    row->package = yon_char_new(package);

    char *description_full = yon_char_new(_(description));
    if (strlen(description)>100){
        guint size;
        config_str description_wrapped = yon_char_wrap_to_lines(description_full,3,&size);
        description_full = yon_char_parsed_to_string(description_wrapped,size,"\n");
        yon_char_parsed_free(description_wrapped,size);
        while(gtk_events_pending()) gtk_main_iteration();
    }
    int modules_size=0;
    config_str modules_parsed = yon_char_parse(modules,&modules_size," ");
    char *label_markup = yon_char_unite("<a href=\"\">",name,"</a>",NULL);
    for (int i=0;i<modules_size;i++){
        char *temp = yon_char_unite("<a href=\"",modules_parsed[i],"\">",modules_parsed[i],"</a>",NULL);
        free(modules_parsed[i]);
        modules_parsed[i] = temp;
        while(gtk_events_pending()) gtk_main_iteration();
    }
    char *modules_final = yon_char_parsed_to_string(modules_parsed,modules_size,"\n");
    gtk_label_set_markup(GTK_LABEL(row->NameLabel),label_markup);
    gtk_label_set_markup(GTK_LABEL(row->ModulesLabel),modules_final);
    gtk_label_set_label(GTK_LABEL(row->DescriptionLabel),description_full);

    yon_kernel_row_setup_tags(row,tags);
    free(description_full);
    free(modules_parsed);
    free(label_markup);
}

void yon_kernel_resize(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->KernelListBox));
    GList *iter = list;
    int install_size=0;
    int enable_size=0;
    int name_size=0;
    int modules_size=0;
        gtk_widget_get_preferred_width(widgets->KernelInstallLabel,&install_size,NULL);
        gtk_widget_get_preferred_width(widgets->KernelEnableLabel,&enable_size,NULL);
        gtk_widget_get_preferred_width(widgets->KernelNameLabel,&name_size,NULL);
        gtk_widget_get_preferred_width(widgets->KernelModulesLabel,&modules_size,NULL);

    for (;iter;iter=iter->next){
        kernel_row *row = g_object_get_data(G_OBJECT(iter->data),"kernel_row");
        int cur_name_size;
        int cur_modules_size;

        gtk_widget_get_preferred_width(row->NameLabel,&cur_name_size,NULL);
        gtk_widget_get_preferred_width(row->ModulesLabel,&cur_modules_size,NULL);
        if (name_size<cur_name_size) name_size=cur_name_size;
        if (modules_size<cur_modules_size) modules_size=cur_modules_size;
        while(gtk_events_pending()) gtk_main_iteration();
    }
        gtk_widget_set_size_request(GTK_WIDGET(widgets->KernelNameLabel),name_size,-1);
        gtk_widget_set_size_request(GTK_WIDGET(widgets->KernelModulesLabel),modules_size,-1);
    for (iter = list;iter;iter = iter->next){
        kernel_row *row = g_object_get_data(G_OBJECT(iter->data),"kernel_row");
        gtk_widget_set_size_request(GTK_WIDGET(row->InstallCheck),install_size,-1);
        gtk_widget_set_size_request(GTK_WIDGET(row->EnableRadio),enable_size,-1);
        gtk_widget_set_size_request(row->NameLabel,name_size,-1);
        gtk_widget_set_size_request(row->ModulesLabel,modules_size,-1);
        while(gtk_events_pending()) gtk_main_iteration();
    }
        g_list_free(list);
}

void yon_kernel_addon_resize(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->KernelAddonListBox));
    GList *iter = list;
    int install_size=0;
    int name_size=0;
    int modules_size=0;

    for (;iter;iter=iter->next){
        kernel_row *row = g_object_get_data(G_OBJECT(iter->data),"kernel_row");
        int cur_name_size;
        int cur_modules_size;

        gtk_widget_get_preferred_width(row->NameLabel,&cur_name_size,NULL);
        gtk_widget_get_preferred_width(row->ModulesLabel,&cur_modules_size,NULL);
        if (name_size<cur_name_size) name_size=cur_name_size;
        if (modules_size<cur_modules_size) modules_size=cur_modules_size;
    }
    for (iter = list;iter;iter = iter->next){
        kernel_row *row = g_object_get_data(G_OBJECT(iter->data),"kernel_row");
        gtk_widget_set_size_request(GTK_WIDGET(row->InstallCheck),install_size,-1);
        gtk_widget_set_size_request(row->NameLabel,name_size,-1);
        gtk_widget_set_size_request(row->ModulesLabel,modules_size,-1);
    }
        g_list_free(list);
}

void yon_kernel_init(main_window *widgets){
    if (!main_config.configure_mode){
        yon_config_update_by_args(main_config.config_load_path, packages_parameter,KERNEL_BOOT_parameter,NULL);
    }
    while(gtk_events_pending()) gtk_main_iteration();
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->KernelListBox));
    GList *iter;
    for (iter = list; iter; iter = iter->next){
        kernel_row *row = g_object_get_data(G_OBJECT(iter->data),"kernel_row");
        if (row){
            if (row->name) free(row->name);
            if (!yon_char_is_empty(row->modules)) free(row->modules);
            free(row);
            gtk_widget_destroy(GTK_WIDGET(iter->data));
        }
        while(gtk_events_pending()) gtk_main_iteration();
    }
    g_list_free(list);

    char *modules = config(packages_parameter);
    char *enabled = config(KERNEL_BOOT_parameter);

    int modules_size;
    config_str modules_parsed = NULL;
    if (!yon_char_is_empty(modules)){
        modules_parsed = yon_char_parse(modules,&modules_size,",");
    }

        while(gtk_events_pending()) gtk_main_iteration();
    int size;
    config_str kernels = yon_resource_open_file(kernel_list_path,&size);
        gtk_size_group_add_widget(widgets->KernelSizeGroup,widgets->KernelTagsLabel);

    for (int i=1;i<size;i++){
        int parsed_size;
        config_str parsed = yon_char_parse(kernels[i],&parsed_size,";");
        if (!yon_packages_check_exist(parsed[1])||!yon_packages_check_exist(parsed[3])) continue;
        char *name = parsed[0];
        char *modules = yon_char_unite(parsed[1]," ",parsed[3],NULL);
        char *tags = parsed[5];
        char *package = parsed[2];
        
        char *description = parsed[6];
        kernel_row *row = yon_kernel_row_new();
        gtk_list_box_insert(GTK_LIST_BOX(widgets->KernelListBox),row->row,-1);
        gtk_size_group_add_widget(widgets->KernelSizeGroup,row->TagsBox);

        gtk_radio_button_join_group(GTK_RADIO_BUTTON(row->EnableRadio),GTK_RADIO_BUTTON(main_config.kernel_unchosen_radio));

        yon_kernel_row_setup(row,name,modules,package,tags,description);

        while(gtk_events_pending()) gtk_main_iteration();
        if (yon_char_parsed_check_exist(modules_parsed,modules_size,parsed[1])>-1){
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->InstallCheck),1);
        } else {
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->InstallCheck),0);
        }
        if (yon_packages_check_installed(parsed[1])||yon_packages_check_installed(package)){
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->InstallCheck),1);
            gtk_widget_set_sensitive(row->InstallCheck,0);
        }
        if (!yon_char_is_empty(enabled)&&(!strcmp(package,enabled)||yon_char_check_element(modules,enabled," "))){
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->EnableRadio),1);
        }
        yon_char_parsed_free(parsed,parsed_size);
    while(gtk_events_pending()) gtk_main_iteration();
    }
    yon_kernel_resize(widgets);

    yon_char_parsed_free(kernels,size);

    yon_char_parsed_free(modules_parsed,modules_size);
}

void on_kernel_addon_install_enabled(GtkWidget *, kernel_addon_row *row){
    if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->InstallCheck))){
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(main_config.kernel_unchosen_radio),1);
    }
}

void yon_gtk_label_set_font(GtkLabel *label){
    g_return_if_fail(GTK_IS_LABEL(label));
    PangoAttrList *attributes = pango_attr_list_new();
    PangoFontDescription *font_descr = pango_font_description_new();
    pango_font_description_set_family(font_descr,"Source Code Pro 10");
    PangoAttribute *font_attr = pango_attr_font_desc_new(font_descr);
    pango_attr_list_insert(attributes,font_attr);
    gtk_label_set_attributes(GTK_LABEL(label),attributes);
}

info_element *yon_package_info_element_new(){
    info_element *element = malloc(sizeof(info_element));
    memset(element,0,sizeof(info_element));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_package_info);
    element->MainBox = yon_gtk_builder_get_widget(builder,"MainBox");
    element->InfoTree = yon_gtk_builder_get_widget(builder,"InfoTree");
    element->InfoList = GTK_LIST_STORE(gtk_builder_get_object(builder,"InfoList"));
 
    return element;
}

gboolean on_kernel_addon_info(GtkLabel *self, char *, kernel_addon_row *row){
    yon_window *window = yon_window_new();
    yon_gtk_window_setup(GTK_WINDOW(window->Window),NULL,(GtkWidget*)self==row->PackageLabel?PACKAGE_INFO_LABEL:MODULE_INFO_LABEL,icon_path,"info-label");
    char *package=NULL;
        package = yon_char_new(gtk_label_get_text(self));
    int size=0;

    if (strstr(package,",")){
        
        char *temp = yon_char_replace(package,","," ");
        free(package);
        package = temp;
    }
    
    GtkWidget *label = NULL;
    yon_packages_info *info_struct = yon_packages_get_info_struct(YON_PACKAGES_ALL,package);
    if (!info_struct) return 0;
    char* info_string = yon_packages_get_info_string(info_struct);
    config_str info = yon_char_parse(info_string,&size,"\n");
    if (size>0){
        info_element *element = yon_package_info_element_new();
        gtk_box_pack_start(GTK_BOX(window->MainBox),element->MainBox,1,1,0);
        for (int i=0;i<size;i++){
            yon_char_remove_last_symbol(info[i],'\n');
            GtkTreeIter iter;
            int parsed_size;
            config_str parsed = yon_char_parse(info[i],&parsed_size,":");
            gtk_list_store_append(element->InfoList,&iter);
            gtk_list_store_set(element->InfoList,&iter,0,parsed[0],1,!yon_char_is_empty(parsed[1])?_(parsed[1]):"",-1);
            
        }
    } else {
        label = gtk_label_new(PACKAGE_NOT_FOUND_LABEL);
        yon_gtk_label_set_font(GTK_LABEL(label));
        gtk_widget_show(label);
        gtk_box_pack_start(GTK_BOX(window->MainBox),label,0,0,0);
        gtk_label_set_xalign(GTK_LABEL(label),0);
    }
    gtk_widget_show(window->Window);
    return 0;
}

gboolean on_kernel_addon_menu_block(GObject *, GdkEventButton *event){
    if (event->button==3){
        return 1;
    }
    return 0;
}

kernel_addon_row *yon_kernel_addon_row_new(){
    kernel_addon_row *row = new(kernel_addon_row);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_kernel_addon_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->ModuleLabel = yon_gtk_builder_get_widget(builder,"ModuleLabel");
    row->PackageLabel = yon_gtk_builder_get_widget(builder,"PackageLabel");
    row->DescriptionLabel = yon_gtk_builder_get_widget(builder,"DescriptionLabel");
    row->name=NULL;
    row->modules=NULL;
    row->package=NULL;

    g_signal_connect(G_OBJECT(row->InstallCheck),"toggled",G_CALLBACK(on_kernel_addon_install_enabled),row);
    g_signal_connect(G_OBJECT(row->ModuleLabel),"activate-link",G_CALLBACK(on_kernel_addon_info),row);
    g_signal_connect(G_OBJECT(row->PackageLabel),"activate-link",G_CALLBACK(on_kernel_addon_info),row);
    g_signal_connect(G_OBJECT(row->PackageLabel),"button-press-event",G_CALLBACK(on_kernel_addon_menu_block),row);
    g_signal_connect(G_OBJECT(row->ModuleLabel),"button-press-event",G_CALLBACK(on_kernel_addon_menu_block),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_kernel_addon_row_setup(kernel_addon_row *row, char *name, char *modules,char *package, char *description){
    row->name = yon_char_new(name);
    row->modules = yon_char_new(modules);
    row->package = yon_char_new(package);

    char *description_full = NULL;
    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);
    } else {
        description_full = yon_char_new(_(description));
    }

    char *package_label = NULL;
    if (!yon_char_is_empty(row->modules)){
        package_label = yon_char_unite("<i><a href=\"\">",row->modules,"</a></i>",NULL);
        gtk_label_set_markup(GTK_LABEL(row->ModuleLabel),package_label);
    } else {
        gtk_widget_hide(gtk_widget_get_parent(row->ModuleLabel));
    }
    char *module_label = NULL;
    if (!yon_char_is_empty(row->package)){
        module_label = yon_char_unite("<i><a href=\"\">",row->package,"</a></i>",NULL);
        gtk_label_set_markup(GTK_LABEL(row->PackageLabel),module_label);
    } else {
        gtk_widget_hide(gtk_widget_get_parent(row->PackageLabel));
    }
    gtk_label_set_label(GTK_LABEL(row->NameLabel),_(name));
    gtk_label_set_label(GTK_LABEL(row->DescriptionLabel),description_full);

    free(description_full);
}

void yon_kernel_addon_init(main_window *widgets){
    yon_config_remove_by_args(packages_parameter,NULL);
    char *command = yon_config_parameter_prepare_command(packages_parameter_command,main_config.config_load_path,NULL,NULL);
    yon_config_load_config(YON_CONFIG_CUSTOM,command,NULL);
    free(command);
    while(gtk_events_pending()) gtk_main_iteration();
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->KernelAddonListBox));
    GList *iter;
    for (iter = list; iter; iter = iter->next){
        kernel_addon_row *row = g_object_get_data(G_OBJECT(iter->data),"kernel_row");
        if (row){
            if (row->name) free(row->name);
            if (!yon_char_is_empty(row->modules)) free(row->modules);
            free(row);
            gtk_widget_destroy(GTK_WIDGET(iter->data));
        }
    while(gtk_events_pending()) gtk_main_iteration();
    }
    g_list_free(list);

    char *modules = config(packages_parameter);

    int modules_size;
    config_str modules_parsed = NULL;
    if (!yon_char_is_empty(modules)){
        modules_parsed = yon_char_parse(modules,&modules_size,",");
    }

    int size;
    config_str kernels = yon_resource_open_file(kernel_list_addon_path,&size);

    for (int i=1;i<size;i++){
        int parsed_size;
        config_str parsed = yon_char_parse(kernels[i],&parsed_size,";");
        int status_module = !yon_char_is_empty(parsed[1])&&yon_packages_check_exist(parsed[1]);
        int status_package = !yon_char_is_empty(parsed[2])&&yon_packages_check_exist(parsed[2]);

        if (!status_package&&!status_module){
            yon_char_parsed_free(parsed,parsed_size);
            continue;
        }
        char *name = parsed[0];
        char *modules = yon_char_new(parsed[1]);
        char *package = parsed[2];
        
        char *description = parsed[3];
        kernel_addon_row *row = yon_kernel_addon_row_new();
        gtk_list_box_insert(GTK_LIST_BOX(widgets->KernelAddonListBox),row->row,-1);

        yon_kernel_addon_row_setup(row,name,modules,package,description);

        if (yon_char_parsed_check_exist(modules_parsed,modules_size,parsed[1])>-1){
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->InstallCheck),1);
        } else {
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->InstallCheck),0);
        }
        if (yon_packages_check_installed(modules)||yon_packages_check_installed(package)){
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->InstallCheck),1);
            gtk_widget_set_sensitive(row->InstallCheck,0);
        }
        yon_char_parsed_free(parsed,parsed_size);
        while(gtk_events_pending()) gtk_main_iteration();
    }
    yon_kernel_addon_resize(widgets);

    yon_char_parsed_free(kernels,size);


}
int yon_kernel_save(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->KernelListBox));
    char *config_val = config(packages_parameter);
    if (yon_char_is_empty(config_val)) {
        char *old_packages = config(packages_parameter);
        yon_config_register(packages_parameter,packages_parameter_command,NULL);
        int status = yon_char_is_empty(old_packages);
        yon_config_set_status(packages_parameter,status-1);
    }
    char *enabled_module = NULL;
    int overall_size = 0;
    int active_size = 0;
    for(GList *iter = list;iter;iter = iter->next){
        overall_size++;
        kernel_row *row = g_object_get_data(G_OBJECT(iter->data),"kernel_row");
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->InstallCheck))){
            if (gtk_widget_get_sensitive(row->InstallCheck)){
                char *append_str = yon_char_replace(row->modules," ",",");
                yon_config_append_elements(packages_parameter,append_str,",");
                free(append_str);
                active_size++;
            }
            if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->EnableRadio))){
                enabled_module = row->package;
            }
        } else {
            char *append_str = yon_char_replace(row->modules," ",",");
            yon_config_remove_elements(packages_parameter,append_str,",");
            free(append_str);
        }
    }

    if (!yon_char_is_empty(enabled_module)){
        yon_config_register(KERNEL_BOOT_parameter,KERNEL_BOOT_parameter_command,enabled_module);
    } else {
        yon_config_remove_by_key(KERNEL_BOOT_parameter);
    }

    if (!main_config.configure_mode){
        yon_debug_output("%s\n",yon_config_save_args_simple(main_config.config_load_path,packages_parameter, KERNEL_BOOT_parameter,NULL));
    }
    return 1;
}

int yon_kernel_addon_save(main_window *widgets){
    char *parameter = config(packages_parameter);
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->KernelAddonListBox));
    if (yon_char_is_empty(parameter)){
        yon_config_register(packages_parameter,packages_parameter_command,NULL);
        
    }
    for(GList *iter = list;iter;iter = iter->next){
        kernel_addon_row *row = g_object_get_data(G_OBJECT(iter->data),"kernel_row");
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->InstallCheck))){
            if (gtk_widget_get_sensitive(row->InstallCheck)){
                yon_config_append_elements(packages_parameter,row->modules,",");
            }
        } else {
            char *append_str = yon_char_replace(row->modules," ",",");
            yon_config_remove_elements(packages_parameter,row->modules,",");
            free(append_str);
        }
    }

    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(yon_debug_output("%s\n",command))){}
            free(command);
            yon_char_parsed_free(parameters,size);
        }
    }
    return 1;
}