/*
 * settings_mode.cc
 *
 * Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
 *
 * This file is part of propeller-clock (hereafter referred to as "this
 * program"). See http://ed.am/dev/software/arduino/propeller-clock for more
 * information.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "Arduino.h"
#include "settings_mode.h"
#include "text.h"
#include "text_renderer.h"
#include "time.h"
#include "common.h"


#define TIME_SET_IDX 0
#define DATE_SET_IDX 1
#define FONT_SET_IDX 2


// item we're setting
static int _item;
static int _part;


void settings_mode_press()
{
	switch( _item )
	{
/*	case TIME_SET_IDX:
		switch( _part ) {
		case 0: Time::inc_hours(); break;
		case 1: Time::inc_minutes(); break;
		case 2: Time::reset_seconds(); break;
		}
		break;
	case DATE_SET_IDX:
		switch( _part ) {
		case 0: Time::inc_year(); break;
		case 1: Time::inc_month(); break;
		case 2: Time::inc_day(); break;
		}
		break;*/
	case FONT_SET_IDX:
		TextRenderer::inc_font();
		break;
	}
}


static void reset_messages()
{
	Text::reset_message( 0, Text::MODE_TOP | Text::MODE_ALL );
}


void settings_mode_long_press()
{
	// how many parts does this item have?
	int max = 1;
	switch( _item ) {
	case DATE_SET_IDX: // fall through
	case TIME_SET_IDX: max = 3; break;
	}

	// inc part and item
	if( ++_part >= max ) {
		_part = 0;
		if( ++_item >= 3 )
			_item = 0;
	}

	// reset messages
	reset_messages();
}


void settings_mode_draw( int segment )
{
	Text::draw( 0, segment );
}


void settings_mode_draw_reset()
{
	bool flash = ( ::millis() % 1000 ) > 600;
	PString str0( Text::_messages[ 0 ], MESSAGE_LEN * 2 );
	str0.begin();

	switch( _item ) {
	case TIME_SET_IDX:
		if( _part == 0 && flash )
			str0.print( "  " );
		else
			str0.format( "%2d", Time::get_hours() % 12 );
		str0.print( ':' );
		if( _part == 1 && flash )
			str0.print( "  " );
		else
			str0.format( "%02d", Time::get_minutes() );
		str0.print( ':' );
		if( _part == 2 && flash )
			str0.print( "  " );
		else
			str0.format( "%02d", Time::get_seconds() );
		if( _part == 0 && flash )
			str0.print( "  " );
		else
			str0.print( Time::get_hours() > 11? "pm" : "am" );
		break;

	case DATE_SET_IDX:
		if( _part == 0 && flash )
			str0.print( "    " );
		else
			str0.format( "%04d", Time::get_year() );
		str0.print( '-' );
		if( _part == 1 && flash )
			str0.print( "  " );
		else
			str0.format( "%02d", Time::get_month() );
		str0.print( '-' );
		if( _part == 2 && flash )
			str0.print( "  " );
		else
			str0.format( "%02d", Time::get_day() );
		break;

	case FONT_SET_IDX:
		str0.print( "font " );
		if( flash )
			str0.print( ' ' );
		else
			str0.format( "%d", TextRenderer::get_font() );
		break;
	}

	Text::set_message( 0, str0 );
}


void settings_mode_activate()
{
	_item = _part = 0;

	// reset messages
	reset_messages();
}
