/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+
#+     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
#+
#+	astyle --style=banner --indent=tab load-program.c.in
#+
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

//	July 4, 2026

#include "includes.h"
#include<sys/wait.h>
#include "common-externs.h"
#define SLASH '/'

//-----------------------------------------------------------------------------
//      paint colors on tile buttons depending if played, playing or playable
//-----------------------------------------------------------------------------

void    buttonColor(int i, GtkWidget *lbl) {

//---------------------------------------------
//	i is the ProgramArray index
//	lbl is the tile
//	bn is  button nbr
//---------------------------------------------

	char *p1, name[8192], tmp[8192], tmp1[8192];
	int j;

	GdkPixbuf       *pix_In, *pix;

	if (i < 0) {
		return;
		}

	if (i >= SP->scount || access(ProgramArray[i], F_OK) == -1) {
		strcpy(tmp1, "/usr/share/sgr-zippy/no_image.jpg");
		pix_In = gdk_pixbuf_new_from_file (tmp1, &err);   // unscaled pixbuf
		pix = gdk_pixbuf_scale_simple (pix_In, 198, 134, GDK_INTERP_NEAREST); // scaled pixbuf
		gtk_image_set_from_pixbuf (GTK_IMAGE(lbl), pix);
		g_object_unref(pix_In);
		g_object_unref(pix);
		return;
		}

	strncpy(name, ProgramArray[i], PGM_MAX);

	strcpy(tmp, ProgramName(name));

	int ft = filetype(ProgramArray[i]);

	if (strlen(tmp) == 0 || ft == MP3) {
		if (ft == MP3)
			strcpy(tmp1, "/usr/share/sgr-zippy/audio.jpg");
		else
			strcpy(tmp1, "/usr/share/sgr-zippy/no_image.jpg");
		err = 0;
		pix_In = gdk_pixbuf_new_from_file (tmp1, &err);                      // unscaled pixbuf
		pix = gdk_pixbuf_scale_simple (pix_In, 198, 134, GDK_INTERP_NEAREST); // scaled pixbuf
		gtk_image_set_from_pixbuf (GTK_IMAGE(lbl), pix);
		g_object_unref(pix_In);
		g_object_unref(pix);
		return;
		}

//-----------------------------------------------------
//	tmp is name only - no dir or file type info
//	tmp1 is full address of .jpg in thumbnails
//-----------------------------------------------------

	thumbnail_name(tmp1, tmp); // name of thumbnail to tmp1 - includes dir info

	if (ft < 100 ) { // video

		if (access(tmp1, F_OK) == -1) { // does not exist - get thumbnail
			double thumb_offset = DEFAULT_THUMBNAIL_OFFSET;
			if (programTime[i] < DEFAULT_THUMBNAIL_OFFSET ) thumb_offset = ALT_THUMBNAIL_OFFSET; 

			sprintf(tmp,
			        "ffmpeg " FFLOGLEVEL " -y -noaccurate_seek -ss %f -i \"%s\"  "
			        "-frames:v 1 -s 198x134 \"%s\"",
			        thumb_offset,	// how far into the clip to go to get frame
			        ProgramArray[i],  // name of video file
			        tmp1);	// name of frame image to be created

			int syx = system(tmp);

			int flg = 0;

			if (WEXITSTATUS(syx) != 0) flg = 1;

			else {
				err = 0;
				pix = gdk_pixbuf_new_from_file (tmp1, &err);
				if (err != NULL) {
					g_error_free (err);
					flg = 1;
					}
				}

			if (flg) {
				strcpy(tmp1, "/usr/share/sgr-zippy/no_image.jpg");
				pix_In = gdk_pixbuf_new_from_file (tmp1, NULL); // unscaled pixbuf
				pix = gdk_pixbuf_scale_simple (pix_In, 198, 134, 
					GDK_INTERP_NEAREST); // scaled pixbuf
				}
			}

		else { // thumbnail exists
			err = 0;
			pix = gdk_pixbuf_new_from_file (tmp1, NULL);
			}
		}

	else {
		sprintf(tmp, "cp \"%s\" \"%s\"", ProgramArray[i], tmp1);
		system(tmp);

		err = 0;
		pix_In = gdk_pixbuf_new_from_file (tmp1, &err); // unscaled pixbuf
		pix = gdk_pixbuf_scale_simple 
			(pix_In, 198, 134, GDK_INTERP_NEAREST); // scaled pixbuf size
		g_object_unref(pix_In);
		}

	gtk_image_set_from_pixbuf (GTK_IMAGE(lbl), pix);
	g_object_unref(pix);

	while (gtk_events_pending ()) gtk_main_iteration ();
	}


// ----------------------------------------------------------------
// program tiles content load
// ----------------------------------------------------------------

void	DisplayProgramButtons() {

	char name[2048], tmp[2048];

        GdkColor playing, notplaying;

        playing.red       = 0x0000;
        playing.green     = 0xBBBB;
        playing.blue      = 0x0000;

        notplaying.red       = 0x0000;
        notplaying.green     = 0x0000;
        notplaying.blue      = 0x9999;

//----------------
//	upper tray
//----------------
	on_update_times_clicked(NULL);

	buttonColor(SP->begin+0,  lbl1);
	buttonColor(SP->begin+1,  lbl2);
	buttonColor(SP->begin+2,  lbl3);
	buttonColor(SP->begin+3,  lbl4);
	buttonColor(SP->begin+4,  lbl5);
	buttonColor(SP->begin+5,  lbl6);
	buttonColor(SP->begin+6,  lbl7);
	buttonColor(SP->begin+7,  lbl8);
	buttonColor(SP->begin+8,  lbl9);
	buttonColor(SP->begin+9,  lbl10);
	buttonColor(SP->begin+10, lbl11);
	buttonColor(SP->begin+11, lbl12);

//------------------------------
//	set 'play' checkboxes
//------------------------------

	if (SP->begin + 0 == SP->NowPlaying) 
		gtk_widget_modify_bg(p1name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p1name, GTK_STATE_NORMAL, &notplaying);

	if (SP->begin + 1 == SP->NowPlaying) 
		gtk_widget_modify_bg(p2name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p2name, GTK_STATE_NORMAL, &notplaying);

	if (SP->begin + 2 == SP->NowPlaying) 
		gtk_widget_modify_bg(p3name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p3name, GTK_STATE_NORMAL, &notplaying);

	if (SP->begin + 3 == SP->NowPlaying) 
		gtk_widget_modify_bg(p4name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p4name, GTK_STATE_NORMAL, &notplaying);

	if (SP->begin + 4 == SP->NowPlaying) 
		gtk_widget_modify_bg(p5name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p5name, GTK_STATE_NORMAL, &notplaying);

	if (SP->begin + 5 == SP->NowPlaying) 
		gtk_widget_modify_bg(p6name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p6name, GTK_STATE_NORMAL, &notplaying);

	if (SP->begin + 6 == SP->NowPlaying) 
		gtk_widget_modify_bg(p7name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p7name, GTK_STATE_NORMAL, &notplaying);

	if (SP->begin + 7 == SP->NowPlaying) 
		gtk_widget_modify_bg(p8name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p8name, GTK_STATE_NORMAL, &notplaying);

	if (SP->begin + 8 == SP->NowPlaying) 
		gtk_widget_modify_bg(p9name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p9name, GTK_STATE_NORMAL, &notplaying);

	if (SP->begin + 9 == SP->NowPlaying) 
		gtk_widget_modify_bg(p10name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p10name, GTK_STATE_NORMAL, &notplaying);

	if (SP->begin + 10 == SP->NowPlaying) 
		gtk_widget_modify_bg(p11name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p11name, GTK_STATE_NORMAL, &notplaying);

	if (SP->begin + 11 == SP->NowPlaying) 
		gtk_widget_modify_bg(p12name, GTK_STATE_NORMAL, &playing);
	else
		gtk_widget_modify_bg(p12name, GTK_STATE_NORMAL, &notplaying);


	if (programData[SP->begin + 0]->play > 0) {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check1),TRUE);
		}
	else {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check1),FALSE);
		}

	if (programData[SP->begin+1]->play > 0 )
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check2),TRUE);
	else	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check2),FALSE);

	if (programData[SP->begin+2]->play > 0 )
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check3),TRUE);
	else	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check3),FALSE);

	if (programData[SP->begin+3]->play > 0 )
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check4),TRUE);
	else	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check4),FALSE);

	if (programData[SP->begin+4]->play > 0 )
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check5),TRUE);
	else	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check5),FALSE);

	if (programData[SP->begin+5]->play > 0 )
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check6),TRUE);
	else	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check6),FALSE);

	if (programData[SP->begin+6]->play > 0 )
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check7),TRUE);
	else	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check7),FALSE);

	if (programData[SP->begin+7]->play > 0 )
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check8),TRUE);
	else	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check8),FALSE);

	if (programData[SP->begin+8]->play > 0 )
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check9),TRUE);
	else	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check9),FALSE);

	if (programData[SP->begin+9]->play > 0 )
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check10),TRUE);
	else	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check10),FALSE);

	if (programData[SP->begin+10]->play > 0 )
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check11),TRUE);
	else	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check11),FALSE);

	if (programData[SP->begin+11]->play > 0 )
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check12),TRUE);
	else	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(check12),FALSE);


	}

void	thumbnail_name(char *tmp1, char *tmp) {
	sprintf(tmp1, "%s/thumbnails/%s.jpg", Code_Home, tmp); // name of image frame
	}
