/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+
#+     Internet Radio Automation & Encoding Toolkit
#+
#+     Copyright (C) 2018 - 2026 by Kevin C. O'Kane
#+
#+     Kevin C. O'Kane
#+     kc.okane@gmail.com
#+     okane@uni.edu
#+     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 31, 2026

#define SLIDE_IMAGE_WIDTH 680    // slide size
#define SLIDE_IMAGE_HEIGHT 550


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

#define SLASH '/'

int     showImage(GtkWidget *image, char * file, int width, int height) {

	char tmp1[2048], cmd[2048];
	int j,k,h,h1,v,v1,tmpFlg=0;
	FILE *f1, *p1;
	char fileName[4096] = {0};
	int ver, hor;

	strcpy(fileName, file);

	if (access(fileName, R_OK)) {
                sprintf(tmp1, "readlink -f \"%sslides/\"* > %sslides.lst",
                        Code_Home, Code_Home);
        	system(tmp1);
		return 1; // not found
		}

	printf("*** slide = %s\n", fileName);

	err = NULL;
	pixbuf = gdk_pixbuf_new_from_file_at_scale
	         (fileName, width, height, TRUE, &err);

	if(err != NULL) {
		g_error_free(err);
		return 1;
		}

	if (pixbuf == NULL) return 1;

//--------------------------------------------------
//	these are not the same as width and height
//--------------------------------------------------

	float pwidth = gdk_pixbuf_get_width(pixbuf);
	float pheight = gdk_pixbuf_get_height(pixbuf);

	gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);

	gtk_widget_show(image);

	g_object_unref(pixbuf); /* no longer needed - free it */

	gint horx, verx, winheight, winwidth;

	gtk_window_get_size (GTK_WINDOW(window), &winwidth, &winheight);

	horx = SLIDE_IMAGE_WIDTH * (winwidth / 1080.0 );
	verx = SLIDE_IMAGE_HEIGHT * (winheight / 608.0 );

	horx = (winwidth - 410) / 2.0 - (pwidth / 2.0);
	verx = (winheight - 26) / 2.0 - (pheight / 2.0);
	verx = (winheight - 30) / 2.0 - (pheight / 2.0);

	if (verx < 0 ) verx = 0;
	if (horx < 0 ) horx = 0;

	if (image == SlidesImage)  {
//		gtk_fixed_move (GTK_FIXED(covers), image, 2 + horx, verx );

		gtk_fixed_move (GTK_FIXED(covers), image, horx, verx );
		return 1;
		}

//	if (image == SideSlidePanel) {
//		gtk_fixed_move (GTK_FIXED(fixed1),
//			image, SideSlideX + (width - pwidth) / 2, SideSlideY + (height - pheight)/2);
//        		return 1;
//		}

	return 1;

	}

//---------------------------------------------
//	Display cover art or random slide
//---------------------------------------------

int	DisplayCover(char *file) {

	static char * slideRef[10000] = { NULL} ;
	static int slideCount = 0;
	char tmp[4096];
	char cmd[4096];

	gtk_widget_show(covers);

//------------------------------
//	scan slide directory
//------------------------------

	sprintf(tmp, "readlink -f \"%sslides/\"* > %sslides.lst",
		Code_Home, Code_Home);
	system(tmp);

	if (slideCount == 0) { // read slide names

		FILE *f1;
		static int done = 0;
		char file_name[1024];

		if (done) return 0;
		done = 1;

		sprintf(file_name, "%s/slides.lst", Code_Home);
		f1 = fopen(file_name, "r");
		if (f1 == NULL) {
			printf("*** no slides found.\n");
			sprintf(file_name, "%s/icons/sgr001.jpg", Code_Home);
			slideCount = 1;
			slideRef[0] = (char *)malloc(strlen(file_name) + 1);
			strcpy(slideRef[0], file_name);
			return 0;
			}

		while (1) {
			if (fgets(tmp, 4095, f1) == NULL) break;
			tmp[strlen(tmp)-1] = 0;
			if (tmp[0] == '\"') strcpy(tmp, &tmp[1]); // remove quotes
			if (tmp[strlen(tmp)-1] == '\"') tmp[strlen(tmp)-1] = 0;
			slideRef[slideCount] = (char *)malloc(strlen(tmp) + 1);
			strcpy(slideRef[slideCount], tmp);
			slideCount ++;
			if (slideCount > 10000) {
				slideCount --;
				break;
				}
			}
		printf("*** %d slides loaded.\n", slideCount);
		if (slideCount < 1 ) {
			sprintf(file_name, "%s/icons/sgr001.jpg", Code_Home);
			slideCount = 1;
			slideRef[0] = (char *)malloc(strlen(file_name) + 1);
			strcpy(slideRef[0], file_name);
			}
		fclose(f1);
		}


//---------------------
//      default slide
//---------------------

	if (strcmp(file, "default") == 0) {

		sprintf(cmd, "%s/icons/cover.jpg", Code_Home);
		}

//---------------------
//      random slide
//---------------------

	else {

		int i;
		i = rand() % slideCount;
		strcpy(cmd, slideRef[i]);
		}

	gint hor, ver;
	int width, height;
	gtk_window_get_size (GTK_WINDOW(window), &hor, &ver);

	width = SLIDE_IMAGE_WIDTH * (hor / 1090.0 );
	height = SLIDE_IMAGE_HEIGHT * (ver / 608.0 );

	showImage(SlidesImage, cmd, width, height);

	return 1;
	}

gboolean picture_handler(struct APW *widgets) { // called by timer

	static int first = 1;

	if (first) {
		first = 0;
		DisplayCover("null");
		}

	if (gtk_stack_get_visible_child(GTK_STACK(stack1)) != covers) return TRUE;

	if (slide_timer >= 0) {
		slide_timer++;
		if (slide_timer % slide_time != 0) return TRUE;
		}

	slide_timer = 0;
	gtk_widget_show(SlidesImage);

	DisplayCover("null");

	return TRUE;
	}

