#include "libublsettings-gtk3.h"

// rmb menu section
typedef int (*RmbCheck)(void*);

gboolean on_rmb_menu_open(GtkWidget *,GdkEventButton *event, rmb_menu_window *window){
    int (*func)(void*) = (RmbCheck)(window->show_function);
    if (!func||func(window->show_data)){
        if (event->button==3){

            gtk_widget_show_all(window->menu);
            gtk_menu_popup_at_pointer(GTK_MENU(window->menu),NULL);
            return 0;
        }
        return 0;
    }
}

rmb_menu_window *yon_rmb_menu_new(){
    rmb_menu_window *window = malloc(sizeof(rmb_menu_window));
    window->menu=gtk_menu_new();
    gtk_menu_set_reserve_toggle_size(GTK_MENU(window->menu),0);
    window->buttons=NULL;

    return window;
}

rmb_menu_window *yon_rmb_menu_setup(GtkWidget *target_widget, int (show_function)(void*), void *show_data, const char *button_label, const char *icon_name, GCallback function, gpointer data,...){
    if (target_widget){
        rmb_menu_window *window = yon_rmb_menu_new();
        window->show_function=(GCallback)show_function;
        window->show_data=show_data;
        {
            GtkWidget *menu_item = gtk_menu_item_new();
            GtkWidget *content_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
            gtk_container_add(GTK_CONTAINER(menu_item),content_box);

            GtkWidget *image=NULL;
            if (!yon_char_is_empty(icon_name)){
                image=gtk_image_new_from_icon_name(icon_name,GTK_ICON_SIZE_BUTTON);
                gtk_box_pack_start(GTK_BOX(content_box),image,0,0,0);
                gtk_widget_show(image);
            }

            GtkWidget *label = gtk_label_new(button_label);
            gtk_box_pack_start(GTK_BOX(content_box),label,0,0,0);
            g_object_set_data(G_OBJECT(menu_item),"Label",label);

            if (function){
                g_signal_connect(G_OBJECT(menu_item),"activate",function,data);
            } else {
                gtk_widget_set_sensitive(menu_item,0);
            }
            yon_dictionary_add_or_create_if_exists_with_data(window->buttons,(char*)button_label,menu_item);
            gtk_menu_shell_append(GTK_MENU_SHELL(window->menu),menu_item);
        }
        va_list list;
        va_start(list,data);
        const char *current_label, *current_icon;
        GCallback current_function;
        gpointer *current_data;
        while ((current_label=va_arg(list,const char*))){
            if (current_label){
                current_icon=va_arg(list,const char*);
                current_function=va_arg(list,GCallback);
                current_data=va_arg(list,gpointer);
                GtkWidget *menu_item = gtk_menu_item_new();
                GtkWidget *content_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
                gtk_container_add(GTK_CONTAINER(menu_item),content_box);
                
                GtkWidget *image=NULL;
                if (!yon_char_is_empty(current_icon)){
                    image=gtk_image_new_from_icon_name(current_icon,GTK_ICON_SIZE_BUTTON);
                    gtk_box_pack_start(GTK_BOX(content_box),image,0,0,0);
                    gtk_widget_show(image);
                }

                GtkWidget *label = gtk_label_new(current_label);
                gtk_box_pack_start(GTK_BOX(content_box),label,0,0,0);

                if (current_function){
                    g_signal_connect(G_OBJECT(menu_item),"activate",current_function,current_data);
                } else {
                    gtk_widget_set_sensitive(menu_item,0);
                }
                yon_dictionary_add_or_create_if_exists_with_data(window->buttons,(char*)current_label,menu_item);
                gtk_menu_shell_append(GTK_MENU_SHELL(window->menu),menu_item);
                gtk_widget_show(menu_item);
            }
        }
        g_signal_connect(G_OBJECT(target_widget),"button-press-event",G_CALLBACK(on_rmb_menu_open),window);
        return window;
    }
}