#include "upac-gtk.h"

void on_module_event_toggled(GtkWidget *, GdkEventButton *event, os_row *row);
void on_module_toggled(GtkWidget *, os_row *row);

// gboolean on_os_components_info(GtkWidget *, GdkEventButton *, os_row *row){
//     yon_package_info_window_create(row->modules);
//     return 1;
// }

void yon_modules_switch_all(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->AdditionalComponentsList));
    GList *iter;
    int active = !g_object_get_data(G_OBJECT(widgets->UncheckAllCheck),"uncheck");
    for (iter=list;iter;iter=iter->next){
        os_row *cur = g_object_get_data(G_OBJECT(iter->data),"os_row");
        g_object_set_data(G_OBJECT(cur->RowBox),"widgets",NULL);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cur->InstallCheck),active);
        g_object_set_data(G_OBJECT(cur->RowBox),"widgets",widgets);
    }
    yon_size_update(widgets);
}

void on_module_event_toggled(GtkWidget *, GdkEventButton *event, os_row *row){
    if (event->button==1){
    int active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->InstallCheck));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->InstallCheck),!active);
    }
}

void on_module_toggled(GtkWidget *, os_row *row){
    int active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->InstallCheck));
    row->installed=active;
    main_window *widgets = g_object_get_data(G_OBJECT(row->RowBox),"widgets");
    if (widgets){
        yon_size_update(widgets);
    }
}

int yon_module_check_selected(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->AdditionalComponentsList));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        os_row *cur = g_object_get_data(G_OBJECT(iter->data),"os_row");
        if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cur->InstallCheck)))
            return 1;
    }
    return 0;
}

void yon_module_row_setup(os_row *row, char *name, char *version,char */*tags*/, char *description,char *source, char *size){
    row->name = yon_char_new(name);
    row->source = yon_char_new(source);
    row->modules = yon_char_new(name);

    char *description_full = yon_char_new(description);
    if (yon_char_is_empty(source)||yon_char_check_begins_with(source,"(local)/")){
        gtk_widget_hide(gtk_widget_get_parent(row->SourceLabel));
    } else {
        gtk_label_set_text(GTK_LABEL(row->SourceLabel),source);
    }
    
    char *label_markup = yon_char_unite("<a href=\"\">",name,"</a>",NULL);
    gtk_label_set_markup(GTK_LABEL(row->NameLabel),label_markup);
    gtk_label_set_label(GTK_LABEL(row->VersionLabel),version);
    gtk_label_set_label(GTK_LABEL(row->DescriptionLabel),description_full);
    gtk_label_set_label(GTK_LABEL(row->SizeLabel),size);

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

gboolean yon_module_insert(struct row_data *row_input){
    os_row *row = yon_module_row_new();

    g_signal_connect(G_OBJECT(row->InstallCheck),"toggled",G_CALLBACK(on_module_toggled),row);
    main_window *widgets = row_input->widgets;
    g_object_set_data(G_OBJECT(row->RowBox),"widgets",widgets);
    gtk_list_box_insert(GTK_LIST_BOX(widgets->AdditionalComponentsList),row->row,-1);
    yon_module_row_setup(row,row_input->name,row_input->version,row_input->tags,_(row_input->description),NULL,row_input->size);
    row->size_raw = row_input->size_raw;


    if (yon_packages_check_installed(row_input->name)){
        row->installed = 1;
        row->initially_installed = 1;
        g_signal_handlers_block_by_func(row->InstallCheck,G_CALLBACK(on_module_toggled),row);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->InstallCheck),1);
        g_signal_handlers_unblock_by_func(row->InstallCheck,G_CALLBACK(on_module_toggled),row);
    }
    return G_SOURCE_REMOVE;
}

os_row *yon_module_row_new(){
    os_row *row = new(os_row);
    memset(row,0,sizeof(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->InstallCheckOverlay = yon_gtk_builder_get_widget(builder,"InstallCheckOverlay");
    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->SourceLabel = yon_gtk_builder_get_widget(builder,"SourceLabel");
    row->SizeLabel = yon_gtk_builder_get_widget(builder,"SizeLabel");
    row->name=NULL;
    row->modules=NULL;
    row->version=NULL;
    row->size_raw=0;

    g_signal_connect(G_OBJECT(row->InstallCheckOverlay),"button-press-event",G_CALLBACK(on_module_event_toggled),row);
    // g_signal_connect(G_OBJECT(row->NameLabel),"activate-link",G_CALLBACK(on_os_components_info),row);
    row->row = yon_gtk_builder_get_widget(builder,"MainRow");
    gtk_widget_show(row->row);
    g_object_set_data(G_OBJECT(row->InstallCheck),"os_row",row);
    g_object_set_data(G_OBJECT(row->RowBox),"os_row",row);
    g_object_set_data(G_OBJECT(row->row),"os_row",row);

    return row;
}

void yon_module_init(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->AdditionalComponentsList));
        GList *iter;
    for (iter=list;iter;iter=iter->next){
        os_row *row = g_object_get_data(iter->data,"os_row");
        free(row);
        gtk_widget_destroy(GTK_WIDGET(iter->data));
        while(gtk_events_pending()) gtk_main_iteration();
    }
    int base_size;
        config_str base = yon_packages_find(YON_PACKAGES_ALL,"ubm-",(gsize*)&base_size);
    for (int i=0;i<base_size;i++){
        struct row_data *row = malloc(sizeof(struct row_data));
        row->version = yon_packages_get_version(YON_PACKAGES_ALL,base[i]);
        row->name = yon_char_new(base[i]);
        row->tags = yon_packages_get_groups(YON_PACKAGES_ALL,base[i]);
        int parsed_size;
        config_str tags_parsed = yon_char_parse(row->tags,&parsed_size,", ");
        if (parsed_size){
            int ubmodule_pos = yon_char_parsed_check_exist(tags_parsed,parsed_size,"ubmodule");
            if (ubmodule_pos>-1){
                free(tags_parsed[ubmodule_pos]);
                tags_parsed[ubmodule_pos] = NULL;
                row->tags = yon_char_parsed_to_string(tags_parsed,parsed_size,", ");

            }
            yon_char_parsed_free(tags_parsed,parsed_size);
        }
        row->description = yon_packages_get_description(YON_PACKAGES_ALL,base[i]);
        long size = yon_packages_get_installed_size(YON_PACKAGES_ALL,base[i]);
            char mod = '\0';
            double size_converted = yon_size_long_convert_automatic(size,&mod);
            
            char *size_str = yon_char_unite(yon_char_from_double(size_converted)," ", yon_size_get_name_from_letter(mod),NULL);
        row->size_raw = size;
        row->size = size_str;
        row->widgets=widgets;
        yon_module_insert(row);
    while(gtk_events_pending()) gtk_main_iteration();
    }
        yon_char_parsed_free(base,base_size);
    // yon_software_check_packages_size(NULL,widgets);
    return ;
}

config_str yon_modules_get_install_packages(int *size, main_window *widgets){
    (*size)=0;
    config_str module_install_packages = NULL;
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->AdditionalComponentsList));
    GList *iter = NULL;
    for (iter=list;iter;iter=iter->next){
        os_row *row = g_object_get_data(G_OBJECT(iter->data),"os_row");
        if (row){
            if (!row->initially_installed&&row->installed){
                yon_char_parsed_add_or_create_if_exists(module_install_packages,size, row->name);
            }
        }
    }
    return module_install_packages;
}

config_str yon_modules_get_remove_packages(int *size, main_window *widgets){
    (*size)=0;
    config_str module_install_packages = NULL;
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->AdditionalComponentsList));
    GList *iter = NULL;
    for (iter=list;iter;iter=iter->next){
        os_row *row = g_object_get_data(G_OBJECT(iter->data),"os_row");
        if (row){
            if (row->initially_installed&&!row->installed){
                char *name = yon_char_new(row->name);
                char *db_name = yon_packages_parse_path(name);
                if (!yon_char_is_empty(db_name)) free(db_name);
                yon_char_parsed_add_or_create_if_exists(module_install_packages,size, name);
            }
        }
    }
    return module_install_packages;
}
// yon_window *yon_package_info_window_create(const char *package_str){
//     if (yon_char_is_empty(package_str)) return NULL;
//     yon_window *window = yon_window_new();
//     yon_gtk_window_setup(GTK_WINDOW(window->Window),NULL,PACKAGE_INFO_LABEL,icon_path,"info-label");
//     int size=0;
//     char *package = yon_char_new(package_str);
//     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 NULL;
//     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 window;
// }

long yon_modules_get_size(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->AdditionalComponentsList));
    GList *iter;
    long overall_size = 0;
    for (iter=list;iter;iter=iter->next){
        os_row *row = g_object_get_data(G_OBJECT(iter->data),"os_row");
        if (row->installed&&!row->initially_installed)
            overall_size += row->size_raw;
        else if (!row->installed&&row->initially_installed)
            overall_size -= row->size_raw;

    }
    return overall_size;
}