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

//	Oct 25, 2022

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

#define SLASH '/'

void	graph_manager(cairo_t *cr, GtkDrawingArea *widget, int noLine, int panel) {

	double hor = 0.0, ver = 0.0, len;
	double z, r, g, b, x, y, x1, y1, clip = 1.0 ;
	double D1_DIVISOR = 2.0;

	static cairo_surface_t *imageS = NULL, *imageL = NULL, *imageLogo = NULL;

	char png_file[2048];

//-------------------------------------------------------------
//	replace image in small panel if large panel is showing
//	image used is logo1small.png in icons
//-------------------------------------------------------------

//	if (!video_file) {
//
//		gtk_widget_hide(movie);
//
//		}

//-----------------------------------------------------------------------------
//	render special shape frequency display - not tower or horizontal graph
//-----------------------------------------------------------------------------

	if (!LeftFlg && (circular_graph || bulls_eye || bulls_eye1 ||
	                 show_spikes || show_spikes1)) {

		if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(noBackPic))) {

			if (panel == SMALL_PANEL )  {

				if (imageS == NULL) { // first time

					char png_file[2048];
					strcpy(png_file, Code_Home);
					strcat(png_file,"/icons/small462.png");
					imageS = cairo_image_surface_create_from_png (png_file);

					}

				cairo_set_source_surface (cr, imageS, 4, 4);
				cairo_paint (cr);
				}

			else  if (panel == LARGE_PANEL) {

				if (imageL == NULL) { // first time

					strcpy(png_file, Code_Home);
					strcat(png_file,"/icons/large462.png");
					imageL = cairo_image_surface_create_from_png (png_file);

					}

				cairo_set_source_surface (cr, imageL, 4, 4);
				cairo_paint (cr);
				}
			}

		else {

			cairo_set_source_rgb(cr, .0, .0, .0); // center line color
			cairo_fill(cr);
			cairo_paint (cr);
			}

//-----------------------------------------------------------------
//	render images - both panel sizes
//	drawS() draws cocentric, circles, bullseye.
//-----------------------------------------------------------------

		drawS (widget, cr, panel);

		make_sprinkles(D1_DIVISOR);

		return; // end of special shape renderings
		}


//----------------------------------------
//	horizontal graph & tower section
//	horizontal graph & tower section
//	horizontal graph & tower section
//----------------------------------------

//----------------------------------------------------------
//	frequency graph setup - otherwise data is volume
//----------------------------------------------------------

	if (graph_type == SPECTRUM) { // frequency graph

//-----------------------------------------
//	*** scaling is done in spectrum()
//-----------------------------------------
		spectrum();
		}

//---------------------------
//	volume graph setup
//---------------------------

	else { // volume level graph

#define LARGE_PANEL_SCALE 1.5

		if (super_bars)  // baseline graph

			for (int i=0; i<100; i++) { // scale and segregate data
				slchan[i] = lchan[i] * SCALE_BASELINE_VOLUME * AudioCalibrate ;
				srchan[i] = rchan[i] * SCALE_BASELINE_VOLUME * AudioCalibrate ;
				}

		else
			for (int i=0; i<100; i++) { // segregate data unscaled
				if (panel == SMALL_PANEL) {
					slchan[i] = lchan[i] * AudioCalibrate ;
					srchan[i] = rchan[i] * AudioCalibrate ;
					}
				else {
					slchan[i] = LARGE_PANEL_SCALE * lchan[i] * AudioCalibrate ;
					srchan[i] = LARGE_PANEL_SCALE * rchan[i] * AudioCalibrate ;
					}
				}
		}

//-------------------------------------------------------
//	begin section to draw horizontal graph or tower
//-------------------------------------------------------

	cairo_set_line_width(cr, GRAPH_BAR_WIDTH);

//---------------------------------
//	tower spike length factor
//---------------------------------

#define LARGE_TOWER_SPIKE 1.5
#define SMALL_TOWER_SPIKE 3.0

//--------------------------------------
//	default setuip for small tower
//--------------------------------------

	double hx = 190.0, vx = 80.0, gx1 = SMALL_TOWER_SPIKE;
	double tall = 75.0, base = 35.0;


	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tower))) {

		if (panel != SMALL_PANEL) {  // large tower
			hx = 320.0;
			vx = 230;
			gx1 = LARGE_TOWER_SPIKE;
			TOWER_CIRCLE_FACTOR = 0.92;
			tall = 250.0;
			base = 55.0;
			}

//----------------------------------------------------
//	small panel not spectrum - reduce spike size
//	'meters' is the spectrum button
//----------------------------------------------------

		else {
			if (! gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(meters))) // spectrum box
				gx1 = SMALL_TOWER_SPIKE / 3.0;

			if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(noBackPic))) {
				base = 1.;
				tall = 110.;
				vx = 145.0;
				TOWER_CIRCLE_FACTOR = 0.92;
				base = 65.0;
				}

			else
				TOWER_CIRCLE_FACTOR = 0.62;
			}

		tower_back(cr,panel); // load tower background image
		}


//------------------------------------
//	Loop thru the data
//	'ray_count' is the resolition
//------------------------------------

	if (! gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tower))) {

		cairo_set_source_rgb(cr, .0, .0, .0); // center line color
		cairo_fill(cr);
		cairo_paint (cr);

		}

	static unsigned long flipper = 0; // alternate front/back of tower

	flipper = flipper ^  1;


	if (flipper && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tower))) {

		if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(traces)))  {
			make_sprinkles(D1_DIVISOR);
			}

		build_tower(cr, hx, vx, gx1, tall, base, panel);
//		return;
		}


	for (int i = 0; i < 100; i = i + ray_count) {

		double x,y,ag;

//--------------------------------
//	show tower or graph?
//--------------------------------

		if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tower))) {
			tower_show(cr, i, panel, slchan, srchan, D1_DIVISOR, gx1, hx, vx, LeftFlg);
			continue;
			}

//-------------------
//	Bar Graphs
//-------------------

//-------------------------------
//	display baseline graph
//-------------------------------

		if ((!connect_graph || display_bars) && super_bars) {

			cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);

			if (slchan[i] > srchan[i] ) { // show prominent color
				color_bars(cr, i, noLine, YELLOW);	// left channel color
				bar_graph(UPPER, slchan, i, cr, connect_graph,
				          display_bars, YELLOW, noLine, panel); // left channel
				}
			else {
				color_bars(cr, i, noLine, RED);		// right channel color
				bar_graph(LOWER, srchan, i, cr, connect_graph,
				          display_bars, RED, noLine, panel); // right channel
				}
			}

//----------------------------
//	display midline graph
//----------------------------

		else {

			cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);

			color_bars(cr, i, noLine, YELLOW);	// left channel
			bar_graph(UPPER, slchan, i, cr, connect_graph, display_bars, YELLOW, noLine, panel);

			color_bars(cr, i, noLine, RED);		// right channel
			bar_graph(LOWER, srchan, i, cr, connect_graph, display_bars, RED, noLine, panel);
			}

		} // loop end

	if (flipper) if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tower))) return;

	if (!flipper && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tower))) {

		if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(traces)))  {
			make_sprinkles(D1_DIVISOR);
			}

		build_tower(cr, hx, vx, gx1, tall, base, panel);
		return;
		}

//-------------------------------------------------
//	midpoint line - none if not centered graph
//-------------------------------------------------

	if (!super_bars) {	// mid point line of centered graph
		cairo_set_line_width(cr, 2.0);
		cairo_set_source_rgb(cr, .6, .6, .6); // center line color
		cairo_move_to (cr, (double) G_OFF - 0, DRAW_HEIGHT / 2.0 - 12.0 );

		if (panel == SMALL_PANEL)
			cairo_line_to (cr, (double) G_OFF + 392, DRAW_HEIGHT / 2.0 - 12.0 );
		else
			cairo_line_to (cr, (double) G_OFF + 590, DRAW_HEIGHT / 2.0 - 12.0 );

		cairo_stroke (cr);
		}

//---------------------------------
//	frequency legend markers
//---------------------------------

	if (LeftFlg == 0) {

		if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(meters)))
			frequency_marks(cr);
		make_sprinkles(D1_DIVISOR);
		}

	else leftFlgSubText(cr);

	}

//-----------------------------------------------------------------------------
//	for future reference - not currently used
//	guint width, height;
//	width = gtk_widget_get_allocated_width (widget);   // of draw window
//	height = gtk_widget_get_allocated_height (widget); // of draw window
//-----------------------------------------------------------------------------

void	make_sprinkles(double D1_DIVISOR) {

//		if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(above_bars))) {
//			D1_DIVISOR = 2.0;
//			}

	for (int i=0; i<99; i++) {
		double trace_start = radialStart  - 8 * radialDecay;
		lastRA[i] = lastR9[i] * (trace_start = trace_start + radialDecay);
		lastR9[i] = lastR8[i] * (trace_start = trace_start + radialDecay);
		lastR8[i] = lastR7[i] * (trace_start = trace_start + radialDecay);
		lastR7[i] = lastR6[i] * (trace_start = trace_start + radialDecay);
		lastR6[i] = lastR5[i] * (trace_start = trace_start + radialDecay);
		lastR5[i] = lastR4[i] * (trace_start = trace_start + radialDecay);
		lastR4[i] = lastR3[i] * (trace_start = trace_start + radialDecay);
		lastR3[i] = lastR2[i] * (trace_start = trace_start + radialDecay);
		lastR2[i] = lastR1[i] * (trace_start);
		lastR1[i] = (srchan[i] + slchan[i]) / D1_DIVISOR;
		}
	}

