/*************************************************************************************** 
 *
 * main.cc is part of SimpleSkyCam.
 * 
 *****************************************************************************************/

#include <sys/time.h>
#include <math.h>

#include "simpleskycam.h"
#include "config.h"
#include "configuration.h"
#include "gui.h"
#include "output.h"
#include "detect.h"
#include "convert.h"

/**************************************************************************
 * global variables
 **************************************************************************/

GtkBuilder *_builder_ = NULL;		// work around for the thread situation
Output output;
Detect detect;
Configuration config;
PosCtl posctl;

int main (int argc, char **argv) {
	GtkBuilder *builder;
	GObject *window;

#ifdef BUILD_WINDOWS
	char buffer[16];
	setvbuf (stdout, buffer, _IONBF, 16);
#endif
	
	printf ("SimpleSkyCam - %s\n", VERSION);
	printf ("  written by Steffen Pohle <steffen@gulpe.de>\n");
	printf ("\n");
	printf ("Command Line Options for testing purpose:\n");
	printf (" -d <debugpath>     destination for video debugging path\n");
	printf (" -dd                disable debugging path\n");
	printf (" -dw                show debug window\n");
	printf ("\n");
	printf (" -rd <debugpath>    read video from dumpfile folder\n");
	printf ("\n");
	printf ("\n");

	for (int i = 1; i < argc; i++) {
		if (strcmp (argv[i], "-d") == 0) {
			i++;
			config.debugpath = argv[i];
		}
		if (strcmp (argv[i], "-dd") == 0) {
			config.debugpath = NULL;
		}
		if (strcmp (argv[i], "-dw") == 0) {
			config.show_debugwin = 1;
		}
		if (strcmp (argv[i], "-rd") == 0) {
			i++;
			config.readdumppath = argv[i];
		}
	}

	gtk_init (&argc, &argv);
	_builder_ = builder = gtk_builder_new ();
	gtk_builder_add_from_file (builder, BUILDER_FILE, NULL);
	gtk_builder_connect_signals(builder, builder);

	window = gtk_builder_get_object (builder, "window-main");
	if(window == NULL) {
	  printf("ERROR: gtk_builder_get_object() failed\n");
	  return 1;
	}
	gtk_widget_show_all (GTK_WIDGET(window));

	gtk_main ();

	return 0;
}


float get_cycletime(struct timeval *t) {
	struct timeval t1;
	float f = 0.0;

	t1 = *t;
	gettimeofday(t, NULL);
	f = (float)(t->tv_sec - t1.tv_sec) + ((t->tv_usec - t1.tv_usec) / 1000000.0);

	return f;
}



void calc_vec2anglelen(position_f_2d *p, vector_2d *v) {
	if (v == NULL || p == NULL) return;
	v->a = atan2(p->x, p->y) * 180.0 / M_PI;
	while (v->a < 0.0) v->a += 360.0;
	v->l = hypot(p->x, p->y);

}



