#include "upac-gtk.h"

void yon_update_page(main_window *widgets){
    yon_packages_update();
    while(gtk_events_pending()) gtk_main_iteration();
    g_signal_handlers_unblock_by_func(G_OBJECT(widgets->db_lock_monitor),G_CALLBACK(yon_db_unlock_update),widgets);
    on_status_update(NULL,widgets);
    yon_interface_update(widgets);
}

void *yon_db_update(main_window *widgets){
    yon_launch("pacsync --updated");
    g_idle_add_once((GSourceOnceFunc)yon_update_page,widgets);
    pthread_exit(NULL);
}

void on_status_button_update(GtkWidget *, main_window *widgets){
    gtk_widget_show(widgets->PackagesStatusSpinner);
    gtk_widget_hide(widgets->PackagesStatusImage);
    g_signal_handlers_block_by_func(G_OBJECT(widgets->db_lock_monitor),G_CALLBACK(yon_db_unlock_update),widgets);
    pthread_t tid=0;
    pthread_create(&tid,NULL,(void*(*)(void*))yon_db_update,widgets);

}
void *yon_status_thread_update(main_window *widgets){
    if (system(yon_check_database_command)){
        g_idle_add_once((GSourceOnceFunc)yon_status_thread_update_inactive,widgets);
    } else {
        g_idle_add_once((GSourceOnceFunc)yon_status_thread_update_active,widgets);
    }
    pthread_exit(NULL);
}

void on_status_update(GtkWidget *,main_window *widgets){
    pthread_t tid=0;
    pthread_create(&tid,NULL,(void*(*)(void*))yon_status_thread_update,widgets);

}

void yon_db_lock_update(GFileMonitor *,GFile *,GFile *,GFileMonitorEvent ,main_window *widgets){
    if (!access(pacman_lock_path,F_OK)){
        gtk_widget_set_sensitive(widgets->PackagesStatusButton,0);
}
}

void yon_db_unlock_update(GFileMonitor *,GFile *,GFile *,GFileMonitorEvent ,main_window *widgets){
    if (main_config.pacman_finished&&main_config.ubur_finished)
    if (access(pacman_lock_path,F_OK)){
        // while (1){
            // if (g_mutex_trylock(&main_config.pacman_mutex)){
            //     g_mutex_unlock(&main_config.pacman_mutex);
            //     if (g_mutex_trylock(&main_config.ubur_mutex)){
            //         g_mutex_unlock(&main_config.ubur_mutex);
                    gtk_widget_set_sensitive(widgets->PackagesStatusButton,1);
                    yon_ubl_status_box_render(UPDATE_FINISH_LABEL,BACKGROUND_IMAGE_INFO_TYPE);

                    // break;
                // }
            // }
        // }
    }
}

void yon_status_thread_update_inactive(main_window *widgets){
    gtk_widget_show(widgets->PackagesStatusImage);
    gtk_image_set_from_icon_name(GTK_IMAGE(widgets->PackagesStatusImage),inactive_icon_path,GTK_ICON_SIZE_BUTTON);
    gtk_label_set_text(GTK_LABEL(widgets->StatusLabel),UNACCESSED_LABEL);
    gtk_widget_hide(widgets->PackagesStatusSpinner);
}

void yon_status_thread_update_active(main_window *widgets){
    gtk_widget_show(widgets->PackagesStatusImage);
    gtk_image_set_from_icon_name(GTK_IMAGE(widgets->PackagesStatusImage),active_icon_path,GTK_ICON_SIZE_BUTTON);
    gtk_label_set_text(GTK_LABEL(widgets->StatusLabel),ACCESSED_LABEL);
    gtk_widget_hide(widgets->PackagesStatusSpinner);
}
