/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+
#+     Internet Radio Automation & Encoding Toolkit
#+
#+     Copyright (C) 2018  - 2025 by Kevin C. O'Kane
#+
#+     Kevin C. O'Kane
#+     kc.okane@gmail.com
#+     https://www.cs.uni.edu/~okane
#+     https://threadsafebooks.com/
#+
#+ This program is free software; you can redistribute it and/or modify
#+ it under the terms of the GNU General Public License as published by
#+ the Free Software Foundation; either version 2 of the License, or
#+ (at your option) any later version.
#+
#+ This program is distributed in the hope that it will be useful,
#+ but WITHOUT ANY WARRANTY; without even the implied warranty of
#+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#+ GNU General Public License for more details.
#+
#+ You should have received a copy of the GNU General Public License
#+ along with this program; if not, write to the Free Software
#+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#+
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

//	Sept 19, 2025

#include "includes.h"
#include "defines.h"
#include "common-externs.h"

#define SLASH '/'

void	on_web_playlist_clicked(GtkWidget *w) {

	char url_file[2048];

	gtk_entry_set_text (GTK_ENTRY(web_entry), "");
	webkit_web_view_try_close (WEBKIT_WEB_VIEW(webkit));
	WebKitWebContext * context=webkit_web_context_get_default ();
	webkit_web_context_clear_cache (context);

	strcpy(url_file, "file://");
	strcat(url_file, Code_Home);
	strcat(url_file,"/web/menu.html");
	webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webkit), url_file);
	return;
	}

void    on_zoom_level_value_changed(GtkSpinButton *s, GtkScrollType scr) {
	gdouble z = gtk_spin_button_get_value (s);
	webkit_web_view_set_zoom_level (WEBKIT_WEB_VIEW(webkit), z / 100.0);
	}

GtkWidget* on_webkit_create (WebKitWebView *web_view,
                             WebKitNavigationAction *navigation_action) {

//        gtk_widget_set_size_request (web_panel, WEB_WIDTH, WEB_HEIGHT);
//        gtk_widget_set_size_request (webkit, WEB_WIDTH, WEB_HEIGHT);

	WebKitNavigationType type =
	    webkit_navigation_action_get_navigation_type (navigation_action);

	int mouse;

	printf("Mouse button: %d\n",
	       mouse = webkit_navigation_action_get_mouse_button (navigation_action));

	if (type == WEBKIT_NAVIGATION_TYPE_LINK_CLICKED)
		printf("The navigation was triggered by clicking a link.\n");

	else if (type == WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED)
		printf("The navigation was triggered by submitting a form.\n");

	else if (type == WEBKIT_NAVIGATION_TYPE_BACK_FORWARD)
		printf("The navigation was triggered by navigating forward or backward.\n");

	else if (type == WEBKIT_NAVIGATION_TYPE_RELOAD)
		printf("The navigation was triggered by reloading.\n");

	else if (type == WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED)
		printf("The navigation was triggered by resubmitting a form.\n");

	else if (type == WEBKIT_NAVIGATION_TYPE_OTHER)
		printf("The navigation was triggered by some other action.\n");

	WebKitURIRequest *req = webkit_navigation_action_get_request (navigation_action);

	gchar *uri =  webkit_uri_request_get_uri (req);

	printf("Request URI = %s\n", uri);
	return GTK_WIDGET(web_view);

	}


gboolean        on_webkit_load_failed(GtkWidget *w) {

	return FALSE; // no further processing of this signal

	char url_file[2048];
	strcpy(url_file, "file://");
	strcat(url_file, Code_Home);
	strcat(url_file,"/web/failed.html");
	webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webkit), url_file);
	return TRUE; // no further processing of this signal
	}


void    on_web_entry_changed(GtkEntry *e) {
	strncpy(web_url, gtk_entry_get_text(e), 4096);
	}

void    on_web_button_clicked(GtkWidget *w) {
	if (strlen(web_url)==0) return;
	webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webkit), web_url);
	}

void    on_web2_clear_clicked(GtkWidget *w) {
	gtk_entry_set_text (GTK_ENTRY(web_entry), "");
	}

