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

//	Oct 9, 2022

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

#define SLASH '/'


#define TIME_CAPTIONS(NBR) \
        playTime(Now, TimeStart, TimeEnd, length, programTime[SP->begin + NBR - 1]); \
        gtk_label_set_text(GTK_LABEL(p## NBR ##TimeLength), length); \
        if ( programTime[bx ##NBR ] > 0 && programData[bx ##NBR ]->play > 0 ) { \
                gtk_widget_show(p## NBR ##Time); \
                gtk_widget_show(p## NBR ##TimeEnd); \
                gtk_widget_show(p## NBR ##TimeLength); \
                gtk_label_set_text(GTK_LABEL(p## NBR ##Time), TimeStart); \
                gtk_label_set_text(GTK_LABEL(p## NBR ##TimeEnd), TimeEnd); \
                Now = Now + programTime[SP->begin + NBR - 1]; \
                } \
        else { \
                gtk_widget_hide(p## NBR ##Time); \
                gtk_widget_hide(p## NBR ##TimeEnd); \
                gtk_widget_hide(p## NBR ##TimeLength); \
                }


void    updateTimes() {
	time_t Now = time(0);
	char TimeStart[64], TimeEnd[64], length[32];
	int playable = 0;
	int changeFlg = 0;
	int i, j;

//-----------------------------------------------------------------------
//      GetTime() returns an int with the nbr of seconds in the file
//      playTime() formats string Time
//-----------------------------------------------------------------------

	int scanLimit = SCAN_LIMIT;

	for (i = 0; i < SP->scount; i++) {
		if (programTime[i] < 0 ) break;
		}

	if (i >= SP->scount) scanLimit = SP->scount;

//----------------------------------------------------------------------------
//      find first unplayed file from the beginning of the program array that
//      has no time stamp
//----------------------------------------------------------------------------

	int firstUnplayed;

	for (firstUnplayed = 0; firstUnplayed < SP->scount; firstUnplayed++)
		if (programData[firstUnplayed]->play == 1) break;

	i = 0;

	for (j = firstUnplayed; j < SP->scount; j++) {
		if (programData[j]->play == 1) i++;
		if (i > scanLimit) break;
		}

	for (i = firstUnplayed; i < j; i++) {

		if (programData[i]->play) {
			playable ++;
			if (programTime[i] < 0) GetTime(i, ProgramArray[i], TimeStart);
			if (i < SP->begin + 0) {
				Now = Now + programTime[i];
				changeFlg = 1;
				}
			}
		}

	Now = Now + timeRemaining;

	TIME_CAPTIONS(1) TIME_CAPTIONS(2)  TIME_CAPTIONS(3)  TIME_CAPTIONS(4)
	TIME_CAPTIONS(5) TIME_CAPTIONS(6)  TIME_CAPTIONS(7)  TIME_CAPTIONS(8)
	TIME_CAPTIONS(9) TIME_CAPTIONS(10) TIME_CAPTIONS(11) TIME_CAPTIONS(12)

	Now = time(0) + timeRemaining;

	if (SP->begin < PROGRAM_BUTTON_COUNT ) {
		if (redraw) SP->begin = 0;
		redraw = 0;
		DisplayProgramButtons();
		}

	}
