#include "currentplaying.h"

#define TEXT_COLOR 0x000000
#define TEXT_SIZE 16

CurrentPlaying::CurrentPlaying() : Label()
{
	// Create memory for null string
	lastPlaying = (char *)malloc(1);
	lastPlaying[0] = 0;
}

CurrentPlaying::~CurrentPlaying()
{
	// Free memory
	if(lastPlaying) { free(lastPlaying); }
}

void CurrentPlaying::draw()
{ 
	// Update caption
	char tCaption[MAXPATHLEN];
	sound.getNowPlaying(tCaption);
	
	if(strcmp(tCaption, lastPlaying))
	{
		// Must recreate memory
		free(lastPlaying);
		lastPlaying = (char *)malloc(strlen(tCaption) + 1);
		
		// Copy
		memcpy(lastPlaying, tCaption, strlen(tCaption) + 1);
		
		// Set caption
		setCaption(lastPlaying);
	}
	
	// Invoke base draw routine
	Label::draw(); 
}

void CurrentPlaying::reskin()
{
	// Set up text renderer
	{
		char tFont[256];
		windowManager.getString("NowPlaying.Font.Face", tFont, "DejaVu");	
		setFont(tFont);
		
		int textSize;
		windowManager.getLong("NowPlaying.Font.Size", &textSize, TEXT_SIZE);
		setSize(textSize);
		
		uint32_t textColor;
		windowManager.getLongHex("NowPlaying.Font.Color", &textColor, TEXT_COLOR);
		setColor(Color(textColor));
	}
	
	// Redraw
	setCaption(lastPlaying);
}

void CurrentPlaying::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();
}
