#include "ubhello.h"

void on_back_clicked(GtkWidget *, GtkWidget *socket){
    gtk_widget_destroy(socket);
}

void on_plug_connected(GtkSocket *self, main_window *widgets){
    gtk_widget_show(widgets->BackButton);
    g_signal_connect(G_OBJECT(widgets->BackButton),"clicked",G_CALLBACK(on_back_clicked),self);
    gtk_widget_hide(widgets->HeadBox);
    gtk_widget_show(widgets->ActiveContentBox);
    gtk_widget_hide(widgets->MainContentBox);
}

void on_plug_disconnected(GtkSocket *self, main_window *widgets){
    gtk_widget_hide(widgets->BackButton);
    g_signal_handlers_disconnect_by_func(G_OBJECT(widgets->BackButton),G_CALLBACK(on_back_clicked),self);
    gtk_widget_show(widgets->HeadBox);
        gtk_widget_hide(widgets->ActiveContentBox);
    gtk_widget_show(widgets->MainContentBox);
    gtk_widget_set_sensitive(widgets->MainContentBox,1);
}

gboolean on_plug_disconnect_process(GtkWidget *,main_window *widgets){
    g_mutex_lock(&main_config.app_thread_mutex);
    int active = main_config.app_thread_active;
    g_mutex_unlock(&main_config.app_thread_mutex);
    gtk_widget_show(widgets->HeadBox);
    gtk_widget_hide(widgets->ActiveContentBox);
    gtk_widget_show(widgets->MainContentBox);
    if (!active){
        on_plug_disconnected(NULL,widgets);
        return 0;
    } else {
        gtk_widget_set_sensitive(widgets->MainContentBox,0);
        gtk_widget_hide(widgets->ActiveContentBox);
    }
    return 1;
}


void yon_app_thread_complete(main_window *widgets);
void yon_app_thread_complete(main_window *widgets){
    if (!gtk_widget_get_sensitive(widgets->MainContentBox)){
        gtk_widget_set_sensitive(widgets->MainContentBox,1);
    }
}

void *yon_app_start(struct app_launch_struct *data){
    char *command = data->command;
    g_mutex_lock(&main_config.app_thread_mutex);
    main_config.app_thread_active = 1;
    g_mutex_unlock(&main_config.app_thread_mutex);

    yon_launch(command);

    g_mutex_lock(&main_config.app_thread_mutex);
    main_config.app_thread_active=0;
    g_mutex_unlock(&main_config.app_thread_mutex);
    g_idle_add_once((GSourceOnceFunc)yon_app_thread_complete,data->widgets);
    free(data);
    g_thread_exit(NULL);
}