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

//	April 18, 2021

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

#define SLASH '/'

//-------------------------------
//      randomize program array
//-------------------------------

void    on_randomize_clicked (GtkButton *button) {
	int i,itype,j,k;
	char cmd[2048];
	static int rgen = 1;

	randomize_flg = 1;

//-------------------------------
//      randomize the entries
//-------------------------------

	srand((unsigned int) time(0));

	for (i=0; i<SP->scount; i++) {

//--------------------------------------
//              playable media file?
//--------------------------------------

		if (ProgramArray[i][0] != '/') {
			delRow(i);
			continue;
			}

		j = rand() % SP->scount;

		strcpy(cmd, ProgramArray[i]);
		strcpy(ProgramArray[i], ProgramArray[j]);
		strcpy(ProgramArray[j], cmd);

		k = programData[i]->play;
		programData[i]->play = programData[j]->play;
		programData[j]->play = k;

		k = programPlayed[i]; // play count
		programPlayed[i] = programPlayed[j];
		programPlayed[j] = k;

		k = programTime[i];
		programTime[i] = programTime[j];
		programTime[j] = k;
		}

	SP->PauseKill = 0;
	orgPgm(); // go to start
	redraw = 1;
	DisplayProgramButtons();
	return;
	}

//---------------------
//      Alpha sort
//---------------------

void    on_sort_clicked(GtkWidget *b) {

	int f = 1, i, j;

	for (i = 0; i < SP-> scount; i++) {

		if (ProgramArray[i][0] != '/') {

			delRow ( i );
			}
		}

	j =  SP->scount - 1;

	while (f) {

		f = 0;

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

			if (strcmp(ProgramArray[i], ProgramArray[i + 1]) > 0 ) {
				SwapProgramElement(i);
				f = 1;
				}
			}

		j = j - 1;
		}

	SP->begin = 0;
	DisplayProgramButtons();        // first 24 buttons loaded
	}
