#include "ubl-settings-bootloader.h"



void yon_menu_open_submenu(yon_menu_window *window, yon_menu_item *item){
    int size;
    config_str options = yon_config_load(get_menus_entry_command,&size);
            bind_textdomain_codeset("ublinux-init-grub-functions","UTF-8");
    textdomain("ublinux-init-grub-functions");

    for (int i=1;i<size;i++){
        yon_char_remove_last_symbol(options[i],'\n');
        int parsed_size;
        config_str parsed = yon_char_parse(_(options[i]),&parsed_size,";");
        int id_size;
        config_str id_parsed = yon_char_parse(parsed[0],&id_size,">");
        int name_size;
        config_str name_parsed = yon_char_parse(_(parsed[1]),&name_size,">");
        if (yon_char_parsed_check_exist(id_parsed,id_size,item->id)==0){
            yon_menu_item *subitem = yon_menu_add_item(window,parsed[0],name_parsed[1],0);
            gtk_list_box_insert(GTK_LIST_BOX(window->ChildrenListBox),subitem->MenuItemRow,-1);
            gtk_widget_hide(subitem->ArrowImage);
            
        }
        yon_char_parsed_free(id_parsed,id_size);
        yon_char_parsed_free(name_parsed,name_size);
        yon_char_parsed_free(parsed,parsed_size);
    }
    textdomain(LocaleName);

}

void yon_menu_ungrab(yon_menu_window *window){
        GdkDisplay *display = gdk_display_get_default();
        GdkSeat *seat = gdk_display_get_default_seat(display);
        if (window->seat_grab)
            gdk_seat_ungrab(seat);
        else
            gtk_grab_remove(window->MenuWindow);
        gtk_widget_destroy(window->MenuWindow);
}

void on_menu_chosen(GtkWidget *, GtkListBoxRow* row, yon_menu_window *window){
    yon_menu_item *item = g_object_get_data(G_OBJECT(row),"yon_menu_item");
    if (item->children){
        yon_menu_open_submenu(window,item);
        gtk_widget_show(window->ChildrenRevealer);
        gtk_widget_hide(window->ItemsRevealer);
    gtk_revealer_set_reveal_child(GTK_REVEALER(window->ItemsRevealer),0);
    gtk_revealer_set_reveal_child(GTK_REVEALER(window->ChildrenRevealer),1);
    } else {
        gtk_entry_set_text(GTK_ENTRY(window->target),item->name);
        yon_config_register(GRUB_DEFAULT_parameter,GRUB_DEFAULT_command,item->id);
        yon_menu_ungrab(window);
    }

}

yon_menu_item *yon_menu_add_item(yon_menu_window *target_menu,const char *id, const char *name, int children){
    yon_menu_item *item = yon_menu_item_new();
    item->window=target_menu;
    item->children = children;
    item->id = yon_char_new(id);
    item->name = yon_char_new(name);
    gtk_label_set_text(GTK_LABEL(item->MenuTextLabel),item->name);

    // g_signal_connect(G_OBJECT(item->MenuItemRow),"clicked",G_CALLBACK(on_menu_chosen),item);

    g_object_set_data(G_OBJECT(item->MenuItemRow),"yon_menu_item",item);
    gtk_widget_show(item->MenuItemRow);
    return item;

}

yon_menu_window *yon_menu_window_open(GtkWidget *, main_window *widgets){
    
    yon_menu_window *window = yon_menu_window_new();
    window->target = widgets->OSDefaultEntry;
    g_signal_connect(G_OBJECT(window->ItemsListBox),"row-activated",G_CALLBACK(on_menu_chosen),window);
    g_signal_connect(G_OBJECT(window->ChildrenListBox),"row-activated",G_CALLBACK(on_menu_chosen),window);
    gtk_window_set_transient_for(GTK_WINDOW(window->MenuWindow),GTK_WINDOW(widgets->Window));
        {
            yon_menu_item *item = yon_menu_add_item(window,"",DEFAULT_MENU_ITEM_LABEL,0);
            gtk_list_box_insert(GTK_LIST_BOX(window->ItemsListBox),item->MenuItemRow,-1);
            gtk_widget_hide(item->ArrowImage);
        }

    int size;
    textdomain("ublinux-init-grub-functions");

    config_str options = yon_config_load(get_menus_entry_command,&size);
    GHashTable *created = g_hash_table_new(g_str_hash,g_str_equal);
    for (int i=1;i<size;i++){
        yon_char_remove_last_symbol(options[i],'\n');
        int parsed_size;
        config_str parsed = yon_char_parse(options[i],&parsed_size,";");
        {
            char *temp = _(parsed[1]);
            parsed[1] = yon_char_new(temp);
        }
        int have_children=0;
        char *children_id;
        if ((children_id=strstr(parsed[0],">"))){
            children_id[0]='\0';
            have_children=1;
        }
        char *children_name;
        if ((children_name=strstr(parsed[1],">"))){
            children_name[0]='\0';
            have_children=1;
        }

        if (!g_hash_table_contains(created,parsed[0])){
            g_hash_table_add(created,parsed[0]);
            yon_menu_item *item = yon_menu_add_item(window,parsed[0],_(parsed[1]),have_children);
            gtk_list_box_insert(GTK_LIST_BOX(window->ItemsListBox),item->MenuItemRow,-1);
            if (!have_children){
                gtk_widget_hide(item->ArrowImage);
            }
        }
    }
    textdomain(LocaleName);
    g_hash_table_unref(created);
    yon_char_parsed_free(options,size);
    gtk_window_set_transient_for(GTK_WINDOW(window->MenuWindow), GTK_WINDOW(widgets->Window));

    int x,y,width;
    gtk_widget_realize(window->MenuWindow);
    gdk_window_get_origin(gtk_widget_get_window(widgets->MenuButton),&x,&y);
    gdk_window_get_root_origin(gtk_widget_get_window(widgets->MenuButton),&x,&y);
    // window->button_pos_x += x;
    // window->button_pos_y += y;
    
    gtk_widget_translate_coordinates(widgets->MenuButton,widgets->Window,x,y,&window->button_pos_x,&window->button_pos_y);
    gtk_window_get_size(GTK_WINDOW(window->MenuWindow),&width,NULL);
    GtkAllocation alloc;
    gtk_widget_get_allocation(widgets->MenuButton, &alloc);
    gtk_window_get_position(GTK_WINDOW(widgets->Window),&x,&y);
    gtk_window_move(GTK_WINDOW(window->MenuWindow),window->button_pos_x-width,window->button_pos_y);
    gtk_widget_show(window->MenuWindow);
    GdkGrabStatus status = gdk_seat_grab(gdk_display_get_default_seat(gdk_display_get_default()),gtk_widget_get_window(window->MenuWindow),GDK_SEAT_CAPABILITY_POINTER,TRUE,NULL,NULL,NULL,NULL);
    if (status != GDK_GRAB_SUCCESS) {
        window->seat_grab=0;
        gtk_grab_add(window->MenuWindow);
    } else {
        window->seat_grab=1;
    }
    return window;
}

gboolean on_menu_clicked(GtkWidget *, GdkEventButton *event, yon_menu_window *window){
    int x,y,width,height;
    gtk_window_get_size(GTK_WINDOW(window->MenuWindow),&width,&height);
    gtk_window_get_position(GTK_WINDOW(window->MenuWindow),&x,&y);
    if (event->x_root<x||event->y_root<y||event->x_root>x+width||event->y_root>y+height){
        yon_menu_ungrab(window);
    }
    return 1;

}

gboolean yon_on_revealer_switched (yon_menu_window *window){
    if (gtk_revealer_get_reveal_child(GTK_REVEALER(window->ItemsRevealer))){
        gtk_widget_hide(window->ChildrenRevealer);
        gtk_widget_show(window->ItemsRevealer);
    } else {
        gtk_widget_hide(window->ItemsRevealer);
        gtk_widget_show(window->ChildrenRevealer);

    }
    return G_SOURCE_REMOVE;
}

void on_children_clean(GtkWidget*, yon_menu_window *window){
    GList *list = gtk_container_get_children(GTK_CONTAINER(window->ChildrenListBox));
    list = g_list_last(list);
    for (guint i=0;i<g_list_length(list);i++){
        GList *item = gtk_container_get_children(GTK_CONTAINER(list->data));
        g_object_ref(G_OBJECT(item->data));
        gtk_container_remove(GTK_CONTAINER(list->data),GTK_WIDGET(item->data));
        gtk_widget_destroy(GTK_WIDGET(list->data));
        g_list_free(item);
        list=list->prev;
    }

    gtk_revealer_set_reveal_child(GTK_REVEALER(window->ItemsRevealer),1);
    gtk_revealer_set_reveal_child(GTK_REVEALER(window->ChildrenRevealer),0);
    gtk_box_set_child_packing(GTK_BOX(gtk_widget_get_parent(window->ItemsRevealer)),window->ItemsRevealer,1,1,0,GTK_PACK_START);
    yon_on_revealer_switched(window);
    g_signal_connect(G_OBJECT(window->MenuWindow),"size-allocate",G_CALLBACK(on_menu_window_size_reallocated),window);
    gtk_box_set_child_packing(GTK_BOX(gtk_widget_get_parent(window->ChildrenRevealer)),window->ChildrenRevealer,0,0,0,GTK_PACK_START);

}

void on_menu_window_size_reallocated(GtkWidget *self,GtkAllocation *,yon_menu_window *window){
    int x,y,width;
    gtk_window_get_size(GTK_WINDOW(window->MenuWindow),&width,NULL);
    gtk_window_get_position(GTK_WINDOW(gtk_window_get_transient_for(GTK_WINDOW(window->MenuWindow))),&x,&y);
    gtk_window_move(GTK_WINDOW(window->MenuWindow),window->button_pos_x-width,window->button_pos_y);
    g_signal_handlers_disconnect_by_func(self,on_menu_window_size_reallocated,window);
}

 yon_menu_window *yon_menu_window_new(){
    yon_menu_window *window = new(yon_menu_window);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_menu_window);
    window->MenuWindow = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->ItemsRevealer = yon_gtk_builder_get_widget(builder,"ItemsRevealer");
    window->BackButton = yon_gtk_builder_get_widget(builder,"BackButton");
    window->ItemsListBox = yon_gtk_builder_get_widget(builder,"ItemsListBox");
    window->ChildrenRevealer = yon_gtk_builder_get_widget(builder,"ChildrenRevealer");
    window->ChildrenListBox = yon_gtk_builder_get_widget(builder,"ChildrenListBox");

    window->menu_items=NULL;

    gtk_window_set_decorated(GTK_WINDOW(window->MenuWindow),0);

    g_signal_connect(G_OBJECT(window->BackButton),"clicked",G_CALLBACK(on_children_clean),window);
    g_signal_connect(G_OBJECT(window->MenuWindow),"button-press-event",G_CALLBACK(on_menu_clicked),window);
    return window;
}

yon_menu_item *yon_menu_item_new(){
    yon_menu_item *item = new(yon_menu_item);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_menu_item);
    item->MenuItemRow = yon_gtk_builder_get_widget(builder,"MenuItemRow");
    item->MenuTextLabel = yon_gtk_builder_get_widget(builder,"MenuTextLabel");
    item->ArrowImage = yon_gtk_builder_get_widget(builder,"ArrowImage");

    return item;
}