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

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

#define SLASH '/'

void    on_alt_search_changed(GtkEntry  *entry) {

	static int last = -1;
	static char alt_search_text[4096];

	int i = gtk_entry_get_text_length (entry);

	if (i <= 0) return;

	if (i>STR_MAX) {
		Message("Entry Error");
		gtk_editable_delete_text (GTK_EDITABLE(entry), 0, -1 );
		return;
		}

	strncpy(alt_search_text, gtk_entry_get_text(entry), 4095); // extract text from box

	char name[4096], tmp[4096];

	int j;

	if (search_again_flag) j = last;
	else j = 0;

	search_again_flag = 0;

	while (1) {
		for (i = j; i < SP->scount; i++) { // start looking for it
			strncpy(name, ProgramArray[i], 4096);
			strncpy(tmp, ProgramName(name,0), 4096);
			search_again_flag = 0;

			if (strcasestr(tmp, alt_search_text) != 0) {
				SP->Xbegin = i;
				DisplayProgramButtons();
				last = i + 1;
				return;
				}
			}
		if (last > 0) {
			last = j = 0;
			continue;
			}
		break;
		}
	last = 0;
	}


void    on_alt_search1_changed(GtkEntry  *entry) {

	static int last = -1;
	static char alt_search_text[4096];

	int i = gtk_entry_get_text_length (entry);

	if (i <= 0) return;

	if (i>STR_MAX) {
		Message("Entry Error");
		gtk_editable_delete_text (GTK_EDITABLE(entry), 0, -1 );
		return;
		}

	strncpy(alt_search_text, gtk_entry_get_text(entry), 4095); // extract text from box

	char name[4096], tmp[4096];

	int j;

	if (search_again_flag) j = last;
	else j = 0;

	search_again_flag = 0;

	while (1) {
		for (i = j; i < SP->scount; i++) { // start looking for it
			strncpy(name, ProgramArray[i], 4096);
			strncpy(tmp, ProgramName(name,0), 4096);
			search_again_flag = 0;

			if (strcasestr(tmp, alt_search_text) != 0) {
				SP->begin = i;
				DisplayProgramButtons();
				last = i + 1;
				return;
				}
			}
		if (last > 0) {
			last = j = 0;
			continue;
			}
		break;
		}
	last = 0;
	}


void    on_search_again_clicked(GtkWidget *w) {
	search_again_flag = 1;
	on_alt_search_changed(alt_search);
	}

void    on_search_again1_clicked(GtkWidget *w) {
	search_again_flag = 1;
	on_alt_search1_changed(alt_search1);
	}


//-------------------------
//      service function
//-------------------------

void on_location_insert_text(GtkEditable  *entry, gchar *edit) {
	int i = gtk_entry_get_text_length (GTK_ENTRY(entry));
	}


//-------------------------
//      service function
//-------------------------

void on_alt_search_insert_text(GtkEditable  *entry, gchar *edit) {
	int i = gtk_entry_get_text_length (GTK_ENTRY(entry));
	}


void on_alt_search1_insert_text(GtkEditable  *entry, gchar *edit) {
	int i = gtk_entry_get_text_length (GTK_ENTRY(entry));
	}

