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

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

#define SLASH '/'

//------------------------------------------------
//      Determine next selection to be played
//------------------------------------------------

int     nextSelection() {

	int i, itype, j, k, CurCmd = 0;

	if (SP->scount < 1) return -1;

retry:

	i = 0;

	while (i < SP->scount ) {

		if ( !programData[i]->play || !playable(ProgramArray[i]) ) {
			i++;
			continue;
			}       // play check box not checked

		SP->NowPlaying = i;

		if (strlen(ProgramArray[i]) == 0) {
			i = -1;
			break;  // empty entry - halt
			}

		if (SP->playonce == 1) programData[i]->play = 0; // this has been played

		CurCmd=i;

		strncpy(SP->playThis, ProgramArray[CurCmd], 2048);
		SP->NowPlaying = i;
		programData[i]-> play = 0;

		char Time[64];
		SP->Clock = GetTime(i, ProgramArray[i], Time);

		for (k = i; k < SP->scount; k++)
			if ( programData[k]->play && playable(ProgramArray[k]) ) break;

		if (k < SP->scount)  {
			for (j = strlen(ProgramArray[k]); j >= 0 && ProgramArray[k][j] != '\\'; j--);
			sprintf(SP->next_random, "Cue: %s", &ProgramArray[k][j+1]);
			}
		else sprintf(SP->next_random, "Cue: <none>");

		return i;
		break;
		} // while loop

	on_Playerx_clicked (GTK_BUTTON(Playerx)); // reset play buttons to on
	goto retry;

	}

int     findNext() {
	int i, j, k;

	i = SP->NowPlaying;

	if (i < 0 ) return -1;

	for (k = i; k < SP->scount; k++)  // find current next candidate
		if ( programData[k]->play && playable(ProgramArray[k]) ) break;

	if (k >= SP->scount)  {
		on_Playerx_clicked (GTK_BUTTON(Playerx)); // reset play buttons to on
		k = 0;
		goto done;
		}

	programData[k]->play = 0;

	for (; k < SP->scount; k++) { // find new next candidate
		if ( programData[k]->play && playable(ProgramArray[k]) ) break;
		}

	if (k >= SP->scount)  {
		sprintf(SP->next_random, "Cue: <none>");
		return -1;
		}
done:

	for (j = strlen(ProgramArray[k]); j >= 0 && ProgramArray[k][j] != SLASH; j--);
	sprintf(SP->next_random, "Cue: %s", &ProgramArray[k][j+1]);
	return k;
	}


int     findNext1() {
	int i, j, k;

	for (k = 0; k < SP->scount; k++)  // find current next candidate
		if ( programData[k]->play && playable(ProgramArray[k]) ) break;

	if (k >= SP->scount)  {
		sprintf(SP->next_random, "Cue: <none>");
		return -1;
		}

	for (j = strlen(ProgramArray[k]); j >= 0 && ProgramArray[k][j] != SLASH; j--);
	sprintf(SP->next_random, "Cue: %s", &ProgramArray[k][j+1]);
	return k;
	}

