/*
 * Copyright (C) 2014 Julian Lewis
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
 *
 * 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 3
 * 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
 */

/**
 * Clock application.
 */
#include <libfwatch.h>
#include "clock.h"
#include "application.h"
#include "widgets/status_bar.h"
#include <drivers/rtc.h>

#include <stdio.h>
#include <time.h>

static struct rtc_time rtc;
static struct tm cur_time;
application clock_app;

static void digital_watch_redraw(struct ui_widget *w) {
	char buf[32];

	gfx_clear(&w->dc, 0);

	strftime(buf, sizeof(buf), "%H:%M", &cur_time);
	gfx_text(&w->dc, &font_helv38b, 4, 0, buf, 1);

	// sprintf must be used, so we can display msecs too
	sprintf(buf, "%02d.%01d", cur_time.tm_sec, rtc.msecs / 100);
	gfx_text(&w->dc, &font_helv22b, 88, 14, buf, 1);

	strftime(buf, sizeof(buf), "%a %d %b %Y", &cur_time);
	gfx_centered_text(&w->dc, &font_helv17, 40, buf, 1);
}

static void digital_watch_event(struct ui_widget *w, const struct event *evt) {
	// Hour has changed, it is time to redraw the clock
	if(evt->type == RTC_TICK) {
		rtc = rtc_get_time();
		localtime_r((time_t*) &rtc.epoch, &cur_time);

		w->flags |= WF_DIRTY;
	}
}

struct ui_widget digital_watch = {
	digital_watch_redraw,
	digital_watch_event,
	{ 0, 35, 127, 35 + 58 },
	0,
	WF_ACTIVE | WF_VISIBLE
};

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

static int clock_setup(struct application *app)
{
	// Restore clock
	rtc = rtc_get_time();
	localtime_r((time_t*) &rtc.epoch, &cur_time);

	// Initialize user interface
	ui_clear();

	ui_init_widget(&clock_screen);
	ui_init_widget(&digital_watch);

	ui_add_widget(&digital_watch);
	ui_add_child(&clock_screen, &digital_watch);
	ui_add_widget(&clock_screen);

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

	// Draw the screen
	ui_update(NULL);

	return 0;
}

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

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

	// Handle clock's events
	fwatch_event_input_handler(&clock_app, &evt);
	ui_update(&evt);
}

static int clock_event_button(struct application *app, struct event *evt)
{
	if (evt->data.button == BUT_TR || evt->data.button == BUT_TL)
		fwatch_application_stop(app);
	return 0;
}

static struct app_evt_callback evt_call[] = {
	{
		.type = BUTTON_PRESSED,
		.callback = clock_event_button,
	},
};

application clock_app = {
	.name = "Clock",
	.setup = clock_setup,
	.loop = clock_loop,
	.event_handlers = evt_call,
	.event_handlers_n = ARRAY_SIZE(evt_call),
};

// Settings
struct tm clock_get_time(void) {
	rtc = rtc_get_time();
	localtime_r((time_t*) &rtc.epoch, &cur_time);

	return cur_time;
}

void clock_set_time(struct tm *time) {
	struct rtc_time r;
	r.msecs = 0;
	r.epoch = mktime(time);

	rtc_set_time(r);
}
