#include "ubl-panel-ubmanager.h"

config main_config;

#ifdef WEBKIT_INCLUDE

void on_browser(GtkWidget* self){
    char *command=yon_char_unite("xdg-open ",ConnectionAdress," &",NULL);
    yon_launch(command);
    gtk_widget_destroy(gtk_widget_get_toplevel(self));
    free(command);
    gtk_main_quit();
    exit(0);
}
    void on_browser_failed(GtkWidget* self,  WebKitLoadEvent ,  gchar* ,  GError* error, main_window *widgets){
        if (error->code==4){
            printf("Error code 4\n");
            yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),ERROR_LABEL(error->message),0,BACKGROUND_IMAGE_FAIL_TYPE);
            gtk_style_context_add_class(gtk_widget_get_style_context(widgets->StatusBox),"boxInfoMessError");
            gtk_widget_hide(widgets->WebView);
        } else {
            printf("Error message %s\n",error->message);
            on_browser(self);
        }
    }

    void on_browser_tls(WebKitWebView *, GTlsCertificate *, GTlsCertificateFlags, WebKitTLSErrorsPolicy *policy);
    void on_browser_tls(WebKitWebView *, GTlsCertificate *, GTlsCertificateFlags, WebKitTLSErrorsPolicy *policy){
        *policy = WEBKIT_TLS_ERRORS_POLICY_IGNORE;
    }

main_window *yon_main_window_complete(main_window *widgets){
    printf("Entered window\n");
    widgets = yon_remalloc(widgets,sizeof(main_window)); 
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path); 
    gtk_box_pack_start(GTK_BOX(widgets->InterfaceBox),yon_gtk_builder_get_widget(builder,"BoxMain"),1,1,0); 
    // Custom widgets configuration 
    {
        widgets->WebView = yon_gtk_builder_get_widget(builder,"WebView");
        widgets->BrowserButton = yon_gtk_builder_get_widget(builder,"OpenBrowserButton");

        g_signal_connect(G_OBJECT(widgets->Window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
        g_signal_connect(G_OBJECT(widgets->WebView),"load-failed",G_CALLBACK(on_browser_failed),widgets);
        g_signal_connect(G_OBJECT(widgets->BrowserButton),"clicked",G_CALLBACK(on_browser),widgets);
        g_setenv("WEBKIT_DISABLE_COMPOSITING_MODE","1",TRUE);
        g_setenv("WEBKIT_DISABLE_DMABUF_RENDERER","1",TRUE);
        g_setenv("LIBGL_ALWAYS_SOFTWARE","1",TRUE);
        GdkRGBA rgba = {1.0,1.0,1.0,1.0};
        webkit_web_view_set_background_color(WEBKIT_WEB_VIEW(widgets->WebView),&rgba);
        webkit_settings_set_user_agent(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(widgets->WebView)),"Mozilla/5.0 (X11; Linux x86_64) AppleWebkit/537.36 (KHTML. like Gecko) Chrome/120.0.0.0 Safari/537.36");
        webkit_website_data_manager_set_tls_errors_policy(webkit_web_view_get_website_data_manager(WEBKIT_WEB_VIEW(widgets->WebView)),
        WEBKIT_TLS_ERRORS_POLICY_IGNORE);
        webkit_web_view_load_uri(WEBKIT_WEB_VIEW(widgets->WebView),ConnectionAdress);
        printf("Load link window\n");
        GList *list = gtk_container_get_children(GTK_CONTAINER(gtk_window_get_titlebar(GTK_WINDOW(widgets->Window))));
        GList *iter;
        for (iter=list;iter;iter=iter->next){
            if (GTK_IS_BOX(iter->data)){
                GList *chlist = gtk_container_get_children(GTK_CONTAINER(iter->data));
                if (chlist->next){
                    gtk_widget_hide(GTK_WIDGET(chlist->data));
                } else {
                    gtk_widget_hide(GTK_WIDGET(iter->data));
                }
            }
        }
        g_list_free(list);
        gtk_header_bar_pack_end(GTK_HEADER_BAR(gtk_window_get_titlebar(GTK_WINDOW(widgets->Window))),widgets->BrowserButton);
        list = gtk_container_get_children(GTK_CONTAINER(widgets->StatusBox));
        for (iter=list;iter;iter=iter->next){
            gtk_widget_destroy(iter->data);
        }
        g_list_free(list);
        gtk_style_context_remove_class(gtk_widget_get_style_context(widgets->StatusBox),"boxInfoMessError");
        gtk_style_context_remove_class(gtk_widget_get_style_context(widgets->StatusBox),"boxInfoMessOK");
        gtk_widget_queue_draw(widgets->WebView);
    }

    return widgets;
}
#endif 

// #endif

int main(__attribute_maybe_unused__ int argc, __attribute_maybe_unused__ char** argv){
    #ifdef WEBKIT_INCLUDE
        setlocale(LC_ALL, "");
        textdomain (LocaleName);
        
        yon_ubl_connect_config((_template_config*)&main_config);
        yon_ubl_window_init(TITLE_LABEL,TITLE_INFO_LABEL,LocaleName,CssPath,LocaleName,version_application,WIKI_LINK);
        config_str unfound = NULL;
        int size=0;
        yon_ubl_setup_arguments(argc,argv,&unfound,&size,NULL);
        gtk_init(&argc,&argv);
        template_main_window *widgets = yon_ubl_window_setup();

        main_config.launch_arguments=yon_char_parsed_copy(argv,argc);
        main_config.launch_size=argc;
        yon_main_window_complete((main_window*)widgets);
        char *path = yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL);
        yon_window_config_load(path);
        
        gtk_main();
    #else
        char *command=yon_char_unite("xdg-open ",ConnectionAdress," &",NULL);
        yon_launch(command);
        free(command);
    #endif 
    return 0;
}