#include "ubl-settings-video.h"

char *yon_char_extend(char *target, char symbol, size_t length){
    if (!target||strlen(target)>=length) return target;
    int size = sizeof(char)*length+1;
    char *newstring = malloc(size);
    memset(newstring,0,size);
    memset(newstring,symbol,length);
    memcpy(newstring,target,strlen(target));
    return newstring;
}

void on_terminal_revealer_open(GtkWidget *, main_window *widgets){
    int opened = gtk_widget_get_visible(widgets->TerminalPaned);
    switch (opened){
        case 0: { // opens
            gtk_button_set_label(GTK_BUTTON(widgets->MoreButton),LESS_LABEL);

            g_object_ref(widgets->TerminalMoveBox);
            gtk_container_remove(GTK_CONTAINER(widgets->TerminalMoveTargetBox),widgets->TerminalMoveBox);
            gtk_paned_add1(GTK_PANED(widgets->TerminalPaned),widgets->TerminalMoveBox);

            int pos=250;
            if (yon_window_config_get_parameter("settings","paned_pos",&pos,YON_TYPE_INT)) {
                gtk_paned_set_position(GTK_PANED(widgets->TerminalPaned),pos);
            }
            
            gtk_widget_show(widgets->TerminalPaned);
            gtk_widget_show(widgets->TerminalMoveBox);

        } break;
        case 1: { //closes
            gtk_button_set_label(GTK_BUTTON(widgets->MoreButton),MORE_LABEL);

            int pos = gtk_paned_get_position(GTK_PANED(widgets->TerminalPaned));
            yon_window_config_add_instant_parameter("paned_pos","settings",&pos,YON_TYPE_INT);

            GValue val = G_VALUE_INIT;
            g_object_get_property(G_OBJECT(widgets->TerminalPaned),"max-position",&val);
            gtk_paned_set_position(GTK_PANED(widgets->TerminalPaned),g_value_get_int(&val));
            g_value_unset(&val);

            g_object_ref(widgets->TerminalMoveBox);
            gtk_container_remove(GTK_CONTAINER(widgets->TerminalPaned),widgets->TerminalMoveBox);
            gtk_box_pack_start(GTK_BOX(widgets->TerminalMoveTargetBox),widgets->TerminalMoveBox,1,1,0);

            gtk_box_reorder_child(GTK_BOX(widgets->TerminalMoveTargetBox),widgets->TerminalMoveBox,0);
            gtk_widget_hide(widgets->TerminalPaned);
        } break;
    }
}

void on_install_activate(GtkWidget *self, main_window *widgets){
    g_signal_handlers_block_by_func(G_OBJECT(self),G_CALLBACK(on_install_activate),widgets);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(self),!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self)));
    g_signal_handlers_unblock_by_func(G_OBJECT(self),G_CALLBACK(on_install_activate),widgets);
    int is_installed = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self));
    driver_struct *driver = g_object_get_data(G_OBJECT(self),"driver_struct");
    char *install_target = NULL;
    dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
    if (self == driver->ModuleInstalledCheck){
        install_target = driver->module;
        data->title = is_installed?MODULE_REMOVE_PROCESS_LABEL(install_target):MODULE_INSTALL_PROCESS_LABEL(install_target);
    } else if (self == driver->PackageInstalledCheck){
        install_target = driver->package;
        data->title = is_installed?PACKAGE_REMOVE_PROCESS_LABEL(install_target):PACKAGE_INSTALL_PROCESS_LABEL(install_target);
    }
    if (is_installed){
        data->action_text = REMOVE_CONFIRMATION_LABEL(install_target);
    } else {
        data->action_text = self == driver->ModuleInstalledCheck?INSTALL_CONFIRMATION_LABEL(install_target):
                                                                yon_char_unite(INSTALL_CONFIRMATION_LABEL(install_target),"\n",INSTALL_ATTENTION_LABEL,NULL);
    }
    char *command = NULL;
    if (is_installed){
        command = delete_proprietary_command(install_target);
    } else {
        command = install_proprietary_command(install_target);
    }
    if (yon_confirmation_dialog_call(widgets->Window,data)!=GTK_RESPONSE_ACCEPT){
        return;
    }
        g_mutex_lock(&main_config.progress_mutex);
        main_config.progress_active=1;
        g_mutex_unlock(&main_config.progress_mutex);
    vte_terminal_set_size(VTE_TERMINAL(widgets->InstallTerminal),10,15);
    vte_terminal_set_input_enabled(VTE_TERMINAL(widgets->InstallTerminal),1);
    vte_terminal_reset(VTE_TERMINAL(widgets->InstallTerminal),0,1);
    g_signal_connect_after(G_OBJECT(widgets->InstallTerminal),"child-exited",G_CALLBACK(on_install_check),driver); 
    yon_install_buttons_set_sensitive(widgets,0);
    yon_terminal_integrated_start_shell(widgets->InstallTerminal,yon_debug_output("%s\n",command),NULL,NULL);
    // g_signal_connect(G_OBJECT(widgets->InstallTerminal),"contents-changed",G_CALLBACK(on_terminal_check_progress),widgets);
    if (!gtk_widget_get_visible(widgets->TerminalPaned)){
        on_terminal_revealer_open(NULL,widgets);
    }
    g_timeout_add(500,(GSourceFunc)on_terminal_check_progress,widgets);
}

void on_install_check(GtkWidget *self,gint ,driver_struct *driver){
    main_window *widgets = g_object_get_data(G_OBJECT(self),"widgets");
    yon_packages_update();
    
    yon_driver_struct_update(widgets,driver);
    g_signal_handlers_disconnect_by_func(G_OBJECT(self),G_CALLBACK(on_install_check),driver);
}

 
void on_driver_child_ready(VteTerminal *terminal, GPid pid, GError *, gpointer ){

    if (!terminal) return;
    if (pid == -1) printf("Error\n\n\n");
    // else g_object_set_data(G_OBJECT(terminal),"pid",(void*)pid);
}