#include "ubl-settings-manager.h"

typedef struct {
    GtkWidget *expander;
    GtkWidget *MainLabel;
    GtkWidget *AppsList;
    app_section *section;
} main_section;

void yon_main_section_setup_apps(main_section *cur_section, const char *target);
main_section *yon_main_section_new();
void yon_main_section_get_max_size(main_section *section, int *ret_width, int *ret_height);

void on_main_plug_connected(GtkWidget *,main_theme_struct *theme){
    gtk_widget_show(theme->SocketBox);
    gtk_widget_hide(main_config.widgets->SettingsButton);
    gtk_widget_hide(theme->HideBox);
}

void on_main_plug_disconnected(GtkWidget *,main_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_show(main_config.widgets->SettingsButton);
    gtk_widget_set_sensitive(theme->HideBox,1);
}

gboolean on_main_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_show(main_config.widgets->SettingsButton);
        gtk_widget_hide(theme->SocketBox);
    if (!active){
        on_plug_disconnected(GTK_SOCKET(theme->Socket),main_config.widgets);
        return 0;
    } else {
        gtk_widget_set_sensitive(theme->HideBox,0);
        gtk_widget_hide(main_config.widgets->BackButton);
    }
    return 1;
}

void on_main_selected(GtkWidget* self, main_theme_struct *theme);
void on_main_selected(GtkWidget* self, main_theme_struct *theme){
    GList *list = gtk_container_get_children(GTK_CONTAINER(theme->AppsTree));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        main_section *cur_section = g_object_get_data(G_OBJECT(iter->data),"main_section");
        if (cur_section->AppsList != self){
            g_signal_handlers_block_by_func(G_OBJECT(cur_section->AppsList),G_CALLBACK(on_main_selected),theme);
            GList *flowlist = gtk_flow_box_get_selected_children(GTK_FLOW_BOX(cur_section->AppsList));
            if (flowlist){
                gtk_flow_box_unselect_child(GTK_FLOW_BOX(cur_section->AppsList),GTK_FLOW_BOX_CHILD(flowlist->data));
                g_list_free(flowlist);
            }
            g_signal_handlers_unblock_by_func(G_OBJECT(cur_section->AppsList),G_CALLBACK(on_main_selected),theme);
        }
    }
}

void on_main_activate(GtkFlowBox* , GtkFlowBoxChild* child, main_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_main_plug_connected),theme);
        g_signal_connect(G_OBJECT(theme->Socket),"plug-removed",G_CALLBACK(on_main_plug_disconnect_process),theme);
        g_signal_connect(G_OBJECT(theme->Socket),"destroy",G_CALLBACK(on_main_plug_disconnected),theme);
    }
    apps *cur_app = g_object_get_data(G_OBJECT(child),"apps");
    char *command = yon_get_launch_command(cur_app->Desktop_path);
    if (yon_char_is_empty(command)) return;
    yon_debug_output("%s\n",command);
    g_thread_new("application_thread",(GThreadFunc)yon_app_start,command);
}

// void on_main_socket_add(){

// }

void yon_main_section_setup_apps(main_section *cur_section, const char *target){
    if (yon_char_is_empty(target)) return;
    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));

    }
    char *name = cur_app->Name;

    GtkWidget *Row = gtk_flow_box_child_new();
    GtkWidget *Box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
    GtkWidget *Label = gtk_label_new(name);
    GtkWidget *Icon = Image;

    gtk_box_pack_start(GTK_BOX(Box),Icon,0,0,0);
    gtk_box_pack_start(GTK_BOX(Box),Label,1,1,0);
    gtk_container_add(GTK_CONTAINER(Row),Box);
    gtk_flow_box_insert(GTK_FLOW_BOX(cur_section->AppsList),Row,-1);

    gtk_widget_set_size_request(Row,50,50);

    gtk_widget_set_halign(Box,GTK_ALIGN_FILL);
    gtk_widget_set_valign(Box,GTK_ALIGN_CENTER);

    gtk_label_set_line_wrap_mode(GTK_LABEL(Label),PANGO_WRAP_WORD_CHAR);
    gtk_label_set_line_wrap(GTK_LABEL(Label),1);
    gtk_label_set_max_width_chars(GTK_LABEL(Label),15);
    gtk_label_set_xalign(GTK_LABEL(Label), GTK_ALIGN_FILL);

    g_object_set_data(G_OBJECT(Row),"main_section",cur_section);
    g_object_set_data(G_OBJECT(Row),"apps",cur_app);
    g_object_set_data(G_OBJECT(Row),"Label",Label);

    gtk_style_context_add_class(gtk_widget_get_style_context(Row),"bggrey");

    gtk_widget_show_all(Row);
}

main_section *yon_main_section_new(){
    main_section *cur_section = malloc(sizeof(main_section));
    memset(cur_section,0,sizeof(main_section));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_main_section);
    cur_section->expander = yon_gtk_builder_get_widget(builder,"MainExpander");
    cur_section->MainLabel = yon_gtk_builder_get_widget(builder,"MainLabel");
    cur_section->AppsList = yon_gtk_builder_get_widget(builder,"AppsFlow");
    g_object_set_data(G_OBJECT(cur_section->expander),"main_section",cur_section);
    int doubleclick=0;
    if (!yon_window_config_get_parameter(settings_section,double_click_parameter,&doubleclick,YON_TYPE_BOOLEAN)){
        gtk_flow_box_set_activate_on_single_click(GTK_FLOW_BOX(cur_section->AppsList),0);
    } else {
        gtk_flow_box_set_activate_on_single_click(GTK_FLOW_BOX(cur_section->AppsList),!doubleclick);
    }
    return cur_section;
}

void yon_main_section_setup(main_theme_struct *theme, app_section *section){
    main_section *cur_section = yon_main_section_new();
    cur_section->section = section;
    gtk_label_set_text(GTK_LABEL(cur_section->MainLabel),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_main_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),"main_section",cur_section);
    g_signal_connect(G_OBJECT(cur_section->AppsList),"child-activated",G_CALLBACK(on_main_activate),theme);
    g_signal_connect(G_OBJECT(cur_section->AppsList),"selected-children-changed",G_CALLBACK(on_main_selected),theme);

}

void yon_main_section_get_max_size(main_section *section, int *ret_width, int *ret_height){
    GList *list = gtk_container_get_children(GTK_CONTAINER(section->AppsList));
    GList *iter;
    for (iter = list; iter;iter=iter->next){
        gtk_widget_realize(GTK_WIDGET(iter->data));
        int width;
        int height;
            GtkWidget *Label = g_object_get_data(G_OBJECT(iter->data),"Label");

        gtk_widget_get_preferred_width(Label,NULL,&width);
        gtk_widget_get_preferred_height(Label,NULL,&height);
        if ((*ret_width)<width) (*ret_width) = width;
        if ((*ret_height)<height) (*ret_height) = height;
    }
}

void yon_main_theme_resize(main_theme_struct *theme){
    GList *list = gtk_container_get_children(GTK_CONTAINER(theme->AppsTree));
    GList *iter;
    int max_width=0;
    int max_height=0;
    for (iter=list;iter;iter=iter->next){
        main_section *section = g_object_get_data(G_OBJECT(iter->data),"main_section");
        yon_main_section_get_max_size(section,&max_width,&max_height);
    }
    for (iter=list;iter;iter=iter->next){
        main_section *section = g_object_get_data(G_OBJECT(iter->data),"main_section");
        GList *child_list = gtk_container_get_children(GTK_CONTAINER(section->AppsList));
        if (child_list){
            GtkWidget *Label = g_object_get_data(G_OBJECT(child_list->data),"Label");
            gtk_widget_set_size_request(Label,max_width,max_height);
        }
    }
}

int yon_main_update(main_theme_struct *theme){
    GList *list = gtk_container_get_children(GTK_CONTAINER(theme->AppsTree));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        gtk_widget_destroy(GTK_WIDGET(iter->data));
    }

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

    yon_main_theme_resize(theme);
    return 1;
}

main_theme_struct *yon_main_theme_new(){
    main_theme_struct *theme = malloc(sizeof(main_theme_struct));

    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_main_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->HideBox = yon_gtk_builder_get_widget(builder,"HideBox");
    theme->theme_name = yon_char_new(MAIN_THEME_LABEL);
    theme->Socket = NULL;
    theme->list_update_func = (int(*)(struct main_theme_struct*))yon_main_update;
    
    return theme;
}
