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

//	August 2, 2026

#include "includes.h"
#include "defines.h"
#include "common-externs.h"
#include "common-fcn-headers.h"


#include <vlc/libvlc.h>
#include <vlc/libvlc_renderer_discoverer.h>
#include <vlc/libvlc_media.h>
#include <vlc/libvlc_media_player.h>

void on_dxb8_clicked(GtkWidget *);

#define SLASH '/'

//	change directory
void on_dir_choose_clicked(GtkWidget *w) { FileChooser(0); }

//	copy to this directory
void on_dir_choose1_clicked(GtkWidget *w) { FileChooser(1); }

//	copy from this directory
void on_dir_choose2_clicked(GtkWidget *w) { FileChooser(2); }

//	move from this directory
void on_rename1_clicked(GtkWidget *w) { FileChooser(3); }

void    FileChooser (int b) {

	char tmp[MAX_FILE_NAME], tmp1[MAX_FILE_NAME];

	gchar *directory, *p1;

	gint res;
	GtkWidget *dialog;
	GtkFileChooser *chooser;
	GtkFileChooserAction action;

	if (b == 1) 
		action = GTK_FILE_CHOOSER_ACTION_OPEN;
	else
		action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;

	chooser = GTK_FILE_CHOOSER(gtk_file_chooser_dialog_new ("Open File",
           GTK_WINDOW(window), // Parent window (NULL for standalone)
           action,
           "_Cancel", GTK_RESPONSE_CANCEL,
           "_Open", GTK_RESPONSE_ACCEPT,
           NULL));

	if (b == 1) // permit multiple selections
		gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(chooser), TRUE);

//-------------------------------------------
//	Run the dialog and check the response
//-------------------------------------------

	res = gtk_dialog_run (GTK_DIALOG (chooser));

	if (res == GTK_RESPONSE_CANCEL || 
		res == GTK_RESPONSE_DELETE_EVENT ||
		res == GTK_RESPONSE_NO) {

		gtk_widget_hide (GTK_WIDGET(chooser));
		gtk_widget_destroy (GTK_WIDGET(chooser));
		return;
		}

//------------------------------
//	copy to this directory
//------------------------------

	if (b == 1)  {
		char tmp[MAX_FILE_NAME];
		GSList *files;
		GSList *iter;

		gtk_widget_hide (GTK_WIDGET(chooser));

		files = gtk_file_chooser_get_filenames(chooser);

		for (iter = files; iter != NULL; iter = iter->next) {
			const gchar *p2 = iter->data;
			printf("*** %s\n", p2);
			sprintf(tmp, "cp \"%s\" \"%s\"", p2, Music_Home_Directory);
			printf("*** %s\n", tmp);
			char msg[MAX_FILE_NAME];
			sprintf(msg, "copying %s - Please wait.", p2);
			Zippy_says(msg);
			while (gtk_events_pending()) gtk_main_iteration();
			system(tmp);
			}

		on_dxb8_clicked(NULL);
		while (gtk_events_pending()) gtk_main_iteration();

//-------------------------------------
//	Free the list and filenames
//-------------------------------------

		g_slist_free(files);

		gtk_widget_destroy (GTK_WIDGET(chooser));
		on_scan_video_dir_clicked(UPDATE_TILES);
        	DisplayProgramButtons();

		return;
		}


	p1 = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER(chooser));

	if (p1 == NULL) {
		error_message("Error in file/directory selection.", NULL);
		return;
		}

//------------------------------------
//	copy from this directory
//	move from this directory
//------------------------------------

	if (b == 2 || b == 3) { // copy or move files

		char tmp[8196];

		gtk_widget_hide (GTK_WIDGET(chooser));

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

			if (programData[i]->play) {
				if (b == 2 )sprintf(tmp, "cp \"%s\" \"%s\" ", ProgramArray[i], p1);
				else sprintf(tmp, "mv \"%s\" \"%s\" ", ProgramArray[i], p1);
				printf("*** %s\n", tmp);
				char msg[MAX_FILE_NAME];
				sprintf(msg, "copying %s - Please wait.", ProgramArray[i]);
				Zippy_says(msg);
				while (gtk_events_pending()) gtk_main_iteration();
				system(tmp);
				}
			}

		on_dxb8_clicked(NULL);
		while (gtk_events_pending()) gtk_main_iteration();

		g_free(p1);
		gtk_widget_hide (GTK_WIDGET(chooser));
		gtk_widget_destroy (GTK_WIDGET(chooser));
		on_scan_video_dir_clicked(UPDATE_TILES);
        	DisplayProgramButtons();
		return;
		}

//----------------------------
//	change directory
//----------------------------

	char ptmp[MAX_FILE_NAME];
	strcpy(ptmp, p1);

	Cur_Dir(ptmp);

        strcpy(Program_List, "\"");
        strcat(Program_List, Code_Home);
        strcat(Program_List,"/sgr-zippy/program.lst");     // file address of tmp.lst
        strcat(Program_List, "\"");
	printf("New ProgramList: %s\n", Program_List);

	chdir(Music_Home_Directory);

	g_free(p1);
	gtk_widget_hide (GTK_WIDGET(chooser));
	gtk_widget_destroy (GTK_WIDGET(chooser));

	on_scan_video_dir_clicked(UPDATE_TILES);
        DisplayProgramButtons();

	return;
	}


