/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+
#+     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 17, 2025

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

#define SLASH '/'

void    AlbumBtnLoad() {

	int start = AlbumBegin;
	char tmp[2048];

	for (int i=0; i<9; i++) AlbumButton[i] = -1; // empty

	gtk_widget_hide(image13);
	gtk_widget_hide(image14);
	gtk_widget_hide(image15);
	gtk_widget_hide(image16);
	gtk_widget_hide(image17);
	gtk_widget_hide(image18);
	gtk_widget_hide(image19);
	gtk_widget_hide(image20);
	gtk_widget_hide(image21);

	gtk_label_set_text(GTK_LABEL(lbl13), "");
	gtk_label_set_text(GTK_LABEL(lbl14), "");
	gtk_label_set_text(GTK_LABEL(lbl15), "");
	gtk_label_set_text(GTK_LABEL(lbl16), "");
	gtk_label_set_text(GTK_LABEL(lbl17), "");
	gtk_label_set_text(GTK_LABEL(lbl18), "");
	gtk_label_set_text(GTK_LABEL(lbl19), "");
	gtk_label_set_text(GTK_LABEL(lbl20), "");
	gtk_label_set_text(GTK_LABEL(lbl21), "");

	if (!SP->acount) return; // zero albums present

	if (!albumImage(image13, AlbumArray[start], ALBUM_IMAGE_SMALL_WIDTH, ALBUM_IMAGE_SMALL_HEIGHT)) {
		gtk_label_set_text(GTK_LABEL(lbl13), ShortName(AlbumArray[start]));
		}

	AlbumButton[0] = start; // index in AlbumArray of entry in this tile

	start++;
	if (start >= SP->acount) return;

	if (!albumImage(image14, AlbumArray[start], ALBUM_IMAGE_SMALL_WIDTH, ALBUM_IMAGE_SMALL_HEIGHT)) {
		gtk_label_set_text(GTK_LABEL(lbl14), ShortName(AlbumArray[start]));
		}

	AlbumButton[1] = start;
	start++;
	if (start >= SP->acount) return;

	if (!albumImage(image15, AlbumArray[start], ALBUM_IMAGE_SMALL_WIDTH, ALBUM_IMAGE_SMALL_HEIGHT)) {
		gtk_label_set_text(GTK_LABEL(lbl15), ShortName(AlbumArray[start]));
		}

	AlbumButton[2] = start;

	start++;
	if (start >= SP->acount) return;

	if (!albumImage(image16, AlbumArray[start], ALBUM_IMAGE_SMALL_WIDTH, ALBUM_IMAGE_SMALL_HEIGHT)) {
		gtk_label_set_text(GTK_LABEL(lbl16), ShortName(AlbumArray[start]));
		}

	AlbumButton[3] = start;

	start++;
	if (start >= SP->acount) return;

	if (!albumImage(image17, AlbumArray[start], ALBUM_IMAGE_SMALL_WIDTH, ALBUM_IMAGE_SMALL_HEIGHT)) {
		gtk_label_set_text(GTK_LABEL(lbl17), ShortName(AlbumArray[start]));
		}

	AlbumButton[4] = start;

	start++;
	if (start >= SP->acount) return;

	if (!albumImage(image18, AlbumArray[start], ALBUM_IMAGE_SMALL_WIDTH, ALBUM_IMAGE_SMALL_HEIGHT)) {
		gtk_label_set_text(GTK_LABEL(lbl18), ShortName(AlbumArray[start]));
		}

	AlbumButton[5] = start;

	start++;
	if (start >= SP->acount) return;

	if (!albumImage(image19, AlbumArray[start], ALBUM_IMAGE_SMALL_WIDTH, ALBUM_IMAGE_SMALL_HEIGHT)) {
		gtk_label_set_text(GTK_LABEL(lbl19), ShortName(AlbumArray[start]));
		}

	AlbumButton[6] = start;

	start++;
	if (start >= SP->acount) return;

	if (!albumImage(image20, AlbumArray[start], ALBUM_IMAGE_SMALL_WIDTH, ALBUM_IMAGE_SMALL_HEIGHT)) {
		gtk_label_set_text(GTK_LABEL(lbl20), ShortName(AlbumArray[start]));
		}

	AlbumButton[7] = start;

	start++;
	if (start >= SP->acount) return;

	if (!albumImage(image21, AlbumArray[start], ALBUM_IMAGE_SMALL_WIDTH, ALBUM_IMAGE_SMALL_HEIGHT)) {
		gtk_label_set_text(GTK_LABEL(lbl21), ShortName(AlbumArray[start]));
		}

	AlbumButton[8] = start;

	}

void    on_albumForward_clicked(GtkWidget *b) {
	int i;
	if (AlbumBegin + 9 >= SP->acount) AlbumBegin = SP->acount - 9;
	else AlbumBegin = AlbumBegin + 9;
	gtk_adjustment_set_value (adjustment32, (gdouble) AlbumBegin);
	}

void    on_albumBack_clicked(GtkWidget *b) {
	int i;
	if (AlbumBegin - 9 < 0) AlbumBegin = 0;
	else AlbumBegin = AlbumBegin - 9;
	gtk_adjustment_set_value (adjustment32, (gdouble) AlbumBegin);
	}

gboolean        on_albumScroll_scroll_event(GtkEntry *b, GdkEventScroll *e) {

//--------------------------------------------------------------
//      see GdkEventScroll data structure for valid elements
//      see: gtk_widget_set_events(g_buttonxx,GDK_SCROLL_MASK);
//--------------------------------------------------------------

	scroll_event3(e);

	return FALSE;
	}

//----------------------------
//      Album scroll bar
//----------------------------

void    on_albumScroll_value_changed(GtkRange *r) {

	int k = (int) gtk_range_get_value (r);

	if (k + 9 > SP-> acount) k = SP->acount - 9;

	if (k < 0) k = 0;

	AlbumBegin = k;

	AlbumBtnLoad();
	}

//---------------------------------
//      Show covers of albums
//---------------------------------

void    on_showCovers_toggled (GtkButton *b) { // on album page

	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(b))) {
		show_covers = 1;
		AlbumBtnLoad();
		}
	else {
		show_covers = 0;
		AlbumBtnLoad();
		}
	}

//--------------------------------------
//--------------------------------------
//	ALBUM BUTTON CLICKED signals
//--------------------------------------
//--------------------------------------

//----------------------------------------------------------------------------------------
//      on_p13_button_press_event on_p14_button_press_event on_p15_button_press_event
//      on_p16_button_press_event on_p17_button_press_event on_p18_button_press_event
//      on_p19_button_press_event on_p20_button_press_event on_p21_button_press_event
//      on_p13_clicked            on_p14_clicked            on_p15_clicked
//      on_p16_clicked            on_p17_clicked            on_p18_clicked
//      on_p19_clicked            on_p20_clicked            on_p21_clicked
//----------------------------------------------------------------------------------------


//---------------------
//	Linux version
//---------------------

#define P_ALBUM_CLICKED(NBR) \
        gboolean on_p## NBR ##_button_press_event(GtkButton *b, GdkEventButton *event) { \
                if (event->button == 1 ) return FALSE; /*left click*/ \
                char cmd[2048]; int i, where = 0;\
                if (SP->acount < 0 ) return FALSE; \
                sprintf(cmd, "%s/tmpFileList", Code_Home); \
                printf("Saving program list to %s\n",cmd); \
                FILE *f1 = fopen (cmd, "w"); \
                if (f1 == NULL ) { Message("File Error Saving Program List"); return TRUE; } \
                fprintf(f1, "%s/\n", AlbumArray[AlbumButton[NBR - 13]]); /* add at start */ \
                for (i = 0; i < SP->scount; i++) { /* write program list to disk */ \
                        if (strlen(ProgramArray[i]) == 0 ) continue; \
                        fprintf(f1, "%s\n", ProgramArray[i]);  \
                        } \
                fclose(f1); \
                sprintf(cmd, "%s/tmpFileList", Code_Home); \
                LoadProgram(cmd); \
                SP->Xbegin = where; \
                DisplayProgramButtons( ); \
                return TRUE; } \
        void on_p## NBR ##_clicked(GtkWidget *b) { \
                char cmd[2048]; int i, where = 0;\
                if (SP->acount < 0 ) return; \
                sprintf(cmd, "%s/tmpFileList", Code_Home); \
                printf("Saving program list to %s\n",cmd); \
                FILE *f1 = fopen (cmd, "w"); \
                if (f1 ==NULL ) { Message("File Error Saving Program List"); return; } \
                for (i=0; i<SP->scount; i++) { /* write program list to disk */ \
                        if (strlen(ProgramArray[i]) == 0 ) continue; \
                        fprintf(f1, "%s\n", ProgramArray[i]);  \
                        } \
                fprintf(f1, "%s/\n", AlbumArray[AlbumButton[NBR - 13]]); where = i; /* add album at end */ \
                fclose(f1); \
                sprintf(cmd, "%s/tmpFileList", Code_Home); \
                LoadProgram(cmd); \
                SP->Xbegin = where; \
                DisplayProgramButtons( ); \
		gtk_stack_set_visible_child (GTK_STACK(stack1), pg); \
                return; \
                }

P_ALBUM_CLICKED(13) P_ALBUM_CLICKED(14) P_ALBUM_CLICKED(15)
P_ALBUM_CLICKED(16) P_ALBUM_CLICKED(17) P_ALBUM_CLICKED(18)
P_ALBUM_CLICKED(19) P_ALBUM_CLICKED(20) P_ALBUM_CLICKED(21)

//---------------------------------
//
// load album images
//
//---------------------------------

int     albumImage(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];
	int ver, hor;

	if (SP->acount < 0 ) return 0;

	if (! show_covers) return 0;

	strcpy(fileName, file);

	strcat(fileName, "/cover.jpg");

	f1 = fopen(fileName, "r"); // does it exist?

	if (f1 == NULL) { // no - is there a .png image?
		strcpy(fileName, file);

		strcat(fileName, "/cover.png");

		f1 = fopen(fileName, "r"); // does it exist?
		}

	if (f1 == NULL) { // nope
		gtk_widget_hide(image);
		return 0;
		}

	else fclose(f1);

	showImage(image, fileName, width, height);

	return 1;

	}

gboolean        scroll_event3(GdkEventScroll *e) {

	// see GdkEventScroll data structure for valid elements
	// see: gtk_widget_set_events(g_buttonxx,GDK_SCROLL_MASK);

	if (e->delta_y < 0.0) { // up
		on_albumForward_clicked(albumForward);
		}

	else { // down
		on_albumBack_clicked(albumBack);
		}

	return TRUE;
	}
