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

//	Nov 23, 2021

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

#define SLASH '/'


//---------------------------------------------------------------
//      AGC gain rate
//---------------------------------------------------------------

void    on_maxGain_value_changed(GtkSpinButton *s) {
	Topgain  = gtk_spin_button_get_value (s);
	if (Topgain < 1) Topgain = 1;
	}


void    on_rampUp_value_changed(GtkSpinButton *s, GtkScrollType scr) {
	RAMP_SPEED_UP  = gtk_spin_button_get_value (s) / 10.;
	if (RAMP_SPEED_UP < 0.01) RAMP_SPEED_UP = 0.01;
	}


void    on_audioCalibrate_value_changed(GtkSpinButton *s, GtkScrollType scr) {
	AudioCalibrate  = gtk_spin_button_get_value (s) / 100.0 ;
	if (AudioCalibrate < .2) AudioCalibrate = .2;
	}


void    on_rampDown_value_changed(GtkSpinButton *s, GtkScrollType scr) {
	RAMP_SPEED_DOWN  = gtk_spin_button_get_value (s) / 10.;
	if (RAMP_SPEED_DOWN < 0.01) RAMP_SPEED_DOWN = 0.01;
	}


void    on_clipThreshold_value_changed(GtkSpinButton *s, GtkScrollType scr) {
	ClipLevel  = gtk_spin_button_get_value (s);
	if (ClipLevel < 50.) ClipLevel = 50.;
	}

//---------------------------------------------------------------
//      AGC gain drop
//---------------------------------------------------------------

void    on_rampSpeedDown_value_changed(GtkSpinButton *spin_button, GtkScrollType  scroll) {
	RAMP_SPEED_DOWN = gtk_spin_button_get_value (spin_button);
	}

//----------------------------------------------------------------
//      AGC gain rate output signal Down (format)
//----------------------------------------------------------------

gboolean on_rampSpeedDown_output (GtkSpinButton *spin) {
	GtkAdjustment *adjustment;
	gchar *text;
	int value;
	adjustment = gtk_spin_button_get_adjustment (spin);
	value = (int)gtk_adjustment_get_value (adjustment);
	text = g_strdup_printf ("%3d%%", value);
	gtk_entry_set_text (GTK_ENTRY (spin), text);
	g_free (text);
	return TRUE;
	}

gboolean talkPush (GtkWidget *widget, GdkEventKey *event, gpointer data) {

	if (event->keyval == talk_button || event->keyval == talk_button1) {
		printf("TALK KEY PRESSED!\n");
		gtk_spin_button_set_value (GTK_SPIN_BUTTON(micThreshold1), mic_max);
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(micMute), TRUE);
		return TRUE;
		}

	return FALSE;
	}

gboolean talkRelease (GtkWidget *widget, GdkEventKey *event, gpointer data) {

	if (event->keyval == talk_button || event->keyval == talk_button1) {
		printf("TALK KEY RELEASED!\n");
		gtk_spin_button_set_value (GTK_SPIN_BUTTON(micThreshold1), 0.0);
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(micMute), FALSE);
		return TRUE;
		}

	return FALSE;
	}

//--------------------------------------------------------
//
//      Lower/resume music. Optionally: rase/lower mic
//
//--------------------------------------------------------

void    on_micMute_toggled (GtkButton *b) {

	static int old = 100;

	if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(b)) ) { // unmute
		micmute = 0;
		SP->volumeLevel = INIT_VOLUME ;
		outVolume(SP->volumeLevel); //music on - start low and let it rise
		MaxAudioSample = 0; // restart volume seek
		gtk_adjustment_set_value (GTK_ADJUSTMENT(adjustment10), SP->volumeLevel - 100);
		}

	else {  // mute music.
		micmute = 1;
		SP->volumeLevel = SWEEPER_REDUCE;
		outVolume(SP->volumeLevel);
		gtk_adjustment_set_value (GTK_ADJUSTMENT(adjustment10), SP->volumeLevel - 100);
		}
	}


gboolean on_preAmp_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(adjustment10);

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


	return FALSE;
	}


gboolean on_preAmp_change_value(GtkRange *r) {
	gdouble x = gtk_range_get_value(r);
	x = round(x + 100.0);
	gtk_adjustment_set_value (GTK_ADJUSTMENT(adjustment10), x);
	SP->volumeLevel = x;
	return FALSE; // continue processing
	}

void	reset_last() {
	for (int i = 0; i< 100; i++) {
		lastR1[i] = 0;
		lastR2[i] = 0;
		lastR3[i] = 0;
		lastR4[i] = 0;
		lastR5[i] = 0;
		lastR6[i] = 0;
		lastR7[i] = 0;
		lastR8[i] = 0;
		lastR9[i] = 0;
		lastRA[i] = 0;
		}
	}


void    on_spec20_toggled(GtkToggleButton *b) {
	if (gtk_toggle_button_get_active (b) ) {
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(meters), TRUE); // emits signal
		style = WIDE_SPECTRUM;
		reset_last();
		}
	}



void    on_spec12_toggled(GtkToggleButton *b) {
	if (gtk_toggle_button_get_active (b) ) {
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(meters), TRUE); // emits signal
		style = NARROW_SPECTRUM;
		reset_last();
		}
	}

void    on_spec7_toggled(GtkToggleButton *b) {
	if (gtk_toggle_button_get_active (b) ) {
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(meters), TRUE); // emits signal
		style = VERY_NARROW_SPECTRUM;
		reset_last();
		}
	}


//----------------------------------------------------------
//      auto gain control 1 button toggled (check/uncheck)
//----------------------------------------------------------

void    on_agc_toggled(GtkWidget *b) {

	if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(b)) ) { // stop AGC
		if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(agcMod)) ) {
			gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(agcMod), FALSE); // emits signal
			AGC1 = 0;
			}
		AGC = 0;
		}
	else {
		AGC = 1;
		MaxAudioSample = 0; // an excessive  max has not been found yet
		}
	}


//-----------------------------------------------------------
//      auto gain control 2 button toggled (check/uncheck)
//-----------------------------------------------------------

void    on_agcMod_toggled(GtkToggleButton *b) {

	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(b))) {
		AGC1 = 1;
		if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(agc)) ) { // start AGC
			gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(agc), TRUE); // emits signal
			}
		}
	else {
		AGC1 = 0;
		}
	}



//----------------------------------------------------------------
//      AGC gain rate output signal Up (format)
//----------------------------------------------------------------

gboolean on_rampSpeedUp_output (GtkSpinButton *spin) {
	GtkAdjustment *adjustment;
	gchar *text;
	int value;
	adjustment = gtk_spin_button_get_adjustment (spin);
	value = (int)gtk_adjustment_get_value (adjustment);
	text = g_strdup_printf ("%3d%%", value);
	gtk_entry_set_text (GTK_ENTRY (spin), text);
	g_free (text);
	return TRUE;
	}

//---------------------------------------------------------
//      Set VLC master volume level - not same as preamp
//---------------------------------------------------------

void    on_masterVolume_value_changed(GtkRange *r) {

//-----------------------------------------------
//      k is percent <0:100:200> - nominally 100.
//-----------------------------------------------

	master_volume = gtk_range_get_value (r); // remember value from slider
	int k = (int) master_volume;

//----------------------------------------------------------
//      Set master vlc api volume directly - this is noisy
//----------------------------------------------------------

	char tmp[32];

	if (mediaPlayer) // can't be set until there is a player
		libvlc_audio_set_volume(mediaPlayer, k);

	sprintf(tmp,"%+d%%", k);
	gtk_label_set_text(GTK_LABEL(masterVolLevel), tmp);
	return;
	}

void    on_mediaGain_value_changed(GtkSpinButton *s, GtkScrollType scr) {
	SWEEPER_REDUCE  = gtk_spin_button_get_value (s);
	}


