#include "libublsettings-gtk3.h"


void yon_ubl_setup_sockets(GtkWidget *main_window, GtkWidget *left_window, GtkWidget *right_window, int socket_main_id, int socket_left_id, int socket_right_id){
    if (main_window&&socket_main_id>-1){
        if (!GDK_IS_X11_DISPLAY(gdk_display_get_default())){
            return;
        }
        GtkWidget *plug_main=gtk_plug_new(socket_main_id);
        printf("%p\n\n\n",plug_main);
        gtk_widget_hide(gtk_widget_get_toplevel(main_window));
        GtkWidget *plug_left=NULL;
        GtkWidget *plug_right=NULL;
        GtkWidget *box=NULL;
        g_signal_connect(G_OBJECT(plug_main), "destroy", G_CALLBACK(gtk_main_quit),NULL);
        if (socket_left_id>-1&&left_window){
            plug_left=gtk_plug_new(socket_left_id);
            g_object_ref(left_window);
            gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(left_window)),left_window);
            gtk_container_add(GTK_CONTAINER(plug_left),left_window);
        gtk_style_context_add_class(gtk_widget_get_style_context(plug_left),"primary-toolbar");
        gtk_style_context_add_class(gtk_widget_get_style_context(left_window),"button");
        gtk_style_context_add_class(gtk_widget_get_style_context(left_window),"opacited");
        gtk_style_context_add_class(gtk_widget_get_style_context(left_window),"color");
        gtk_style_context_add_class(gtk_widget_get_style_context(plug_left),"noborder");
            gtk_widget_show(plug_left);
        }
        else if (left_window){
            if (box==NULL){
                box=gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
                gtk_box_pack_start(GTK_BOX(main_window),box,0,0,5);
                gtk_box_reorder_child(GTK_BOX(main_window),box,0);
                gtk_widget_show(box);
            }
            gtk_style_context_add_class(gtk_widget_get_style_context(left_window),"inherited");
            gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(left_window)),left_window);
            gtk_box_pack_start(GTK_BOX(box),left_window,0,0,5);
        }
        if (socket_right_id>-1&&right_window){
            plug_right=gtk_plug_new(socket_right_id);
            g_object_ref(right_window);
            gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(right_window)),right_window);
            gtk_container_add(GTK_CONTAINER(plug_right),right_window);
        gtk_style_context_add_class(gtk_widget_get_style_context(plug_right),"primary-toolbar");
        gtk_style_context_add_class(gtk_widget_get_style_context(right_window),"button");
        gtk_style_context_add_class(gtk_widget_get_style_context(right_window),"opacited");
        gtk_style_context_add_class(gtk_widget_get_style_context(right_window),"color");
        gtk_style_context_add_class(gtk_widget_get_style_context(plug_right),"noborder");
            gtk_widget_show(plug_right);
        }
        else if (right_window){
            if (box==NULL){
                box=gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
                gtk_box_pack_start(GTK_BOX(main_window),box,0,0,5);
                gtk_box_reorder_child(GTK_BOX(main_window),box,0);
                gtk_widget_show(box);
            }
            gtk_style_context_add_class(gtk_widget_get_style_context(right_window),"inherited");
            gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(right_window)),right_window);
            gtk_box_pack_end(GTK_BOX(box),right_window,0,0,5);
        }
        g_object_ref(main_window);
        gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(main_window)),main_window);
        gtk_container_add(GTK_CONTAINER(plug_main),main_window);
        gtk_widget_show(plug_main);
    }
}