/*
 * Copyright (C) 2014 Julian Lewis
 * @author Theodor Stana <theodor.stana@gmail.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, you may find one here:
 * http://www.gnu.org/licenses/gpl-3.0-standalone.html
 * or you may search the http://www.gnu.org website for the version 3 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <string.h>
#include <stdio.h>

#include <event.h>

#include <drivers/lcd.h>
#include <gfx/graphics.h>
#include <gfx/ui.h>
#include "widgets/status_bar.h"

#include <bitmaps.h>

#include <drivers/gps.h>
#include <libfwatch.h>
#include "application.h"

#include "settings/settings.h"

application gpscoord;

static struct gps_coord coord;
static int coord_format;
static int gpsscreen = 0;

static void gps_redraw(struct ui_widget *w)
{
	char buf[16];
	float latdeg;
	float latmin;
	float latsec;
	float londeg;
	float lonmin;
	float lonsec;

	coord_format = setting_coord_style.val;

	if (gps_fixed())
		gps_get_coord(&coord, coord_format);

	/* Decompose degrees, mins, secs for formats 0 & 1 */
	latdeg = coord.lat/100;
	latmin = 100 * (latdeg - (int)latdeg);
	latsec = coord.lat - (int)coord.lat;

	londeg = coord.lon/100;
	lonmin = 100 * (londeg - (int)londeg);
	lonsec = coord.lon - (int)coord.lon;

	gfx_clear(&w->dc, 0);

	/* Display latitude and longitude depending on format */
	if (gpsscreen == 0) {
		switch (coord_format) {
		case 0:
			/* raw [deg][min].[sec/60] */
			sprintf(buf, "L: %d deg", (int)latdeg);
			gfx_text(&w->dc, &font_helv22b, 0, 0, buf, 0);
			sprintf(buf, "%2.4f'", latmin);
			gfx_text(&w->dc, &font_helv22b, 22, 20, buf, 0);

			sprintf(buf, "l: %d deg", (int)(londeg));
			gfx_text(&w->dc, &font_helv22b, 0, 50, buf, 0);
			sprintf(buf, "%2.4f'", lonmin);
			gfx_text(&w->dc, &font_helv22b, 15, 70, buf, 0);
		case 1:
			/* [deg] [min] [sec] */
			sprintf(buf, "L: %d deg", (int)latdeg);
			gfx_text(&w->dc, &font_helv22b, 0, 0, buf, 0);
			sprintf(buf, "%d'%2.2f\"", (int)latmin, 100 * latsec);
			gfx_text(&w->dc, &font_helv22b, 5, 20, buf, 0);

			sprintf(buf, "l: %d deg", (int)londeg);
			gfx_text(&w->dc, &font_helv22b, 0, 50, buf, 0);
			sprintf(buf, "%d'%2.2f\"", (int)lonmin, 100 * lonsec);
			gfx_text(&w->dc, &font_helv22b, 5, 70, buf, 0);
		case 2:
			/* [deg].[min/60] */
			sprintf(buf, "L: %2.4f", coord.lat);
			gfx_text(&w->dc, &font_helv22b, 0, 0, buf, 0);

			sprintf(buf, "l: %2.4f", coord.lon);
			gfx_text(&w->dc, &font_helv22b, 5, 50, buf, 0);
		}
		/* Display elevation */
	} else {
		sprintf(buf, "elev:");
		gfx_text(&w->dc, &font_helv22b, 0, 20, buf, 0);
		sprintf(buf, "%5.3f", coord.elev);
		gfx_text(&w->dc, &font_helv22b, 0, 40, buf, 0);
	}
}

static void gps_event(struct ui_widget *w, const struct event *evt)
{
	if (evt->type == GPS_TICK || evt->type == BUTTON_PRESSED)
		w->flags |= WF_DIRTY;
}

static struct ui_widget gps_coord_display = {
	gps_redraw,
	gps_event,
	{16, 16, 111, 111},
	0,
	WF_ACTIVE | WF_VISIBLE
};

static struct ui_widget gps_screen = {
	NULL,
	NULL,
	{0, 0, 127, 127},
	0,
	WF_ACTIVE | WF_VISIBLE
};

static void gpscoord_setup(struct application *app)
{
	/* Init UI */
	ui_clear();

	ui_init_widget(&gps_screen);
	ui_add_widget(&gps_screen);

	ui_init_widget(&gps_coord_display);
	ui_add_widget(&gps_coord_display);
	ui_add_child(&gps_screen, &gps_coord_display);

	ui_init_widget(&status_bar);
	ui_add_widget(&status_bar);

	ui_update(NULL);

	return 0;
}

static void gpscoord_loop(struct application *app)
{
	struct event evt;
	int ret;

	ret = xQueueReceive(appQueue, &evt, portMAX_DELAY);
	if(ret != pdTRUE)
		return;

	fwatch_event_input_handler(&gpscoord, &evt);
}

static int gpscoord_event_button(struct application *app, struct event *evt)
{
	if (evt->data.button == BUT_TR || evt->data.button == BUT_TL)
		fwatch_application_stop(app);
//                else if (evt.data.button == BUT_BR) {
//                    /*
//                     * Toggle between coordinate and
//                     * elevation screens
//                     */
//                    gpsscreen += 1;
//                    gpsscreen %= 2;
//                }
	return 0;
}

static int gpscoord_event_gps_tick(struct application *app, struct event *evt)
{
	ui_update(evt);
	return 0;
}

static struct app_evt_callback evt_call[] = {
	{
		.type = BUTTON_PRESSED,
		.callback = gpscoord_event_button,
	},
	{
		.type = GPS_TICK,
		.callback = gpscoord_event_gps_tick,
	},
};

application gpscoord = {
	.name = "uerdefucami",
	.setup = gpscoord_setup,
	.loop = gpscoord_loop,
	.event_handlers = evt_call,
	.event_handlers_n = ARRAY_SIZE(evt_call),
};
