/*
 * text.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 "text.h"
#include "text_renderer.h"
#include "config.h"
#include "Arduino.h"
#include <avr/pgmspace.h>
#include "display.h"


// the buffer for the message
char Text::_messages[ NUM_MESSAGE_BUFFERS ][ MESSAGE_BUFFER_LEN ];

// message buffer lengths
static int _message_lens[ NUM_MESSAGE_BUFFERS ];

// the mode that each message represents
static char _message_modes[ NUM_MESSAGE_BUFFERS ];

// mode-specific message parameters
static signed long _message_params[ NUM_MESSAGE_BUFFERS ];

// time last frame
static unsigned long _last_millis;

// duration last frame (in case we can't calculate it this frame!)
static unsigned long _duration;

// recalculate message params, based on _duration
static bool _do_draw_reset;


void reset_message_param( int message_num )
{
	if( _message_modes[ message_num ] & Text::MODE_HSCROLL )
	{
		switch( _message_modes[ message_num ] & 0x1c )
		{
		case Text::MODE_ONEQUARTER:
			_message_params[ message_num ] = -NUM_SEGMENTS / 4; break;
		case Text::MODE_ONETHIRD:
			_message_params[ message_num ] = -NUM_SEGMENTS / 3; break;
		case Text::MODE_HALF:
			_message_params[ message_num ] = -NUM_SEGMENTS / 2; break;
		case Text::MODE_TWOTHIRDS:
			_message_params[ message_num ] = -NUM_SEGMENTS * 2 / 3; break;
		case Text::MODE_THREEQUARTERS:
			_message_params[ message_num ] = -NUM_SEGMENTS * 3 / 4; break;
		case Text::MODE_ALL:
			_message_params[ message_num ] = -NUM_SEGMENTS; break;
		}
	}
	else
	{
		switch( _message_modes[ message_num ] & 0x1c )
		{
		case Text::MODE_ONEQUARTER:
			_message_params[ message_num ] = NUM_SEGMENTS / 4; break;
		case Text::MODE_ONETHIRD:
			_message_params[ message_num ] = NUM_SEGMENTS / 3; break;
		case Text::MODE_HALF:
			_message_params[ message_num ] = NUM_SEGMENTS / 2; break;
		case Text::MODE_TWOTHIRDS:
			_message_params[ message_num ] = NUM_SEGMENTS * 2 / 3; break;
		case Text::MODE_THREEQUARTERS:
			_message_params[ message_num ] = NUM_SEGMENTS * 3 / 4; break;
		case Text::MODE_ALL:
			_message_params[ message_num ] = NUM_SEGMENTS; break;
		}
		_message_params[ message_num ] = -( _message_params[ message_num ] -
			TextRenderer::get_width( _message_lens[ message_num ] ) ) / 2;
	}
}


void Text::set_message( int message_num, PString &pstring )
{
	// update message
	_message_lens[ message_num ] = pstring.length();
}


void Text::reset_message( int message_num, char mode )
{
	_message_modes[ message_num ] = mode;
	reset_message_param( message_num );
}


void Text::reset()
{
	// reset renderer
	_duration = 0;
	_last_millis = ::millis();
	_do_draw_reset = false;

	// reset the text renderer's output buffer
	TextRenderer::reset_buffer();
}


void Text::draw_reset()
{
	// how many milliseconds have elapsed since last frame?
	unsigned long millis = ::millis();
	if( millis > _last_millis )
		_duration = millis - _last_millis;
	_last_millis = millis;

	_do_draw_reset = true;
}


void Text::draw( int message_num, int segment )
{
	// perform a mid-frame reset, as necessary
	if( _do_draw_reset && segment > NUM_SEGMENTS / 2 ) {
		_do_draw_reset = false;

		if( _message_modes[ message_num ] & MODE_HSCROLL )
		{
			// add to horizontal scroll position
			_message_params[ message_num ] +=
				_duration * TEXT_SCROLL_SPEED / 1000;
			long excess = _message_params[ message_num ] -
				TextRenderer::get_width( _message_lens[ message_num ] );
			if( excess >= 0 ) {
				reset_message_param( message_num );
				_message_params[ message_num ] += excess;
			}
		}
	}

	// calculate the width
	int width;
	switch( _message_modes[ message_num ] & 0x1c )
	{
	case MODE_ONEQUARTER:
		width = NUM_SEGMENTS / 4;
		break;
	case MODE_ONETHIRD:
		width = NUM_SEGMENTS / 3;
		break;
	case MODE_HALF:
		width = NUM_SEGMENTS / 2;
		break;
	case MODE_TWOTHIRDS:
		width = NUM_SEGMENTS * 2 / 3;
		break;
	case MODE_THREEQUARTERS:
		width = NUM_SEGMENTS * 3 / 4;
		break;
	case MODE_ALL:
		width = NUM_SEGMENTS;
		break;
	}

	// convert segment to display-space
	switch( _message_modes[ message_num ] & 0x03 )
	{
	case MODE_TOP:
		if( segment < width / 2 )
			segment += width / 2;
		else if( segment >= NUM_SEGMENTS - width / 2 )
			segment -= ( NUM_SEGMENTS - width / 2 );
		else
			return;
		break;
	case MODE_BOTTOM:
		if( segment > ( NUM_SEGMENTS - width ) / 2 &&
			segment < ( NUM_SEGMENTS - width ) / 2 + width )
		{
			segment = width - ( segment - ( NUM_SEGMENTS - width ) / 2 );
		}		
		else
			return;
		break;
	}

	// render
	TextRenderer::render(
		_messages[ message_num ], _message_lens[ message_num ],
		(long)segment + _message_params[ message_num ],
		( _message_modes[ message_num ] & 1 ) == 0, 0 );
}
