/*************************************************************************************** 
 *
 * gui.h is part of SimpleSkyCam.
 *
 *****************************************************************************************/

#ifndef _GUI_H_
#define _GUI_H_

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

#include <math.h>
#include <string>
#include <iostream>
#include <list>

#define LEN_FILENAME 64
#define LEN_FULLFILENAME 256

enum {
	VID_ENTERDATA_NONE = 0,
	VID_ENTERDATA_POS,
	VID_ENTERDATA_SETDEST
};

#define BUILDER_FILE "simpleskycam.ui"

class position_2d {
public:
	position_2d() { x = 0; y = 0; };
	~position_2d() {};

	position_2d operator + (position_2d const &obj) {
	         position_2d res;
	         res.x = x + obj.x;
	         res.y = y + obj.y;
	         return res;
	    }

	position_2d operator - (position_2d const &obj) {
	         position_2d res;
	         res.x = x - obj.x;
	         res.y = y - obj.y;
	         return res;
	    }

	int x;
	int y;
};


class position_f_2d {
public:
	position_f_2d() { x = NAN; y = NAN; };
	~position_f_2d() {};

	position_f_2d operator + (position_f_2d const &obj) {
	         position_f_2d res;
	         res.x = x + obj.x;
	         res.y = y + obj.y;
	         return res;
	    }

	position_f_2d operator - (position_f_2d const &obj) {
	         position_f_2d res;
	         res.x = x - obj.x;
	         res.y = y - obj.y;
	         return res;
	    }

	double cosalpha(position_f_2d &pos) {
		return (x * pos.x + y * pos.y) / (length() * pos.length());
	}

	double sinalpha(position_f_2d &pos) {
		return (x * pos.y - y * pos.x) / (length() * pos.length());
	}

	double perpendicular(position_f_2d &pos, position_f_2d &base) {
		position_f_2d p = pos - base;
		double l = p.length();
		return l == 0.0 ? 0.0 : l * sinalpha(p);
	}

	double length() { return hypot(x, y); }

	double x;
	double y;
};


class vector_2d {
public:
	double a;
	double l;
	vector_2d () { a = NAN; l = NAN; }
	~vector_2d () {};
};


#define DETECT_MOVEMENT_SAMPLES 50
struct {
	struct {
		struct timeval ts;
		int x;
		int y;
	} samples[DETECT_MOVEMENT_SAMPLES];
	float dx;
	float dy;
} typedef detect_movement;

void draw_text (cairo_t *cr, int x, int y, float border, std::string text);
void draw_printf(cairo_t *cr, int x, int y, float border, char *fmt,...);
#define gtk_entry_set_text_nofocus(__w__,__t__)  if (!gtk_widget_has_focus(__w__)) gtk_entry_set_text (GTK_ENTRY(__w__), __t__)


#ifdef __cplusplus
extern "C" {
#endif


// ***********************************************************************
//
// main windows call backs
//
G_MODULE_EXPORT void cb_window_show  (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT gboolean cb_window_delete_event (GtkWidget *widget,
				GdkEvent *event, gpointer data);


//
// video and video devices
G_MODULE_EXPORT void cb_video_cbox_videodev  (GtkWidget *widget, gpointer data);

G_MODULE_EXPORT void cb_video_btnrefreshlist  (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_video_btnrec  (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_video_btnstop (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_videoda_draw(GtkWidget *area, cairo_t *cr, int w, int h, gpointer data);
G_MODULE_EXPORT gboolean video_display(gpointer data);
G_MODULE_EXPORT void cb_videoda_motionevent (GtkWidget *widget, GdkEvent *event, gpointer data);
G_MODULE_EXPORT void cb_videoda_btnpress (GtkWidget *widget, GdkEvent *event, gpointer data);
G_MODULE_EXPORT void cb_videoda_btnrelease (GtkWidget *widget, GdkEvent *event, gpointer data);
G_MODULE_EXPORT void cb_vidctrl_entry_change (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_vidctrl_scale_change (GtkRange *range, gpointer data);
gboolean videoctrl_update(gpointer data);

// preset buttons
G_MODULE_EXPORT void cb_video_pre_click (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_video_pre_pressed (GtkWidget *widget, gpointer data);
gboolean video_pre_long(gpointer data);

//
// thread new inframe
G_MODULE_EXPORT gboolean cb_thread_video (gpointer data);
G_MODULE_EXPORT void cb_input_show_window (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_input_btncapture (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_input_btnsnapshot (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_input_btnstop (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_input_btnscale (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_input_btnsetdestpath (GtkWidget *widget, gpointer data);

//
// filter detect or image data
G_MODULE_EXPORT void cb_imagetempda_draw(GtkWidget *area, cairo_t *cr, int w, int h, gpointer data);
G_MODULE_EXPORT void cb_image_btnnew (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_image_btnsave (GtkWidget *widget, gpointer data);


//
// detection elements
G_MODULE_EXPORT void cb_detect_bright (GtkRange *range, gpointer data);
G_MODULE_EXPORT void cb_detect_btnsetpos (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_detect_btnsetsize (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_detect_followtype (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_detect_detecttype (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_detect_show_window (GtkWidget *widget, gpointer data);


//
// histogram elements
G_MODULE_EXPORT void cb_histogramda_draw(GtkWidget *area, cairo_t *cr, int w, int h, gpointer data);
G_MODULE_EXPORT void cb_histogramda_motion (GtkWidget *widget, GdkEvent *event, gpointer data);
G_MODULE_EXPORT void cb_histogramda_btnpress (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_histogramda_btnrelease (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_histogramda_keypress (GtkWidget *widget, GdkEventKey *event, gpointer data);
G_MODULE_EXPORT void cb_histogram_show_window (GtkWidget *widget, gpointer data);

//
// position control elements
G_MODULE_EXPORT void cb_posctl_show (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_posctl_show_window (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_posctl_btncalib (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_posctl_btnstop (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_posctl_btnsetdest (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_posctl_btncontrol (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_posctl_entryanglelen (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_posctl_change_entry (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_posctl_btn_axismove (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_posctl_btnsimreset (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_posctl_angles_draw (GtkWidget *area, cairo_t *cr, int w, int h, gpointer data);
G_MODULE_EXPORT void cb_posctl_axis_draw(GtkWidget *area, cairo_t *cr, int w, int h, gpointer data);
G_MODULE_EXPORT void cb_posctl_track_draw(GtkWidget *area, cairo_t *cr, int w, int h, gpointer data);
G_MODULE_EXPORT void cb_posctl_btn2steprun (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT void cb_posctl_btn2stepreset (GtkWidget *widget, gpointer data);


//
// menu elements
G_MODULE_EXPORT void cb_menu_set_rgbenc (GtkCheckMenuItem *checkmenuitem,  gpointer user_data);
G_MODULE_EXPORT void cb_menu_set_histlog (GtkCheckMenuItem *checkmenuitem,  gpointer user_data);
G_MODULE_EXPORT void cb_menu_set_displaycalibdata(GtkCheckMenuItem *checkmenuitem,  gpointer user_data);

//
// error handling and thread handling
G_MODULE_EXPORT gboolean errormessage_thread (gpointer data);
G_MODULE_EXPORT void errormessage_cb_btnok (GtkWidget *widget, gpointer data);
G_MODULE_EXPORT gboolean debug_thread_cb (gpointer data);

G_MODULE_EXPORT gboolean cb_thread_detect (gpointer data);
G_MODULE_EXPORT gboolean cb_thread_posctl (gpointer data);
G_MODULE_EXPORT gboolean cb_thread_output (gpointer data);

#ifdef __cplusplus
}
#endif

extern float get_cycletime(struct timeval *t);

#endif // _GUI_H_

