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

//	July 12, 2023

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

#define SLASH '/'


//------------------------------------------------
//      media playback speed spin button changed
//------------------------------------------------

void    on_vlcSpinRate_value_changed(GtkSpinButton *s, GtkScrollType scr) {
	char text[32];
	float rate = (int) gtk_spin_button_get_value(s);
	rate = rate/100.0;
	setVlcRate(rate);
	}

void    on_vlcPosition_move_slider(GtkRange *r) {
	if (!mediaPlayer) {
		gtk_range_set_value(r,0.0);
		return;
		}
	float position = gtk_range_get_value (r) / 100.0; // between 0 and 1

//        libvlc_media_player_set_position(mediaPlayer, position);

#ifdef VLC4
	libvlc_media_player_set_position(mediaPlayer, position, FALSE);
#else
	libvlc_media_player_set_position(mediaPlayer, position);
#endif

	}

void    on_vlcPosition_value_changed(GtkRange *r) {
	char tmp[128];
	GdkColor color2;
	color2.red = 0xFFFF;
	color2.green = 0x0000;
	color2.blue = 0x0000;
	float position = gtk_range_get_value (r) / 100.0; // between 0 and 1
	sprintf(tmp,"%4.1f%%", position*100.);
	gtk_label_set_text(GTK_LABEL(posLbl), tmp);    // update label

	if (timeRemaining < 15 && timeRemaining > 0) {
		gtk_widget_modify_bg(posLbl, GTK_STATE_NORMAL, &color2);
		}

	else gtk_widget_modify_bg(posLbl, GTK_STATE_NORMAL, NULL);
	return;
	}


gboolean        on_vlcPosition_change_value(GtkRange *r,
        GtkScrollType type, gdouble value) {

	if (!mediaPlayer) return TRUE;
	if (type == GTK_SCROLL_JUMP) {
		float position = gtk_range_get_value (r) / 100.0; // between 0 and 1

//                libvlc_media_player_set_position(mediaPlayer, position);

#ifdef VLC4
		libvlc_media_player_set_position(mediaPlayer, position, FALSE);
#else
		libvlc_media_player_set_position(mediaPlayer, position);
#endif
		return FALSE;
		}
	return FALSE;
	}

void    update_vlcPosition() {
	if (!mediaPlayer) {
		return;
		}
	float position = libvlc_media_player_get_position(mediaPlayer);
	gtk_range_set_value (GTK_RANGE(vlcPosition), position*100.0);
	}


void    setVlcRate(float rate) {
	char tmp[64];
	int k;
	if (!mediaPlayer) {
		return;
		}
	k = libvlc_media_player_set_rate(mediaPlayer, rate);
	}

int     vlc_playing() {

	if (!mediaPlayer) return 0;
	if (SP->VlcPauseFlg || libvlc_media_player_is_playing(mediaPlayer))
		return 1; // pause means play
	return 0;
	}

void    vlc_pause() {
	if (!mediaPlayer) return;
	libvlc_media_player_set_pause(mediaPlayer, 1);
	SP->VlcPauseFlg = 1; // play is paused
	return;
	}

void    vlc_no_pause() {
	if (!mediaPlayer) return;
	libvlc_media_player_set_pause(mediaPlayer, 0);
	SP->VlcPauseFlg = 0; // play is not paused
	return;
	}

//-----------------------
//      Pause program
//-----------------------

void    on_pauseVideo_toggled (GtkButton *b) {
	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(b))) vlc_pause();
	else vlc_no_pause();
	}

//--------------------------------------------------------------
//      Pause at end of current media file if in program mode
//--------------------------------------------------------------

void    on_pauseAtEnd_toggled (GtkButton *b) {

	if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(b))) {
		SP->Pause = 0;  // no pause at end
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pauseAtEnd), FALSE);
		}
	else {
		SP->Pause = 1; // pause at end
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pauseAtEnd), TRUE);
		}
	}


int     vlc_stop() {

	gtk_widget_hide(video_draw);
	gtk_widget_show(video_bg);

	strcpy(SP->CurrentPlay, "");

//----------------------
//	Stop playing
//----------------------

	if (mediaPlayer) {

//                libvlc_media_player_stop (mediaPlayer);

#ifdef VLC4
		libvlc_media_player_stop_async (mediaPlayer);
#else
		libvlc_media_player_stop (mediaPlayer);
#endif


//--------------------------------------
//                detach equalizer
//--------------------------------------

		libvlc_media_player_set_equalizer(mediaPlayer,  NULL);

//---------------------------
//	release equalizer
//---------------------------

		libvlc_audio_equalizer_release(equalizer);
		equalizer = 0;

//----------------------------
//	Free the media_player
//----------------------------

		libvlc_media_player_release (mediaPlayer);
		mediaPlayer = (libvlc_media_player_t *) 0; // wipe the pointer
		}

	if (inst) {
		libvlc_release (inst);
		inst = (libvlc_instance_t *) 0;
		}

	if (AGC) { // only reset if AGC
		SP->volumeLevel = INIT_VOLUME;
		outVolume(SP->volumeLevel);
		}

	setVlcRate(1.0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON(vlcSpinRate), 100.);

	return EXIT_SUCCESS;
	}

gboolean        on_vlcPosition_scroll_event(GtkEntry *b, GdkEventScroll *e) {

//--------------------------------------------------------------
//      see GdkEventScroll data structure for valid elements
//      see: gtk_widget_set_events(g_buttonxx,GDK_SCROLL_MASK);
//--------------------------------------------------------------

//----------------------------
//      get current value
//----------------------------

	gdouble x = (int) gtk_adjustment_get_value(adjustment8);

	x = x - e->delta_y ; // opposite
	gtk_adjustment_set_value (GTK_ADJUSTMENT(adjustment8), x);

	return FALSE;
	}


