/*
 * 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>


// 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_pos[ NUM_MESSAGE_BUFFERS ];

// text scaling factors
static char _message_scale[ 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;


#define MASK_MODE_SIZE 0x07
#define MASK_MODE_POS 0x40

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


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

	// clear all message modes (disable messages)
	for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
		_message_modes[ a ] = 0;

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


void Text::set_up_message( int message_num, char mode, char scale )
{
	_message_modes[ message_num ] = mode;
	_message_scale[ message_num ] = scale;

	// horizontal scrolling messages need their position reset at start of mode
	if( _message_modes[ message_num ] & MODE_HSCROLL )
		reset_message_pos( message_num );
}


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

	// non-horizontal scrolling modes need their position reset whenever the
	// text changes
	if( !( _message_modes[ message_num ] & MODE_HSCROLL ) )
		reset_message_pos( message_num );
}


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;

	// we'll need to recalculate params for the frame, but we want to do it when
	// we're not in the middle of a text display (to avoid tearing).
	_do_draw_reset = true;
}


static void draw_message( int message_num, int segment )
{
	TextRenderer::buffer_in_use();

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

	// convert segment to display-space
	switch( _message_modes[ message_num ] & MASK_MODE_POS )
	{
	case Text::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 Text::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(
		Text::_messages[ message_num ], _message_lens[ message_num ],
		(long)segment + _message_pos[ message_num ],
		( _message_modes[ message_num ] & MASK_MODE_POS ) == Text::MODE_BOTTOM,
		0, _message_scale[ message_num ] );
}


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

		// go through the messages looking for an active one
		for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
			if( _message_modes[ a ] & MASK_MODE_SIZE )
			{
				// add to horizontal scroll position
				if( _message_modes[ a ] & Text::MODE_HSCROLL ) {
					_message_pos[ a ] +=
							_duration * TEXT_SCROLL_SPEED / 1000;
					long excess = _message_pos[ a ] -
							TextRenderer::get_width( _message_lens[ a ],
									_message_scale[ a ] );
					if( excess >= 0 ) {
						reset_message_pos( a );
						_message_pos[ a ] += excess;
					}
				}
			}
	}

	// draw any active messages
	for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
		if( _message_modes[ a ] & MASK_MODE_SIZE )
			draw_message( a, segment );
}

