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

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

#define SLASH '/'

void	tower_back(cairo_t *cr, int panel) {

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

//-----------------------------------------
//	if no background pics not checked
//-----------------------------------------

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

		if (panel == SMALL_PANEL) {

			if (imageS == NULL) {

				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 (imageL == NULL) {

				char png_file[2048];
				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 { // no background pics box checked

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



	//not currently in use:
	//w = cairo_image_surface_get_width (image);
	//h = cairo_image_surface_get_height (image);
	//cairo_translate (cr, 128.0, 128.0);
	//cairo_rotate (cr, 45* M_PI/180);
	//cairo_scale  (cr, 400.0/w, 256.0/h);
	//cairo_translate (cr, -0.5*w, -0.5*h);

	}


void	build_tower(cairo_t *cr, double hx, double vx, double gx, double tall, double base, int panel) {

	if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(antenna))) return;

	double line_width = cairo_get_line_width(cr);

	cairo_new_path(cr); // ball
	cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);

	cairo_arc (cr, hx, vx-6., 4.0, -M_PI, M_PI );
	cairo_line_to (cr, hx, vx );
	cairo_fill(cr); // fill in arc

	cairo_stroke (cr);

	if (panel == SMALL_PANEL) {
		cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
		cairo_set_line_width(cr, 4.0);
		}
	else {
		cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
		cairo_set_line_width(cr, 5.0);
		}

// main

// if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(noBackPic)))

	cairo_set_source_rgb(cr, 0.5, 0.5, 0.5);

// 	else

// 	cairo_set_source_rgb(cr, 0.1, 0.1, 0.5);


	cairo_new_path(cr); // feet
	cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
	cairo_move_to(cr, hx - base -4.0, vx + tall- 7.0);
	cairo_line_to (cr, hx - base + 4.0, vx +tall - 7.0 );

	cairo_move_to(cr, hx + base - 4.0, vx + tall- 7.0);
	cairo_line_to (cr, hx + base + 4.0, vx + tall -7.0 );

	cairo_move_to(cr, hx -4.0, vx + tall);
	cairo_line_to (cr, hx +4.0, vx + tall );

	cairo_stroke (cr);

	cairo_new_path(cr); // left leg

	cairo_move_to(cr, hx - base, vx + tall- 7.0);
	cairo_line_to (cr, hx, vx );
	cairo_stroke (cr);

	cairo_new_path(cr); // right leg
	cairo_move_to(cr, hx + base, vx + tall- 7.0);
	cairo_line_to (cr, hx, vx );
	cairo_stroke (cr);

	cairo_new_path(cr);  // center leg
	cairo_move_to(cr, hx, vx + tall);
	cairo_line_to (cr, hx, vx );
	cairo_stroke (cr);


	cairo_new_path(cr); // base to left leg
	cairo_move_to(cr, hx, vx + tall); // center leg
	cairo_line_to (cr, hx - base / 2.0, vx + tall / 2.0 ); // to left leg
	cairo_line_to (cr, hx + base / 3.0, vx + tall / 3.3 );
	cairo_move_to(cr, hx, vx + tall);
	cairo_stroke (cr);

	cairo_new_path(cr); // base to right leg
	cairo_move_to(cr, hx, vx + tall);
	cairo_line_to (cr, hx + base / 2.0, vx + tall / 2.0 );
	cairo_line_to (cr, hx - base / 3.0, vx + tall / 3.3 );
	cairo_stroke (cr);

	cairo_new_path(cr);
	cairo_move_to(cr, hx + base, vx + tall - 7.0);
	cairo_line_to (cr, hx, vx + tall / 2.0 );
	cairo_stroke (cr);

	cairo_new_path(cr);
	cairo_move_to(cr, hx - base, vx + tall- 7.0);
	cairo_line_to (cr, hx, vx + tall / 2.0 );
	cairo_stroke (cr);
// }

// shadow

	if (panel == SMALL_PANEL) {
		cairo_set_source_rgb(cr, 0.0, 0.0, 0.4);
		cairo_set_line_width(cr, 1.0);
		}
	else {
		cairo_set_source_rgb(cr, 0.0, 0.0, 0.4);
		cairo_set_line_width(cr, 2.);
		}

// if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(noBackPic))) {

	cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);

	cairo_new_path(cr); // feet
//	cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
	cairo_move_to(cr, hx - base -4.0, vx + tall- 7.0);
	cairo_line_to (cr, hx - base + 4.0, vx +tall - 7.0 );

	cairo_move_to(cr, hx + base - 4.0, vx + tall- 7.0);
	cairo_line_to (cr, hx + base + 4.0, vx + tall -7.0 );

	cairo_move_to(cr, hx -4.0, vx + tall);
	cairo_line_to (cr, hx +4.0, vx + tall );

	cairo_stroke (cr);

	cairo_new_path(cr); // left leg

	cairo_move_to(cr, hx - base, vx + tall- 7.0);
	cairo_line_to (cr, hx, vx );
	cairo_stroke (cr);

	cairo_new_path(cr); // right leg
	cairo_move_to(cr, hx + base, vx + tall- 7.0);
	cairo_line_to (cr, hx, vx );
	cairo_stroke (cr);

	cairo_new_path(cr);  // center leg
	cairo_move_to(cr, hx, vx + tall-6); // -6 is height adjustment
	cairo_line_to (cr, hx, vx );
	cairo_stroke (cr);


	cairo_new_path(cr); // base to left leg
	cairo_move_to(cr, hx, vx + tall); // center leg
	cairo_line_to (cr, hx - base / 2.0, vx + tall / 2.0 ); // to left leg
	cairo_line_to (cr, hx + base / 3.0, vx + tall / 3.3 );
	cairo_move_to(cr, hx, vx + tall);
	cairo_stroke (cr);

	cairo_new_path(cr); // base to right leg
	cairo_move_to(cr, hx, vx + tall);
	cairo_line_to (cr, hx + base / 2.0, vx + tall / 2.0 );
	cairo_line_to (cr, hx - base / 3.0, vx + tall / 3.3 );
	cairo_stroke (cr);

	cairo_new_path(cr);
	cairo_move_to(cr, hx + base, vx + tall - 7.0);
	cairo_line_to (cr, hx, vx + tall / 2.0 );
	cairo_stroke (cr);

	cairo_new_path(cr);
	cairo_move_to(cr, hx - base, vx + tall- 7.0);
	cairo_line_to (cr, hx, vx + tall / 2.0 );
	cairo_stroke (cr);

// }

	cairo_set_line_width(cr, line_width);  // restore
	return;

//---------------------------------------------------------------------------

	cairo_new_path(cr);

	cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
	                        CAIRO_FONT_WEIGHT_BOLD);

	static int flsh = 0;

	flsh=flsh + 1;

	if (flsh > 20) flsh = 0;

	if (flsh < 10) {

		if (panel == SMALL_PANEL) {
			cairo_move_to (cr, hx-22., vx+70. ); // top
			}

		else {
			cairo_move_to (cr, hx-33., vx-12. ); // top
			cairo_line_to (cr, hx-20., vx - 20.);
			cairo_stroke (cr);
			}

		}



	return;

	cairo_set_font_size(cr, 20.0);
	cairo_show_text(cr, "WSGR");
	cairo_stroke (cr);
	cairo_set_line_width(cr, line_width);

	}


void	tower_show(cairo_t *cr, int i, int panel, double slchan[], double srchan[],
                   double D1_DIVISOR, double gx1, double hx, double vx, int LeftFlg) {

	double rd, gd, bd, ag, x, y, x1, y1;
	static double Origin = M_PI, Origin1 = M_PI;

	if (rayRotate > 0) {
		Origin = Origin + M_PI / rayRotate;
		if (Origin > 2 * M_PI  ) Origin = M_PI;
		}
	else Origin = M_PI;

	Origin1 = Origin;

	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(randColors)))
		rand_color_generator(cr, i, &rd, &gd, &bd);
	// rainbow color in rd, rg, and rb
	else
		color_generator(cr, i, &rd, &gd, &bd);
	// freq based colors

//------------------------------------
//			tower spikes
//------------------------------------

#define SMALL_TOWER_SPIKE_WIDTH 2.0
#define LARGE_TOWER_SPIKE_WIDTH 3.0

	cairo_set_source_rgb(cr, rd, gd, bd);

//----------------------------------------------------------------
//			note: 'meters' is the Spectrum checkbox
//----------------------------------------------------------------

	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(meters)))  {
		D1_DIVISOR = 2.0;
		ag = gx1 * (srchan[i] + slchan[i]) / D1_DIVISOR; // spectrum
		}
	else {
		D1_DIVISOR = 1.0;
		ag = gx1 * (srchan[i] + slchan[i]) / D1_DIVISOR; // amplitude
		}

	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tower_rays))) {
		cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);


		cairo_arc(cr, hx, vx,
		          ag / 2.0,
		          -Origin, -Origin + (i+1)/100.0 * 2.0 * Origin );
//					-M_PI, -M_PI + (i+1)/100.0 * 2.0 * M_PI );

		cairo_get_current_point (cr, &x, &y);
		cairo_new_path(cr);
		cairo_move_to(cr, hx, vx);
		cairo_line_to (cr, x, y );
		cairo_stroke (cr);

		cairo_arc(cr, hx, vx,
		          ag / 3.0,
		          -Origin, -Origin + (i+.2)/100.0 * 2.0 * Origin1 );
//					-M_PI, -M_PI + (i+.2)/100.0 * 2.0 * M_PI );

		cairo_get_current_point (cr, &x1, &y1);
		cairo_new_path(cr);
		cairo_move_to(cr, x, y);
		cairo_line_to (cr, x1, y1 );
		cairo_stroke (cr);

		cairo_arc(cr, hx, vx,
		          ag / 1.6,
		          -Origin, -Origin + (i+.2)/100.0 * 2.0 * Origin1 );
//					-M_PI, -M_PI + (i+.2)/100.0 * 2.0 * M_PI );
		cairo_get_current_point (cr, &x, &y);
		cairo_new_path(cr);
		cairo_move_to(cr, x1, y1);
		cairo_line_to (cr, x, y );

		cairo_stroke (cr);
		}

	if (LeftFlg == 0 && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(traces)))
		sprinkles(cr, i / 50.0 * M_PI, ag / 2.0, i, hx, vx);

//-------------------------------
//			circles
//-------------------------------

	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tower_circles)))  {

		double z = M_PI;
		double b =1.25;
		double tower_wave_size;

//        			if (panel == SMALL_PANEL) cairo_set_line_width(cr, 2.0);
//        				else cairo_set_line_width(cr, 3.0);

//				if (panel == SMALL_PANEL) tower_wave_size = SMALL_TOWER_CIRCLE_FACTOR;
//					else tower_wave_size = LARGE_TOWER_CIRCLE_FACTOR;

		tower_wave_size = TOWER_CIRCLE_FACTOR;
		b=2.0;

		color_generator(cr, i, &rd, &gd, &bd); // rainbow color in rd, rg, and rb

#define TOWER_WAVE_VISIBILITY 0.85

		cairo_new_path(cr);
		cairo_set_source_rgba(cr, rd, gd, bd, TOWER_WAVE_VISIBILITY);  // tranparancy
		cairo_arc (cr, hx, vx -6.,
		           tower_wave_size * (srchan[i] +  slchan[i]) / 2.0,
		           - M_PI - b, - M_PI + z + b );
		cairo_stroke (cr);
		}

	}

