#include "upac-gtk.h"

void yon_pacman_switch_all(main_window *widgets){
    int active = !g_object_get_data(G_OBJECT(widgets->UncheckAllCheck),"uncheck");
    GtkTreeIter iter, chiter;
    for_iter(widgets->PacmanFilter,&iter){
        gtk_tree_model_filter_convert_iter_to_child_iter(GTK_TREE_MODEL_FILTER(widgets->PacmanFilter),&chiter,&iter);
        gtk_list_store_set(widgets->PacmanList,&chiter,0,active,-1);
    }
    gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->PacmanFilter));
}

void on_pacman_toggled(GtkWidget *, char *path, main_window *widgets){
    GtkTreeIter iter;
    if (gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(widgets->PacmanFilter),&iter,path)){
        GtkTreeIter child_iter;
        gtk_tree_model_filter_convert_iter_to_child_iter(GTK_TREE_MODEL_FILTER(widgets->PacmanFilter),&child_iter,&iter);
        int status = 0;
        // char *target = NULL;
        gtk_tree_model_get(GTK_TREE_MODEL(widgets->PacmanList),&child_iter,0,&status,/*1,&target,*/-1);
        gtk_list_store_set(widgets->PacmanList,&child_iter,0,!status,-1);
        if (!status){
            // g_hash_table_add(main_config.pacman_table,yon_char_new(target));
            GdkRGBA rgba;
            rgba.alpha=0.4;
            rgba.red=0.3;
            rgba.blue=0.3;
            rgba.green=0.65;
            char *color = gdk_rgba_to_string(&rgba);
            gtk_list_store_set(GTK_LIST_STORE(widgets->PacmanList),&child_iter,6,color,-1);
        } else {
            GdkRGBA rgba;
            rgba.alpha=0.4;
            rgba.red=1;
            rgba.blue=0.3;
            rgba.green=0.3;
            char *color = gdk_rgba_to_string(&rgba);
            gtk_list_store_set(GTK_LIST_STORE(widgets->PacmanList),&child_iter,6,color,-1);
            // g_hash_table_remove(main_config.pacman_table,target);
        }
    }
    yon_size_update(widgets);
}

void on_pacman_window_toggled(GtkWidget *, char *path, yon_pacman_chosen_window *window){
    GtkTreeIter iter;
    if (gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(window->filter),&iter,path)){
        GtkTreeIter child_iter;
        gtk_tree_model_filter_convert_iter_to_child_iter(GTK_TREE_MODEL_FILTER(window->filter),&child_iter,&iter);
        int status = 0;
        // char *target = NULL;
        GtkTreeModel *model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(window->filter));
        gtk_tree_model_get(model,&child_iter,0,&status,/*1,&target,*/-1);
        gtk_list_store_set(GTK_LIST_STORE(model),&child_iter,0,!status,-1);
        // if (!status){
        //     g_hash_table_add(main_config.pacman_table,yon_char_new(target));
        // } else {
        //     g_hash_table_remove(main_config.pacman_table,target);
        // }
    }
}

void on_filter_changed(GtkWidget *, main_window *widgets){
    gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->PacmanTree),NULL);
    gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->PacmanFilter));
    gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->PacmanTree),GTK_TREE_MODEL(widgets->PacmanFilter));
}

gboolean yon_filter(GtkTreeModel* model, GtkTreeIter *iter, main_window *widgets){
    char *name;
    gtk_tree_model_get(model,iter,1,&name,-1);
    char *name_text = yon_char_new(gtk_entry_get_text(GTK_ENTRY(widgets->PacmanSearchEntry)));
    if (yon_char_is_empty(name_text)) return 1;
    int status = yon_char_check_begins_with(name,name_text);
    free(name_text);
    return status;
}

gboolean yon_pacman_window_filter(GtkTreeModel* model, GtkTreeIter *iter, main_window *){
    char *name;
    int status = 0, init_installed = 0;
    gtk_tree_model_get(model,iter,0,&status,1,&name,5,&init_installed,-1);
    if ((status&!init_installed)||(!status && init_installed)){
        return 1;
    }
    return 0;
}

void yon_pacman_set_package(struct pacman_struct *pacman_data){
    char *version = yon_packages_get_version(YON_PACKAGES_SYNC,(char*)pacman_data->package);
    char *description = yon_packages_get_description(YON_PACKAGES_SYNC,(char*)pacman_data->package);
    GtkTreeIter iter;
    long pkgsize = yon_packages_get_installed_size(YON_PACKAGES_SYNC,(char*)pacman_data->package);
    char mod = '\n';
    double size_converted = yon_size_long_convert_automatic(pkgsize,&mod);
    char *size_str = yon_char_unite(yon_char_from_double(size_converted)," ", yon_size_get_name_from_letter(mod),NULL);
    int installed = yon_packages_check_installed((char*)pacman_data->package);
    char *package_name = pacman_data->package;
    char *db_name = yon_packages_parse_path(package_name);
    gtk_list_store_append(pacman_data->widgets->PacmanList,&iter);
    gtk_list_store_set(pacman_data->widgets->PacmanList,&iter,0,installed,1,(char*)pacman_data->package,2,version,3,_(description),4,size_str,5,installed,7,db_name,-1);
    free(pacman_data->package);
    free(pacman_data);
        // while(gtk_events_pending()) gtk_main_iteration();
}

void yon_pacman_pre_fill(main_window *widgets){
    gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->PacmanTree),NULL);
    gtk_list_store_clear(widgets->PacmanList);

}

void yon_pacman_post_fill(main_window *widgets){
    gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->PacmanTree),GTK_TREE_MODEL(widgets->PacmanFilter));
    main_config.pacman_finished=1;
    yon_db_unlock_update(NULL,NULL,NULL,G_FILE_MONITOR_EVENT_CHANGED,widgets);
}

void *yon_pacman_fill(main_window *widgets){
        g_idle_add_once((GSourceOnceFunc)yon_pacman_pre_fill,widgets);
    GList *list = yon_packages_get_all();
    GList *liter;
    for (liter=list;liter;liter=liter->next){
        // if (yon_char_is_empty((char*)liter->data)||g_hash_table_contains(main_config.pacman_table,(char*)liter->data)) continue;
        struct pacman_struct *pacman_data = malloc(sizeof(struct pacman_struct));
        memset(pacman_data,0,sizeof(struct pacman_struct));
        pacman_data->widgets=widgets;
        pacman_data->package=yon_char_new((char*)liter->data);
        g_idle_add_once((GSourceOnceFunc)yon_pacman_set_package,pacman_data);
    }
    // yon_pacman_software_update_overall_size(widgets);
    // gtk_widget_set_sensitive(widgets->PacmanSearchEntry,1);
    // g_object_ref(G_OBJECT(widgets->MainSpinner));
    // gtk_container_remove(GTK_CONTAINER(widgets->PacmanLoadingOverlay),widgets->MainSpinner);
        g_idle_add_once((GSourceOnceFunc)yon_pacman_post_fill,widgets);
        return G_SOURCE_REMOVE;
}

yon_pacman_chosen_window *yon_pacman_chosen_window_new(){
    yon_pacman_chosen_window *window = malloc(sizeof(yon_pacman_chosen_window));
    memset(window,0,sizeof(yon_pacman_chosen_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_pacman);
    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->PacmanTree = yon_gtk_builder_get_widget(builder,"PacmanTree");
    window->PacmanChosenCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"PacmanChosenCell"));

    g_signal_connect(G_OBJECT(window->PacmanChosenCell),"toggled",G_CALLBACK(on_pacman_window_toggled),window);

    return window;
}

void on_pacman_chosen_window_destroyed(GtkWidget *, main_window *widgets);
void on_pacman_chosen_window_destroyed(GtkWidget *, main_window *widgets){
    gtk_widget_set_sensitive(widgets->PacmanChosenButton,1);
}

void on_pacman_open(GtkWidget *,main_window *widgets){
    yon_pacman_chosen_window *window = yon_pacman_chosen_window_new();
    gtk_widget_show(window->Window);
    
    window->filter = gtk_tree_model_filter_new(GTK_TREE_MODEL(widgets->PacmanList),NULL);
    gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(window->filter),(GtkTreeModelFilterVisibleFunc)yon_pacman_window_filter,widgets,NULL);
    gtk_tree_view_set_model(GTK_TREE_VIEW(window->PacmanTree),window->filter);

    g_signal_connect(G_OBJECT(window->Window),"destroy",G_CALLBACK(on_pacman_chosen_window_destroyed),widgets);
    gtk_widget_set_sensitive(widgets->PacmanChosenButton,0);

}

config_str yon_pacman_get_install_packages(int *size, main_window *widgets){
    (*size) = 0 ;
    config_str final_packages = NULL;
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->PacmanFilter);
    for_iter(model,&iter){
        int status;
        int init_install;
        char *package;
        char *db_name;
        gtk_tree_model_get(model,&iter,0,&status,1,&package,5,&init_install,7,&db_name,-1);
        if(status&&!init_install){
            char *full_path = yon_char_unite(db_name,"/",package,NULL);
            yon_char_parsed_add_or_create_if_exists(final_packages,size,full_path);
        }
    }
    return final_packages;
}

config_str yon_pacman_get_remove_packages(int *size, main_window *widgets){
    (*size) = 0 ;
    config_str final_packages = NULL;
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->PacmanFilter);
    for_iter(model,&iter){
        int status;
        int init_install;
        char *package;
        gtk_tree_model_get(model,&iter,0,&status,1,&package,5,&init_install,-1);
        if(!status&&init_install){
            yon_char_parsed_add_or_create_if_exists(final_packages,size,package);
        }
    }
    return final_packages;
}

long yon_pacman_get_size(main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->PacmanFilter);
    long overall_size = 0;
    for_iter(model,&iter){
        int status;
        int init_status;
        char *package;
        gtk_tree_model_get(model,&iter,0,&status,1,&package,5,&init_status,-1);
        if (status&&!init_status)
            overall_size += yon_packages_get_installed_size(YON_PACKAGES_SYNC,package);
        else if (!status&&init_status)
            overall_size -= yon_packages_get_installed_size(YON_PACKAGES_SYNC,package);

    }
    return overall_size;
}