#include "upac-gtk.h"

struct ubur_struct {
    main_window *widgets;
    config_str packages;
    int size;
    // int depend_size;
    // config_str depends;
};

void yon_ubur_create(struct ubur_struct *ubur);

void on_ubur_toggled(GtkWidget *, os_row *row);

void yon_ubur_switch_all(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->UburComponentsList));
    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");
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cur->InstallCheck),active);
    }
}

int yon_ubur_check_selected(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->UburComponentsList));
    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 on_ubur_toggled(GtkWidget *, os_row *row){
    int active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->InstallCheck));
    row->installed=active;
}

void on_ubur_source_clicked(GtkWidget *, main_window *widgets){
    filechooser_window *window = yon_file_chooser_window_new(GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),TITLE_LABEL,icon_path,"filechooser");
    if (yon_file_chooser_start(window)){
        if (!yon_char_is_empty(window->last_success_selection)){
            gtk_label_set_text(GTK_LABEL(widgets->UburSourceLabel),window->last_success_selection);
        }
    }
}

void *yon_ubur_fill(main_window *widgets){
    int size=0;
    config_str packages = yon_config_load(ubur_get_command,&size);
    struct ubur_struct *ubur = malloc(sizeof(struct ubur_struct));
    ubur->packages=packages;
    ubur->size=size;
    ubur->widgets = widgets;
    
    g_idle_add_once((GSourceOnceFunc)yon_ubur_create,ubur);
    g_thread_exit(NULL);
}

void yon_ubur_create(struct ubur_struct *ubur){
    main_window *widgets = ubur->widgets;
    gtk_widget_set_sensitive(widgets->PackagesStatusButton,0);
    int size = ubur->size;
    config_str packages = ubur->packages;
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->UburComponentsList));
    GList *iter = NULL;
    for (iter=list;iter;iter=iter->next){
        os_row *row = g_object_get_data(G_OBJECT(iter->data),"os_row");
        free(row);
        gtk_widget_destroy(GTK_WIDGET(iter->data));
        while(gtk_events_pending()) gtk_main_iteration();
    }

    for (int i=1;i<size;i++){
        yon_char_remove_last_symbol(packages[i],'\n');
        int parsed_size=0;
        config_str parsed = yon_char_parse(packages[i],&parsed_size,";");
        os_row *row = yon_module_row_new();
        g_signal_connect(G_OBJECT(row->InstallCheck),"toggled",G_CALLBACK(on_ubur_toggled),row);
        row->name = yon_char_new(parsed[0]);
        row->description=yon_char_new(parsed[2]);
        row->version=yon_char_new(parsed[1]);
        if (yon_packages_check_installed(row->name)){
            row->installed = 1;
            row->initially_installed = 1;
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->InstallCheck),1);
            gtk_widget_set_sensitive(gtk_widget_get_parent(row->SourceLabel),1);
        } else {
            gtk_widget_set_sensitive(gtk_widget_get_parent(row->SourceLabel),0);
        }
        gtk_label_set_text(GTK_LABEL(row->NameLabel),row->name);
        gtk_label_set_text(GTK_LABEL(row->VersionLabel),row->version);
        gtk_label_set_text(GTK_LABEL(row->DescriptionLabel),_(row->description));
        gtk_widget_hide(gtk_widget_get_parent(row->SizeLabel));
        gtk_widget_hide(row->TagsBox);
        gtk_list_box_insert(GTK_LIST_BOX(widgets->UburComponentsList),row->row,-1);
        gtk_widget_show(row->row);
        yon_char_parsed_free(parsed,parsed_size);
        while(gtk_events_pending()) gtk_main_iteration();
    }
    main_config.ubur_finished=1;
    yon_db_unlock_update(NULL,NULL,NULL,G_FILE_MONITOR_EVENT_CHANGED,widgets);
}

config_str yon_ubur_get_install_packages(int *size, main_window *widgets){
    (*size)=0;
    const char *source_path = gtk_label_get_text(GTK_LABEL(widgets->UburSourceLabel));
    config_str ubur_install_packages = NULL;
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->UburComponentsList));
    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){            
                if (yon_char_is_empty(source_path)){
                    yon_ubl_status_box_render(UBUR_SOURCE_EMPTY_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
                    (*size)=-1;
                    return NULL;
                }                                     
                yon_char_parsed_add_or_create_if_exists(ubur_install_packages,size, row->name);
            }
        }
    }
    return ubur_install_packages;
}

config_str yon_ubur_get_remove_packages(int *size, main_window *widgets){
    (*size)=0;
    const char *source_path = gtk_label_get_text(GTK_LABEL(widgets->UburSourceLabel));
    config_str ubur_install_packages = NULL;
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->UburComponentsList));
    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){
                if (yon_char_is_empty(source_path)){
                    yon_ubl_status_box_render(_EMPTY_IMPORTANT_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
                    (*size)=-1;
                    return NULL;
                }    
                yon_char_parsed_add_or_create_if_exists(ubur_install_packages,size, row->name);
            }
        }
    }
    return ubur_install_packages;
}