#include "ubl-settings-manager.h"

typedef struct {
    GtkWidget *expander;
    GtkWidget *AppsList;
    app_section *section;
} gnome_section;

gnome_section *yon_gnome_section_new();
void yon_gnome_section_setup_apps(gnome_section *cur_section, const char *target);
void yon_gnome_section_search_show(gnome_section *section, const char *string);

void on_gnome_plug_connected(GtkWidget *,gnome_theme_struct *theme){
    gtk_widget_show(theme->SocketBox);
    gtk_widget_set_sensitive(((gnome_theme_struct*)theme)->GnomePaned,1);
    gtk_widget_hide(theme->HideBox);
    gtk_widget_hide(main_config.widgets->SettingsButton);
}

void on_gnome_plug_disconnected(GtkWidget *,gnome_theme_struct *theme){
    gtk_widget_show(theme->HideBox);
    gtk_widget_hide(theme->SocketBox);
    on_plug_disconnected(GTK_SOCKET(theme->Socket),main_config.widgets);
    gtk_widget_set_sensitive(theme->HideBox,1);
    gtk_widget_show(main_config.widgets->SettingsButton);
}

gboolean on_gnome_plug_disconnect_process(GtkWidget *,main_theme_struct *theme);
gboolean on_gnome_plug_disconnect_process(GtkWidget *,main_theme_struct *theme){
    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(theme->HideBox);
        gtk_widget_hide(theme->SocketBox);
    if (!active){
        on_plug_disconnected(GTK_SOCKET(theme->Socket),main_config.widgets);
        return 0;
    } else {
        gtk_widget_set_sensitive(((gnome_theme_struct*)theme)->GnomePaned,0);
        gtk_widget_hide(main_config.widgets->BackButton);
        gtk_widget_show(main_config.widgets->SettingsButton);
    }
    return 1;
}

void on_gnome_activate(GtkWidget *,GtkListBoxRow *self, gnome_theme_struct *theme);
void on_gnome_activate(GtkWidget *,GtkListBoxRow *self, gnome_theme_struct *theme){
    char *env = NULL;
    int sockets_allow = yon_environment_get_env_for_launch(&env);
    if (!yon_char_is_empty(env)) free(env);
    if (sockets_allow&&!main_config.disable_socket){
        theme->Socket = GTK_WIDGET(yon_sockets_init(GTK_BOX(theme->SocketBox)));
        g_signal_connect(G_OBJECT(theme->Socket),"plug-added",G_CALLBACK(on_gnome_plug_connected),theme);
        g_signal_connect(G_OBJECT(theme->Socket),"plug-removed",G_CALLBACK(on_gnome_plug_disconnect_process),theme);
        g_signal_connect(G_OBJECT(theme->Socket),"destroy",G_CALLBACK(on_gnome_plug_disconnected),theme);
    }
    apps *cur_app = g_object_get_data(G_OBJECT(self),"apps");
    char *command = yon_get_launch_command(cur_app->Desktop_path);
    yon_debug_output("%s ",command);
    if (yon_char_is_empty(command)) return;
    g_thread_new("application_thread",(GThreadFunc)yon_app_start,command);
}

void on_gnome_selected(GtkWidget *self_list,GtkListBoxRow *, gnome_theme_struct *theme);
void on_gnome_selected(GtkWidget *self_list,GtkListBoxRow *, gnome_theme_struct *theme){
    GList *list = gtk_container_get_children(GTK_CONTAINER(theme->AppsTree));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        gnome_section *cur_section = g_object_get_data(G_OBJECT(iter->data),"gnome_section");
        if (cur_section->AppsList != self_list){
            g_signal_handlers_block_by_func(G_OBJECT(cur_section->AppsList),G_CALLBACK(on_gnome_selected),theme);
            gtk_list_box_unselect_all(GTK_LIST_BOX(cur_section->AppsList));
            g_signal_handlers_unblock_by_func(G_OBJECT(cur_section->AppsList),G_CALLBACK(on_gnome_selected),theme);
        }
    }
}


gnome_section *yon_gnome_section_new(){
    gnome_section *cur_section = malloc(sizeof(gnome_section));
    memset(cur_section,0,sizeof(gnome_section));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_gnome_section);
    cur_section->expander = yon_gtk_builder_get_widget(builder,"MainExpander");
    cur_section->AppsList = yon_gtk_builder_get_widget(builder,"AppsList");
    int doubleclick=0;
    if (!yon_window_config_get_parameter(settings_section,double_click_parameter,&doubleclick,YON_TYPE_BOOLEAN)){
        gtk_list_box_set_activate_on_single_click(GTK_LIST_BOX(cur_section->AppsList),0);

    } else {
        gtk_list_box_set_activate_on_single_click(GTK_LIST_BOX(cur_section->AppsList),!doubleclick);
    }
    return cur_section;
}

void yon_gnome_section_setup_apps(gnome_section *cur_section, const char *target){
    apps *cur_app = yon_apps_get((char*)target);
    {
        if (cur_section->section->categories_size==1&&!strcmp(cur_section->section->categories[0],"X-UBL-SettingsManager")) {
            int found_else = 0;
            for (int i=0;i<cur_app->categories_size;i++){
                if (strstr(cur_app->Categories[i],"X-UBL-")&&strcmp(cur_app->Categories[i],"X-UBL-SettingsManager")){
                    found_else = 1;
                }
            }

            if (found_else) return;
        }
    }

    GtkIconInfo *info = gtk_icon_theme_lookup_icon_for_scale(gtk_icon_theme_get_default(), !yon_char_is_empty(cur_app->Icon)?cur_app->Icon:icon_path, main_config.apps_icon_size,1,GTK_ICON_LOOKUP_FORCE_SIZE);
    GtkWidget *Image = NULL;
    if (info){
        Image = gtk_image_new_from_pixbuf(gtk_icon_info_load_icon(info,NULL));
    } else {
    info = gtk_icon_theme_lookup_icon_for_scale(gtk_icon_theme_get_default(), icon_path, main_config.apps_icon_size,1,GTK_ICON_LOOKUP_FORCE_SIZE);
        Image = gtk_image_new_from_pixbuf(gtk_icon_info_load_icon(info,NULL));

    }
    GtkWidget *Label = gtk_label_new(cur_app->Name);
    GtkWidget *Row = gtk_list_box_row_new();
    GtkWidget *Box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
    
    gtk_container_add(GTK_CONTAINER(Row),Box);
    gtk_box_pack_start(GTK_BOX(Box),Image,0,0,0);
    gtk_box_pack_start(GTK_BOX(Box),Label,0,0,0);
    gtk_label_set_xalign(GTK_LABEL(Label),0);
    gtk_label_set_line_wrap(GTK_LABEL(Label),1);
    gtk_label_set_line_wrap_mode(GTK_LABEL(Label),PANGO_WRAP_WORD);
    gtk_list_box_insert(GTK_LIST_BOX(cur_section->AppsList),Row,-1);

    g_object_set_data(G_OBJECT(Row),"gnome_section",cur_section);
    g_object_set_data(G_OBJECT(Row),"apps",cur_app);

    gtk_widget_show_all(Row);
}

void yon_gnome_section_setup(gnome_theme_struct *theme,app_section *section){
    gnome_section *cur_section = yon_gnome_section_new();
    cur_section->section = section;
    gtk_expander_set_label(GTK_EXPANDER(cur_section->expander),section->name);
    int app_size;
    config_str app_list = yon_apps_get_by_categories(section->categories,section->categories_size,&app_size);
    for(int i=0;i<app_size;i++){
        yon_gnome_section_setup_apps(cur_section,app_list[i]);
    }
    gtk_box_pack_start(GTK_BOX(theme->AppsTree),cur_section->expander,0,0,0);
    g_object_set_data(G_OBJECT(cur_section->expander),"gnome_section",cur_section);
    g_signal_connect(G_OBJECT(cur_section->AppsList),"row-activated",G_CALLBACK(on_gnome_activate),theme);
    g_signal_connect(G_OBJECT(cur_section->AppsList),"row-selected",G_CALLBACK(on_gnome_selected),theme);
}

int yon_gnome_update(gnome_theme_struct *theme){
    if (!main_config.sections) return 0;
    GList *list = gtk_container_get_children(GTK_CONTAINER(theme->AppsTree));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        gnome_section *cur_section = g_object_get_data(G_OBJECT(iter->data),"gnome_section");
        gtk_widget_destroy(cur_section->expander);
        free(cur_section);
    }

    dictionary *cur;
    for_dictionaries(cur,main_config.sections){
        app_section *section_data = yon_dictionary_get_data(cur,app_section*);
        yon_gnome_section_setup(theme,section_data);
    }
    return 1;
}

void yon_gnome_section_search_show(gnome_section *section, const char *string){
    GList *list = gtk_container_get_children(GTK_CONTAINER(section->AppsList));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        apps *cur_app = g_object_get_data(G_OBJECT(iter->data),"apps");
        if (yon_char_is_empty(string)||yon_char_check_begins_with(cur_app->keywords,(char*)string)){
            gtk_widget_show(GTK_WIDGET(iter->data));
        } else {
            gtk_widget_hide(GTK_WIDGET(iter->data));
        }
    }
    g_list_free(list);
}

void on_gnome_search(GtkWidget *, gnome_theme_struct *theme){
    const char *search_string = gtk_entry_get_text(GTK_ENTRY(theme->SearchEntry));
    GList *list = gtk_container_get_children(GTK_CONTAINER(theme->AppsTree));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        gnome_section *cur_section = g_object_get_data(G_OBJECT(iter->data),"gnome_section");
        yon_gnome_section_search_show(cur_section,search_string);
    }
    g_list_free(list);
}

gnome_theme_struct *yon_gnome_theme_new(){
    gnome_theme_struct *theme = malloc(sizeof(gnome_theme_struct));

    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_gnome_theme);
    theme->MainBox = yon_gtk_builder_get_widget(builder,"MainBox");
    theme->AppsTree = yon_gtk_builder_get_widget(builder,"AppsTree");
    theme->SocketBox = yon_gtk_builder_get_widget(builder,"SocketBox");
    theme->GnomePaned = yon_gtk_builder_get_widget(builder,"GnomePaned");
    theme->SearchEntry = yon_gtk_builder_get_widget(builder,"SearchEntry");
    theme->GnomeInfoLogoImage = yon_gtk_builder_get_widget(builder,"GnomeInfoLogoImage");
    theme->HideBox = yon_gtk_builder_get_widget(builder,"HideBox");
    theme->IconCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"IconCell"));
    theme->theme_name = yon_char_new(GNOME_THEME_LABEL);
    theme->Socket = gtk_socket_new();
    theme->list_update_func = (int(*)(struct gnome_theme_struct*))yon_gnome_update;

    gtk_box_pack_start(GTK_BOX(theme->SocketBox),theme->Socket,1,1,0);
    gtk_widget_show(theme->Socket);
    
    g_signal_connect(G_OBJECT(theme->SearchEntry),"changed",G_CALLBACK(on_gnome_search),theme);
    // gtk_cell_renderer_set_fixed_size(theme->IconCell,main_config.apps_icon_size,main_config.apps_icon_size);
    return theme;
}
