#include "ubl-settings-video.h"

void on_devices_button_clicked(GtkWidget *, main_window *widgets){
    GtkWidget *selection = GTK_WIDGET(gtk_list_box_get_selected_row(GTK_LIST_BOX(widgets->DriversList)));
    if (selection){
        driver_struct *driver = g_object_get_data(G_OBJECT(selection),"driver_struct");
        on_devices_info_clicked(NULL,driver);
    }
}

void on_devices_info_clicked(GtkWidget *, driver_struct* driver){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_html);
    GtkWidget *MainWindow = yon_gtk_builder_get_widget(builder, "MainWindow");
    GtkWidget *HTMLView = yon_gtk_builder_get_widget(builder,"HTMLView");
    yon_gtk_window_setup(GTK_WINDOW(MainWindow),NULL,SUPPORTED_LABEL,icon_name,"html_window");
    int size;
    config_str fl = yon_resource_open_file(html_path(driver->name),&size);
    char *full_text = yon_char_parsed_to_string(fl,size,"\n");
    webkit_web_view_load_html(WEBKIT_WEB_VIEW(HTMLView),full_text,"");

    gtk_widget_show(MainWindow);
}

void yon_install_buttons_set_sensitive(main_window *widgets,int state){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->DriversList));
    for (GList *iter = list;iter;iter=iter->next){
        driver_struct *driver = g_object_get_data(G_OBJECT(iter->data),"driver_struct");
        gtk_widget_set_sensitive(driver->ModuleInstalledCheck,state);
        gtk_widget_set_sensitive(driver->PackageInstalledCheck,state);
    }
    g_list_free(list);
}

void yon_driver_struct_update(main_window *widgets,driver_struct *driver){
    driver->module_installed = yon_packages_check_installed(driver->module);
    driver->package_installed = yon_packages_check_installed(driver->package);
    gtk_label_set_text(GTK_LABEL(driver->KernelLabel),driver->kernel);
    gtk_label_set_text(GTK_LABEL(driver->DevicesLabel),driver->devices);
    gtk_label_set_text(GTK_LABEL(driver->DescriptionLabel),driver->description);
    gtk_widget_set_visible(driver->ModuleInstalledCheck,driver->module_exist);
    gtk_widget_set_visible(driver->PackageInstalledCheck,driver->package_exist);
    g_signal_handlers_block_by_func(G_OBJECT(driver->ModuleInstalledCheck),G_CALLBACK(on_install_activate),widgets);
    g_signal_handlers_block_by_func(G_OBJECT(driver->PackageInstalledCheck),G_CALLBACK(on_install_activate),widgets);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(driver->ModuleInstalledCheck),driver->module_installed);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(driver->PackageInstalledCheck),driver->package_installed);
    g_signal_handlers_unblock_by_func(G_OBJECT(driver->ModuleInstalledCheck),G_CALLBACK(on_install_activate),widgets);
    g_signal_handlers_unblock_by_func(G_OBJECT(driver->PackageInstalledCheck),G_CALLBACK(on_install_activate),widgets);
    int size;
    char *driver_link = NULL;
    config_str html = yon_resource_open_file(html_path(driver->name),&size);
    if (!size){
        driver_link = yon_char_new(driver->name);
        driver->supported_devices_avaliable=0;
    } else {
    driver_link = yon_char_unite("<a href = \"\">",driver->name,"</a>",NULL);
        yon_char_parsed_free(html,size);
        driver->supported_devices_avaliable=1;
    }

    gtk_label_set_markup(GTK_LABEL(driver->DriverLabel),driver_link);
    free(driver_link);
}

void on_link_activate(GtkWidget *, char*, driver_struct *driver){
    on_devices_info_clicked(NULL,driver);
}

void on_pacman_db_unlock(void){
    yon_launch(pacman_unlock_database_command);
    yon_ubl_status_box_render(yon_char_get_localised_from_lib(SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
}

void on_terminal_done(GtkWidget *,  int , 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);
    on_pacman_db_unlock();
    free(command);
    vte_terminal_feed_child(VTE_TERMINAL(widgets->InstallTerminal),tmp,strlen(tmp));
    free(tmp);
}

void on_terminal_more(GtkWidget *, main_window *widgets){
    int active = gtk_revealer_get_child_revealed(GTK_REVEALER(widgets->TerminalMoreRevieler));
    if (active ==1) active = 0;
    else active=1;
gtk_revealer_set_reveal_child(GTK_REVEALER(widgets->TerminalMoreRevieler),active);
}

gboolean on_command_execute_success(GtkWidget *self,gint status,main_window *widgets){
        yon_install_buttons_set_sensitive(widgets,1);
    vte_terminal_set_input_enabled(VTE_TERMINAL(self),0);
    g_mutex_lock(&main_config.progress_mutex);
    main_config.progress_active=0;
    g_mutex_unlock(&main_config.progress_mutex);
    if (!status){
        if (gtk_widget_get_visible(widgets->TerminalPaned)){
            on_terminal_revealer_open(NULL,widgets);
        }
        yon_ubl_status_box_render(yon_char_unite(yon_char_get_localised_from_lib(SUCCESS_LABEL),". ",PROPRIETARY_LOADING_LABEL,NULL),BACKGROUND_IMAGE_SUCCESS_TYPE);
    } else {
        if (!gtk_widget_get_visible(widgets->TerminalPaned)){
            on_terminal_revealer_open(NULL,widgets);
        }
        yon_ubl_status_box_render(yon_char_get_localised_from_lib(FAIL_LABEL),BACKGROUND_IMAGE_FAIL_TYPE);

    }
    while(gtk_events_pending()) gtk_main_iteration();
    on_terminal_done(self,0,widgets);
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widgets->TerminalInfoLabel),0);
    return G_SOURCE_REMOVE;
}

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->TerminalInfoLabel));
    g_mutex_unlock(&main_config.progress_mutex);
    return G_SOURCE_CONTINUE;
}

void on_command_cancel(GtkWidget *,main_window *widgets){
    g_mutex_lock(&main_config.progress_mutex);
    main_config.progress_active=0;
    g_mutex_unlock(&main_config.progress_mutex);
    on_terminal_done(NULL,0,widgets);

    yon_launch(pacman_unlock_database_command);
}

void on_driver_pack_selection_change(GtkWidget *, GtkListBoxRow *selection, main_window *widgets){
    if (selection){
        driver_struct *driver = g_object_get_data(G_OBJECT(selection),"driver_struct");
        if (driver->supported_devices_avaliable){
            gtk_widget_set_sensitive(widgets->SupportedDevicesButton,1);
        } else {
            gtk_widget_set_sensitive(widgets->SupportedDevicesButton,0);
        }
        if (!yon_char_is_empty(driver->package)&&yon_packages_check_exist(driver->package)){
            gtk_widget_set_sensitive(widgets->AboutPackageButton,1);
        } else {
            gtk_widget_set_sensitive(widgets->AboutPackageButton,0);
        }
        if (!yon_char_is_empty(driver->module)&&yon_packages_check_exist(driver->module)){
            gtk_widget_set_sensitive(widgets->AboutModuleButton,1);
        } else {
            gtk_widget_set_sensitive(widgets->AboutModuleButton,0);
        }
        
    } else {
        gtk_widget_set_sensitive(widgets->SupportedDevicesButton,0);
        gtk_widget_set_sensitive(widgets->AboutModuleButton,0);
        gtk_widget_set_sensitive(widgets->AboutPackageButton,0);
    }
}

gpointer yon_driver_info_launch(void *command){
    if (!system((char*)command)){
        yon_ubl_status_box_render_thread(yon_char_get_localised_from_lib(SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
    } else {
        yon_ubl_status_box_render_thread(yon_char_get_localised_from_lib(FAIL_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
    }
    return NULL;
}

void on_driver_pack_info(GtkWidget *, main_window *widgets){
    GtkWidget *selection = GTK_WIDGET(gtk_list_box_get_selected_row(GTK_LIST_BOX(widgets->DriversList)));
    if (selection){
        driver_struct *driver = g_object_get_data(G_OBJECT(selection),"driver_struct");
        if (!yon_char_is_empty(driver->package)){
            yon_package_info_window_open(driver->package);
        }
        
    } else {
        yon_ubl_status_box_render(PROPRIETARY_NOTHING_SELECTED_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
    }
}

void on_driver_module_info(GtkWidget *, main_window *widgets){
    GtkWidget *selection = GTK_WIDGET(gtk_list_box_get_selected_row(GTK_LIST_BOX(widgets->DriversList)));
    if (selection){
        driver_struct *driver = g_object_get_data(G_OBJECT(selection),"driver_struct");
        if (!yon_char_is_empty(driver->module)){
            yon_window *window = yon_package_info_window_open(driver->module);
            if (window){
                yon_gtk_window_setup(GTK_WINDOW(window->Window),NULL,_MODULE_INFO_LABEL,NULL,NULL);
            }
        }
    } else {
        yon_ubl_status_box_render(PROPRIETARY_NOTHING_SELECTED_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
    }
}

gboolean yon_filter_func(GtkTreeModel* model,GtkTreeIter* iter,main_window *widgets){
    const char *target = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widgets->KernelsCombo));
    char *cur;
    gtk_tree_model_get(model,iter,2,&cur,-1);
    if (!yon_char_is_empty(cur)&&!yon_char_is_empty(target)&&!strcmp(cur,target))return 1;
    return 0;
}

void on_kernel_filter_changed(GtkWidget *self, main_window *widgets){
    const char *active = gtk_combo_box_get_active_id(GTK_COMBO_BOX(self));
    if (!yon_char_is_empty(active)){
        GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->DriversList));
        for (GList *iter = list; iter; iter=iter->next){
            driver_struct *driver = g_object_get_data(G_OBJECT(iter->data),"driver_struct");
            if (!strcmp(driver->kernel,active)){
                gtk_widget_show(GTK_WIDGET(iter->data));
            } else {
                gtk_widget_hide(GTK_WIDGET(iter->data));
            }
        }
        g_list_free(list);
    } else {
        GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->DriversList));
        for (GList *iter = list; iter; iter=iter->next){
            gtk_widget_show(GTK_WIDGET(iter->data));
        }
        g_list_free(list);
    }
}

gboolean yon_proprietary_clear(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->DriversList));
    GList *iter = NULL;
    for (iter = list; iter; iter=iter->next){
        driver_struct *driver = g_object_get_data(G_OBJECT(iter->data),"driver_struct");
        free(driver->name);
        free(driver->module);
        free(driver->package);
        free(driver->kernel);
        free(driver->description);
        free(driver->devices);
        gtk_widget_destroy(GTK_WIDGET(iter->data));
    }
    return G_SOURCE_REMOVE;
}

void yon_drivers_resize(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->DriversList));
    GList *iter;
    int module_width=0;
    int package_width=0;
    int kernel_width=0;
    int driver_width=0;
    int description_width=0;
    gtk_widget_get_preferred_width(widgets->InstalledModuleLabel,&module_width,NULL);
    gtk_widget_get_preferred_width(widgets->InstalledPackageLabel,&package_width,NULL);
    gtk_widget_get_preferred_width(widgets->KernelLabel,&kernel_width,NULL);
    gtk_widget_get_preferred_width(widgets->DriverLabel,&driver_width,NULL);
    gtk_widget_get_preferred_width(widgets->DescriptionLabel,NULL,&description_width);
    for (iter = list;iter;iter=iter->next){
        driver_struct *data = g_object_get_data(G_OBJECT(iter->data),"driver_struct");
        gtk_widget_realize(data->ModuleInstalledCheck);
        gtk_widget_realize(data->PackageInstalledCheck);
        gtk_widget_realize(data->KernelLabel);
        gtk_widget_realize(data->DriverLabel);
        gtk_widget_realize(data->DescriptionLabel);
        gtk_widget_realize(data->DevicesBox);
        int temp_width = 0;
        gtk_widget_get_preferred_width(data->ModuleInstalledCheck,&temp_width,NULL);
        if (temp_width > module_width) module_width=temp_width;
        gtk_widget_get_preferred_width(data->PackageInstalledCheck,&temp_width,NULL);
        if (temp_width > package_width) package_width=temp_width;
        gtk_widget_get_preferred_width(data->KernelLabel,&temp_width,NULL);
        if (temp_width > kernel_width) kernel_width=temp_width;
        gtk_widget_get_preferred_width(data->DriverLabel,&temp_width,NULL);
        if (temp_width > driver_width) driver_width=temp_width;
        gtk_widget_get_preferred_width(data->DescriptionLabel,NULL,&temp_width);
        if (temp_width > description_width) description_width=temp_width;
    }
    if (module_width||package_width||kernel_width||driver_width||description_width){
        for (iter = list;iter;iter=iter->next){
        driver_struct *data = g_object_get_data(G_OBJECT(iter->data),"driver_struct");
        if (module_width)
            gtk_widget_set_size_request(gtk_widget_get_parent(data->ModuleInstalledCheck),module_width,-1);
        if (package_width)
            gtk_widget_set_size_request(gtk_widget_get_parent(data->PackageInstalledCheck),package_width,-1);
        if (kernel_width)
            gtk_widget_set_size_request(gtk_widget_get_parent(data->KernelLabel),kernel_width,-1);
        if (driver_width)
            gtk_widget_set_size_request(gtk_widget_get_parent(data->DriverLabel),driver_width,-1);
        if (description_width)
            gtk_widget_set_size_request(gtk_widget_get_parent(data->DescriptionLabel),description_width,-1);
        }
        g_list_free(list);
        if (module_width) gtk_widget_set_size_request(widgets->InstalledModuleLabel,module_width,-1);
        if (package_width) gtk_widget_set_size_request(widgets->InstalledPackageLabel,package_width,-1);
        if (kernel_width) gtk_widget_set_size_request(widgets->KernelLabel,kernel_width,-1);
        if (driver_width) gtk_widget_set_size_request(widgets->DriverLabel,driver_width,-1);
        if (description_width) gtk_widget_set_size_request(widgets->DescriptionLabel,description_width,-1);
    }
}

gboolean yon_proprietary_set(main_window *widgets){
    gtk_widget_set_sensitive(widgets->LoadDriversButton,1);
    return G_SOURCE_REMOVE;
}

gboolean yon_proprietary_append_kernels(struct proprietary_kernel_append_struct *append){
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(append->widgets->KernelsCombo),append->kernel,append->kernel);
    free(append->kernel);
    free(append);
    return G_SOURCE_REMOVE;
}

driver_struct *yon_driver_struct_new(){
    driver_struct *driver = new(driver_struct);

    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_driver);
    driver->MainBox = yon_gtk_builder_get_widget(builder,"MainBox");
    driver->ModuleInstalledCheck = yon_gtk_builder_get_widget(builder,"ModuleInstalledCheck");
    driver->PackageInstalledCheck = yon_gtk_builder_get_widget(builder,"PackageInstalledCheck");
    driver->KernelLabel = yon_gtk_builder_get_widget(builder,"KernelLabel");
    driver->DriverLabel = yon_gtk_builder_get_widget(builder,"DriverLabel");
    driver->DescriptionLabel = yon_gtk_builder_get_widget(builder,"DescriptionLabel");
    driver->DevicesBox = yon_gtk_builder_get_widget(builder,"DevicesBox");
    driver->DevicesLabel = yon_gtk_builder_get_widget(builder,"DevicesLabel");

    driver->name = NULL;
    driver->kernel = NULL;
    driver->module = NULL;
    driver->package = NULL;
    driver->module_installed = 0;
    driver->package_installed = 0;
    driver->module_exist = 0;
    driver->package_exist = 0;
    driver->supported_devices_avaliable=0;

    g_object_set_data(G_OBJECT(driver->ModuleInstalledCheck),"driver_struct",driver);
    g_object_set_data(G_OBJECT(driver->PackageInstalledCheck),"driver_struct",driver);

    g_signal_connect(G_OBJECT(driver->DriverLabel),"activate-link", G_CALLBACK(on_link_activate),driver);
    return driver;
}