#include "ubinstall-gtk.h"


void on_log_closed(GtkWidget *, log_window *window){
    main_window *widgets = g_object_get_data(G_OBJECT(window->Window),"widgets");

    gtk_widget_set_sensitive(widgets->ReadFullLogButton,1);
    gtk_widget_set_sensitive(widgets->ReadShortLogButton,1);

    free(window->command);
    window->Window=NULL;
}

log_window *yon_log_window_new(){
    log_window *window = malloc(sizeof(log_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_log_view);
    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->ScrollWindow = yon_gtk_builder_get_widget(builder,"ScrollWindow");
    window->HeadLabel = yon_gtk_builder_get_widget(builder,"headerTopic");
    window->LogLabel = yon_gtk_builder_get_widget(builder,"LogLabel");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->ScrollToEndCheck = yon_gtk_builder_get_widget(builder,"ScrollToEndCheck");
    window->monitor = NULL;
    gtk_widget_show(window->Window);
    return window;
}

void yon_read_log(GFileMonitor *,GFile *,GFile *,GFileMonitorEvent ,log_window *window){
        if (window->Window){
            int size;
            g_mutex_lock(&main_config.progress_mutex);
            config_str parsed = yon_file_open(window->command,&size);
            g_mutex_unlock(&main_config.progress_mutex);
            if (size){
                char *final = yon_char_parsed_to_string(parsed,size,"");
                gtk_label_set_text(GTK_LABEL(window->LogLabel),final);
                if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->ScrollToEndCheck))){
                    gtk_adjustment_set_value(gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(window->ScrollWindow)),gtk_adjustment_get_upper(gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(window->ScrollWindow))));
                }
                free(final);
                yon_char_parsed_free(parsed,size);
            }
            g_mutex_lock(&main_config.install_mutex);
            if (!main_config.install_complete){
                g_mutex_unlock(&main_config.install_mutex);
                return;
            } else {
                g_mutex_unlock(&main_config.install_mutex);
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->ScrollToEndCheck),0);
                gtk_widget_set_sensitive(window->ScrollToEndCheck,0);
            }
        }
    return ;
}

void on_process_log_view(GtkWidget *,main_window *widgets){
    log_window *window = yon_log_window_new();
    g_object_set_data(G_OBJECT(window->Window),"widgets",widgets);
    g_signal_connect(G_OBJECT(window->Window),"destroy",G_CALLBACK(on_log_closed),window);
    gtk_widget_set_sensitive(widgets->ReadFullLogButton,0);
    gtk_widget_set_sensitive(widgets->ReadShortLogButton,0);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),NULL,INSTALL_LOG_LABEL,icon_path,"log_viewer");
    window->command = yon_char_new(short_log_path);
    GFile *file = g_file_new_for_path(window->command);
    window->monitor = g_file_monitor_file(file,G_FILE_MONITOR_NONE,NULL,NULL);
    g_signal_connect(G_OBJECT(window->monitor),"changed",G_CALLBACK(yon_read_log),window);
    g_object_unref(file);
    yon_read_log(NULL,NULL,NULL,G_FILE_MONITOR_EVENT_CHANGED,window);
}

void on_summary_log_view(GtkWidget *,main_window *widgets){
    log_window *window = yon_log_window_new();
    g_object_set_data(G_OBJECT(window->Window),"widgets",widgets);
    g_signal_connect(G_OBJECT(window->Window),"destroy",G_CALLBACK(on_log_closed),window);
    gtk_widget_set_sensitive(widgets->ReadFullLogButton,0);
    gtk_widget_set_sensitive(widgets->ReadShortLogButton,0);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),NULL,PROGRESS_LOG_LABEL,icon_path,"log_viewer");
    window->command = yon_char_new(full_log_path);
    GFile *file = g_file_new_for_path(window->command);
    window->monitor = g_file_monitor_file(file,G_FILE_MONITOR_NONE,NULL,NULL);
    g_signal_connect(G_OBJECT(window->monitor),"changed",G_CALLBACK(yon_read_log),window);
    g_object_unref(file);
    yon_read_log(NULL,NULL,NULL,G_FILE_MONITOR_EVENT_CHANGED,window);
}

void yon_installation_progress_update(GFileMonitor *,GFile *,GFile *,GFileMonitorEvent ,main_window *widgets) {
    int size;

    g_mutex_lock(&main_config.progress_mutex);
    config_str text = yon_file_open(progress_path, &size);
    g_mutex_unlock(&main_config.progress_mutex);

    if (size) {
        if (!yon_char_is_empty(text[size-1]) && text[size-1][0] == '(') {
            char *current_copy = yon_char_new(text[size-1]);
            char *percentage = yon_char_divide_search(current_copy, ")", -1);

            if (!strstr(percentage, "#pb")) {
                double fraction = atof(percentage+1);
                gtk_label_set_text(GTK_LABEL(widgets->InstallationLabel), current_copy);
                gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widgets->InstallationProgress), fraction / 100);
                gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widgets->PackageInstallationProgress), 0);
                gtk_label_set_text(GTK_LABEL(widgets->PackageInstallationLabel), "");
            } else {
                gtk_widget_show(gtk_widget_get_parent(widgets->PackageInstallationProgress));

                config_str parsed = yon_char_parse(current_copy, &size, " ");
                if (size >= 3) {
                    double fraction = atof(parsed[2]) / 100;
                    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widgets->PackageInstallationProgress), fraction);
                    gtk_label_set_text(GTK_LABEL(widgets->PackageInstallationLabel), yon_char_parsed_to_string(parsed, size, " "));
                }
            }

        }
        yon_char_parsed_free(text, size);
    }
}
