#include "upac-gtk.h"

void on_more_clicked(GtkWidget *, main_window *widgets){
    if(gtk_widget_get_visible(widgets->TerminalRevealer)){
        gtk_widget_hide(widgets->TerminalRevealer);
    } else {
        gtk_widget_show(widgets->TerminalRevealer);
        on_window_resize(NULL,widgets);
    GdkCursor *terminal_cursor = gdk_cursor_new_for_display(gdk_display_get_default(),GDK_SB_V_DOUBLE_ARROW);
    gdk_window_set_cursor(gtk_widget_get_window(widgets->SizeHandleEvent),terminal_cursor);
    g_object_unref(terminal_cursor);
    }
}

void on_window_resize(GtkWidget *, main_window *widgets){
    int height = gtk_widget_get_allocated_height(gtk_widget_get_parent(widgets->SizeHandleEvent));
    int new_height = yon_terminal_check_height(height,widgets->TerminalRevealer);
    if (height != new_height){
        gtk_widget_set_size_request(gtk_widget_get_parent(widgets->SizeHandleEvent),-1,new_height);
    }
}

int yon_terminal_check_height(int height, GtkWidget *terminal_area){
    int max_height = gtk_widget_get_allocated_height(terminal_area)*0.9;
    height = CLAMP(height,50,max_height);
    return height;
}

void yon_terminal_init(main_window *widgets){
    gtk_widget_add_events(widgets->SizeHandleEvent,GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK|GDK_POINTER_MOTION_MASK);
    g_signal_connect(G_OBJECT(widgets->SizeHandleEvent),"button-press-event",G_CALLBACK(on_terminal_drag),widgets);
    g_signal_connect(G_OBJECT(widgets->SizeHandleEvent),"motion-notify-event",G_CALLBACK(on_terminal_drag_move),widgets);
}

int drag_start = 0;
int base_height = 0;

gboolean on_terminal_drag(GtkWidget *, GdkEventButton *event, main_window *widgets){
    if (event->button==GDK_BUTTON_PRIMARY){
        drag_start=event->y_root;
        base_height = gtk_widget_get_allocated_height(widgets->TerminalRevealer);
        return 1;
    }
    return 0;
}

gboolean on_terminal_drag_move(GtkWidget *self, GdkEventMotion *event, main_window *widgets){
    if(event->state & GDK_BUTTON1_MASK){
        int base_y_root = 0;
        int base_y = 0;
        gdk_window_get_root_origin(gtk_widget_get_window(widgets->TerminalRevealer),NULL,&base_y_root);
        gdk_window_get_origin(gtk_widget_get_window(widgets->TerminalRevealer),NULL,&base_y);
        int vector_y = event->y_root - (base_y) ;// - drag_start;
        int new_height =  base_height - vector_y;
        new_height = yon_terminal_check_height(new_height,widgets->TerminalRevealer);
        g_signal_handlers_block_by_func(self,G_CALLBACK(on_terminal_drag_move),widgets);
        gtk_widget_set_size_request(gtk_widget_get_parent(widgets->SizeHandleEvent),-1,new_height);
        vte_terminal_set_size(VTE_TERMINAL(widgets->InstallTerminal),40,1);
        gtk_widget_queue_resize(gtk_widget_get_parent(widgets->SizeHandleEvent));
        g_signal_handlers_unblock_by_func(self,G_CALLBACK(on_terminal_drag_move),widgets);
        return 1;
    }
    return 0;
}

gboolean on_terminal_check_progress(main_window *widgets){
    g_mutex_lock(&main_config.progress_mutex);
    if (!main_config.progress_active){
        g_mutex_unlock(&main_config.progress_mutex);
        return G_SOURCE_REMOVE;
    }
    gtk_progress_bar_pulse(GTK_PROGRESS_BAR(widgets->TerminalProgress));
    g_mutex_unlock(&main_config.progress_mutex);
    return G_SOURCE_CONTINUE;
}

void on_terminal_cancel(GtkWidget *,main_window *widgets){
    const char cancel_signal = 3;
    char *command = yon_char_new(" ");
    command[0]=cancel_signal;
    char *tmp = yon_char_unite(command,"\n exit 0\n",NULL);
    free(command);
    vte_terminal_feed_child(VTE_TERMINAL(widgets->InstallTerminal),tmp,strlen(tmp));
    free(tmp);
    yon_launch(pacman_unlock_database_command);
}

gboolean on_command_execute_success(GtkWidget *,gint status,main_window *widgets){
    // gtk_revealer_set_reveal_child(GTK_REVEALER(widgets->MainRevealer),0);
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widgets->TerminalProgress),0);
    g_mutex_lock(&main_config.progress_mutex);
    main_config.progress_active=0;
    g_mutex_unlock(&main_config.progress_mutex);
    gtk_widget_set_sensitive(widgets->TerminalCancelButton,0);
    on_status_button_update(NULL,widgets);
    if (status){
        yon_ubl_status_box_render(yon_char_get_localised_from_lib(FAIL_LABEL),BACKGROUND_IMAGE_FAIL_TYPE);
        return 0;
    }
    gtk_widget_set_sensitive(widgets->PackagesStatusButton,1);
        if (!status){
            gtk_button_clicked(GTK_BUTTON(widgets->TerminalMoreButton));
        }
    yon_packages_update();
    return G_SOURCE_REMOVE;
}


void *yon_command_execute_async(char *command,main_window *widgets){
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widgets->TerminalProgress),0);
    vte_terminal_reset(VTE_TERMINAL(widgets->InstallTerminal),0,1);
    yon_terminal_integrated_start_shell(widgets->InstallTerminal,yon_debug_output("%s\n",command),NULL,NULL);
    gtk_widget_set_sensitive(widgets->TerminalCancelButton,1);
    glong columns = vte_terminal_get_column_count(VTE_TERMINAL(widgets->InstallTerminal));
    vte_terminal_set_size(VTE_TERMINAL(widgets->InstallTerminal),columns,20);
    yon_ubl_status_box_render(OPERATION_IN_PROGRESS_LABEL,BACKGROUND_IMAGE_INFO_TYPE);

    g_mutex_lock(&main_config.progress_mutex);
    main_config.progress_active=1;
    g_mutex_unlock(&main_config.progress_mutex);
    g_timeout_add(500,(GSourceFunc)on_terminal_check_progress,widgets);
    gtk_widget_set_sensitive(widgets->PackagesStatusButton,0);

    gtk_button_clicked(GTK_BUTTON(widgets->TerminalMoreButton));
    return NULL;
}