/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+
#+     Internet Radio Automation & Encoding Toolkit
#+
#+     Copyright (C) 2018 - 2026
#+     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
#+
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

//	July 8, 2026

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

#include <vlc/libvlc.h>
#include <vlc/libvlc_renderer_discoverer.h>
#include <vlc/libvlc_media.h>
#include <vlc/libvlc_media_player.h>

#define SLASH '/'

//---------------------------------------
//	Show message with Dismiss button
//---------------------------------------

void	Zippy_says(char *msg) { zippy_says(msg); }

void	zippy_says(char *msg) {

	static char zip[10000] = "";
	char msg1[16000];
	int i, j, k, m;

	if (strlen(msg) == 0) {
		zip[0] = 0;
		return;
		}

	memset(msg1, 0, 16000);
	strcpy(msg1, msg);

	if (strlen(msg1) > 100) {
		msg1[90] = 0;
		strcat(msg1, " ...");
		}

	j = 0;
	while(msg1[j] != 0) { // strip non-printable
		if (msg1[j] < 32 || msg1[j] > 127)
			msg1[j] = ' ';
		j++;
		}

	for (j = strlen(msg1) - 1; j > 0; j--) {
		if (msg1[j] == ' ') msg1[j] = 0;
		else break;
		}

	strcat(msg1, "\n");

        GdkColor color1;
        color1.red = 0x1111;
        color1.green = 0x1111;
        color1.blue = 0x9999;

        GdkColor color2;
        color2.red = 0xFFFF;
        color2.green = 0xFFFF;
        color2.blue = 0x0000;

        gtk_widget_modify_bg(g_dbox4, GTK_STATE_NORMAL, &color1);
        gtk_widget_modify_fg(g_dbox4, GTK_STATE_NORMAL, &color2);
        gtk_widget_modify_bg(dbox4_tv, GTK_STATE_NORMAL, &color1);
        gtk_widget_modify_bg(dbox4_vp, GTK_STATE_NORMAL, &color1);
        gtk_widget_modify_bg(dbox4_sw, GTK_STATE_NORMAL, &color1);
gtk_widget_modify_base(dbox4_tv, GTK_STATE_NORMAL, &color1);
gtk_widget_modify_base(dbox4_vp, GTK_STATE_NORMAL, &color1);
gtk_widget_modify_base(dbox4_sw, GTK_STATE_NORMAL, &color1);

	gtk_window_set_title(GTK_WINDOW(g_dbox4),"Zippy Says");

	strncat(zip, msg1, 9999);

	k = 0;
	j = strlen(zip);
	for (i = 0; i < j; i++) {
		if (zip[i] == '\n') {
			k++;
			if (k == 1) m = i + 1;
			}
		if (k > 10) {
			strcpy(zip, &zip[m]);
			break;
			}
		}

	TextBuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(dbox4_tv));
	gtk_text_buffer_set_text (TextBuffer, zip, -1);

	printf("zippy_says(): %s", msg1);

	gtk_widget_show(g_dbox4);  // show dialog
	}

void    on_dxb8_clicked(GtkButton *b) { // clear button
        zippy_says("");
        TextBuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(dbox4_tv));
        gtk_text_buffer_set_text (TextBuffer, (const gchar *) "", (gint) -1);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(view_log), TRUE);
        gtk_widget_hide(g_dbox4);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(view_log), FALSE);
        }

void    on_dxb7_clicked(GtkButton *b) { // ok button
        gtk_widget_hide(g_dbox4);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(view_log), FALSE);
        }


void    on_view_log_toggled(GtkWidget *w) {
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)) )
                gtk_widget_show(g_dbox4);  // show dialog
        else
                gtk_widget_hide(g_dbox4);  // hide dialog
        }

void    on_halt_ffmpeg_clicked(GtkWidget *w) {
	char tmp[MAX_FILE_NAME];
	sprintf(tmp, "rm \"%s/tmp.mp4\"", Music_Home_Directory);
	system (tmp);
	sprintf(tmp, "rm \"%s/jointmp.mp4\"", Music_Home_Directory);
	system (tmp);
        system("killall -q -s INT ffmpeg");
        system("killall -q -s INT ffplay");
	
        DisplayProgramButtons();
        }

void    on_message1_close(GtkWidget *w) {
        gtk_widget_hide(message1);
        }

static void _set_label_selectable(gpointer data, gpointer user_data) {
	GtkWidget *widget = GTK_WIDGET(data);
	if (GTK_IS_LABEL(widget)) {
		gtk_label_set_selectable(GTK_LABEL(widget), TRUE);
		}
	}

void	error_message(char *p1, char * p2) {
	char tmp[16000];

	on_dxb7_clicked(NULL) ; // hide log

        GdkColor color1;
        color1.red = 0x1111;
        color1.green = 0x1111;
        color1.blue = 0x9999;

        GdkColor color2;
        color2.red = 0xFFFF;
        color2.green = 0xFFFF;
        color2.blue = 0x0000;

        gtk_widget_modify_bg(message1, GTK_STATE_NORMAL, &color1);
        gtk_widget_modify_fg(message1, GTK_STATE_NORMAL, &color2);

	if (p2 == NULL) strcpy(tmp, p1);
	else sprintf(tmp, "%s\n%s", p1, p2);
	gtk_message_dialog_format_secondary_text ( GTK_MESSAGE_DIALOG(message1), tmp);

//----------------------------------
//	make message text copyable
//----------------------------------

	GtkWidget *area = gtk_message_dialog_get_message_area(
		GTK_MESSAGE_DIALOG(message1));
	GtkContainer *box = (GtkContainer *) area;
	GList *children = gtk_container_get_children(box);
	g_list_foreach(children, _set_label_selectable, NULL);
	g_list_free(children);

	printf("--- Message ---\n%s\n", tmp);
	gtk_widget_show(message1);
	}
