/*
 * settings_major_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_major_mode.h"
#include "text.h"
#include "text_renderer.h"
#include "time.h"
#include "common.h"


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


SettingsMajorMode::SettingsMajorMode( Button &button )
	:
	_button( button )
{
}


void SettingsMajorMode::draw( int segment )
{
	// check button state for unpress
	if( _press_state == 1 && !_button.get_state() )
		_press_state = 2;
}


void SettingsMajorMode::draw_reset()
{
	// do a short button press?
	if( _press_state == 2 ) {
		_press_state = 0;
		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;
		}
	}

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

	switch( _item ) {
	case TIME_SET_IDX:
		if( _part == 0 && flash )
			str0.print( ( Time::get_hours() % 12 ) < 10? " " : "  " );
		else
			str0.format( "%d", 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() );
		str0.print( ' ' );
		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_text( 0, str0 );
}


void SettingsMajorMode::activate()
{
	_item = _part = 0;
	_press_state = 0;

	reset_messages();
}


void SettingsMajorMode::deactivate()
{
	// save time
	Time::save_time();
}


void SettingsMajorMode::press()
{
	_press_state = 1;
}


void SettingsMajorMode::long_press()
{
	_press_state = 0;

	// 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();
}


void SettingsMajorMode::reset_messages()
{
	Text::reset();
	Text::set_up_message( 0, Text::MODE_TOP | Text::MODE_ALL );
}
