#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_set_active(GTK_TOGGLE_BUTTON(main_config.kernel_unchosen_radio),1);
    }
}

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);

    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,", ");
    for (int i=0;i<size;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);
    }
}

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,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->ModulesLabel),modules);
    gtk_label_set_label(GTK_LABEL(row->DescriptionLabel),description_full);

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

void yon_kernel_resize(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->KernelListBox));
    GList *iter = list;
    // gtk_widget_realize(widgets->KernelListBox);
    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;
    }
        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);
    }
        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;
    // gtk_widget_realize(widgets->KernelListBox);
    int install_size=0;
    int name_size=0;
    int modules_size=0;
        gtk_widget_get_preferred_width(widgets->KernelAddonInstallLabel,&install_size,NULL);
        gtk_widget_get_preferred_width(widgets->KernelAddonNameLabel,&name_size,NULL);
        gtk_widget_get_preferred_width(widgets->KernelAddonModulesLabel,&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;
    }
        gtk_widget_set_size_request(GTK_WIDGET(widgets->KernelAddonNameLabel),name_size,-1);
        gtk_widget_set_size_request(GTK_WIDGET(widgets->KernelAddonModulesLabel),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(row->NameLabel,name_size,-1);
        gtk_widget_set_size_request(row->ModulesLabel,modules_size,-1);
    }
        g_list_free(list);
}

void yon_kernel_setup(main_window *widgets){
    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));
        }
    }
    g_list_free(list);

    char *modules = config(modules_extra_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," ");
    }

    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,";");
        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);

        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_char_is_empty(enabled)&&!strcmp(package,enabled)){
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->EnableRadio),1);
        }
        yon_char_parsed_free(parsed,parsed_size);
    }
    yon_kernel_resize(widgets);

    yon_char_parsed_free(kernels,size);

    yon_char_parsed_free(modules_parsed,modules_size);
}

void yon_kernel_addon_setup(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->KernelAddonListBox));
    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));
        }
    }
    g_list_free(list);

    char *modules = config(modules_extra_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,";");
        char *name = parsed[0];
        char *modules = yon_char_new(parsed[1]);
        char *tags = NULL;
        char *package = parsed[2];
        
        char *description = parsed[3];
        kernel_row *row = yon_kernel_row_new();
        gtk_list_box_insert(GTK_LIST_BOX(widgets->KernelAddonListBox),row->row,-1);
        gtk_size_group_add_widget(widgets->KernelSizeGroup,row->TagsBox);
        gtk_widget_destroy(row->TagsBox);
        gtk_widget_destroy(row->TagsSeparator);
        gtk_widget_destroy(row->EnableRadio);
        gtk_widget_destroy(row->EnableSeparator);

        yon_kernel_row_setup(row,name,modules,package,tags,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);
        }
        yon_char_parsed_free(parsed,parsed_size);
    }
    yon_kernel_addon_resize(widgets);

    yon_char_parsed_free(kernels,size);


}