#include "currentlength.h"

#define TEXT_COLOR 0x000000
#define TEXT_SIZE 16

CurrentLength::CurrentLength(int w) : Label()
{
	// Set up length
	lastLength = 0xFFFFFFFF;
	
	// Set which type
	which = w;
}

void CurrentLength::draw()
{ 
	// Update caption
	int tLen = 0;

	switch(which)
	{
		case CR_LENGTH:
			tLen = sound.getSoundLength();
			break;
		case CR_TIME:
			tLen = sound.getSoundPosition();
			break;
	}
	
	if(tLen != lastLength)
	{
		// Temporary memory
		char tStr[128];
		
		if(tLen == -1)
		{
			// Fake time
			sprintf(tStr, "-:--");
		}
		else
		{
			// Make time
			sprintf(tStr, "%d:%02d", tLen / 60, tLen % 60);
		}
		
		// Set caption
		setCaption(tStr);
		
		// Remember last length
		lastLength = tLen;
	}
	
	// Invoke base draw routine
	Label::draw(); 
}

void CurrentLength::reskin()
{
	// Set up text renderer
	if(which == CR_LENGTH)
	{
		char tFont[256];
		windowManager.getString("CurrentLength.Font.Face", tFont, "DejaVu");	
		setFont(tFont);
		
		int textSize;
		windowManager.getLong("CurrentLength.Font.Size", &textSize, TEXT_SIZE);
		setSize(textSize);
		
		uint32_t textColor;
		windowManager.getLongHex("CurrentLength.Font.Color", &textColor, TEXT_COLOR);
		setColor(Color(textColor));
	}
	else
	{
		char tFont[256];
		windowManager.getString("CurrentPlayTime.Font.Face", tFont, "DejaVu");	
		setFont(tFont);
		
		int textSize;
		windowManager.getLong("CurrentPlayTime.Font.Size", &textSize, TEXT_SIZE);
		setSize(textSize);
		
		uint32_t textColor;
		windowManager.getLongHex("CurrentPlayTime.Font.Color", &textColor, TEXT_COLOR);
		setColor(Color(textColor));
	}
	
	// Redraw
	lastLength = 0xFFFFFFFF;
}

void CurrentLength::resize(int width, int height)
{
	// Pad for text display
	width += PADDING;
	
	// Ensure width/height is a multiple of 4
	while(width % 4) { width++; };
	while(height % 4) { height++; };
	
	// Resize widget
	_rect.width = width;
	_rect.height = height;
	
	// Remember to redraw
	reskin();
}
