/***************************************************************************************
 *
 * debug-angles.cc is part of SimpleSkyCam.
 *
 *****************************************************************************************/

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <math.h>

#include "debug.h"
#include "gui.h"

position_f_2d dbga_axis;
position_f_2d dbga_pos;
double dbga_angle = NAN;
double dbga_len = NAN;

#define DBGA_W 200
#define DBGA_H 200

void debug_angles_calculate() {
	position_f_2d center;

	center.x = DBGA_W >> 1;
	center.y = DBGA_H >> 1;

	dbga_len = dbga_axis.perpendicular(dbga_pos, center);
}


void debug_angles_draw(cairo_t *cr) {
	int centerX = DBGA_W >> 1;
	int centerY = DBGA_H >> 1;
	position_f_2d v;
	double d, a;

	debug_angles_calculate();

	cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
	cairo_set_line_width(cr, 1.0);
	cairo_move_to(cr, 0, 0);
	cairo_line_to(cr, DBGA_W, 0);
	cairo_line_to(cr, DBGA_W, DBGA_H);
	cairo_line_to(cr, 0, DBGA_H);
	cairo_stroke(cr);

	if (isnan(dbga_axis.x)) return;

	// len
	cairo_set_source_rgb(cr, 0.0, 0.0, 1.0);
	draw_printf(cr, 100, 10, 0.0, (char*)"Len: %5.2f", dbga_len);

	// draw pos
	cairo_set_source_rgb(cr, 0.3, 0.3, 1.0);
	draw_printf(cr, 0, 10, 0.0, (char*)"Pos: %5.1f,%5.1f", dbga_pos.x, dbga_pos.y);
	cairo_set_line_width(cr, 3.0);
	cairo_move_to(cr, centerX, centerY);
	cairo_line_to(cr, dbga_pos.x, dbga_pos.y);
	cairo_stroke(cr);

	cairo_set_line_width(cr, 1.0);
	if (dbga_len >= 0.0) 	cairo_set_source_rgb(cr, 1.0, 0.5, 0.5);
	else 					cairo_set_source_rgb(cr, 0.5, 1.0, 0.5);
	d = hypot(dbga_axis.x, dbga_axis.y);
	v.x = dbga_len * dbga_axis.y / d;
	v.y = dbga_len * (-dbga_axis.x) / d;
	cairo_move_to(cr, dbga_pos.x, dbga_pos.y);
	cairo_line_to(cr, dbga_pos.x + v.x, dbga_pos.y + v.y);
	cairo_stroke(cr);

	// draw axis
	a = 180.0 * atan2(dbga_axis.x, dbga_axis.y) / M_PI;
	cairo_set_source_rgb(cr, 0.5, 0.5, 0.5);
	draw_printf(cr, 0, 25, 0.0, (char*)"Axis: %5.1f,%5.1f", dbga_axis.x, dbga_axis.y);
	cairo_set_source_rgb(cr, 0.5, 0.5, 0.5);
	draw_printf(cr, 0, 40, 0.0, (char*)"Axis: %5.1f", a);
	cairo_set_line_width(cr, 3.0);
	cairo_move_to(cr, centerX, centerY);
	cairo_line_to(cr, centerX + dbga_axis.x, centerY + dbga_axis.y);
	cairo_stroke(cr);

	cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
	cairo_set_line_width(cr, 1.0);
	cairo_move_to(cr, centerX, centerY);
	cairo_line_to(cr, centerX + sindeg(a) * (double)centerX * 0.8, centerY + cosdeg(a) * (double)centerX * 0.8);
	cairo_stroke(cr);


	cairo_set_line_width(cr, 1.0);
	d = hypot(dbga_axis.x, dbga_axis.y);
	v.x = centerX * dbga_axis.x / d;
	v.y = centerY * dbga_axis.y / d;
	cairo_move_to(cr, centerX - v.x, centerY - v.y);
	cairo_line_to(cr, centerX + v.x, centerY + v.y);
	cairo_stroke(cr);
};


void debug_angles_motionevent(GdkEvent *event) {
	if (event->motion.x < 0 || event->motion.x >= DBGA_W
		|| event->motion.y < 0 || event->motion.y >= DBGA_H) return;

	dbga_pos.x = event->motion.x;
	dbga_pos.y = event->motion.y;
};


void debug_angles_btnpress(GdkEvent *event) {
	if (event->motion.x < 0 || event->motion.x >= DBGA_W
		|| event->motion.y < 0 || event->motion.y >= DBGA_H) return;

	dbga_axis.x = event->motion.x - (DBGA_W >> 1);
	dbga_axis.y = event->motion.y - (DBGA_H >> 1);


};

