#include "playcontrol.h"
#include "common.h"
#include "filelist.h"

PlayControl::PlayControl(PlayControlType p) : Widget()
{
	// Set default state
	controlSprite = NULL;
	controlImage = NULL;
	
	// set correct control
	playControl = p;
	state = 0;
}

PlayControl::~PlayControl()
{
	// Free images
	if(controlSprite) { delete controlSprite; }
	if(controlImage) { delete controlImage; }
}

void PlayControl::resize(int width, int height)
{	
	// Ignore this
}

void PlayControl::tieToFileList(Widget *flWidget)
{
	// Remember file list
	fileList = flWidget;
}

void PlayControl::draw()
{
	// Grab location including parent
	Rect dLoc = getDimensions();
	
	// Ensure its been set up
	if(!controlSprite)
	{
		// It hasn't
		reskin();
	}
	
	// Set correct sprite state (nothing, hover, or clicked)
	int tState = state;
	if(tState > 2) { tState = 2; }
	
	// Figure out what to do based on the type
	if(playControl == CONTROL_PLAYPAUSE)
	{
		// Pause/unpause media, figure out which to display
		if(sound.getSoundState() == SOUND_PLAYING)
		{
			// Offset to next group
			tState += 3;
		}
	}
	
	// Set actual image state
	controlSprite->SetFrame(tState);
	
	// Create a window
	LayerManager manager(1);
	
	// Ensure label appears in the proper place
	controlSprite->SetPosition(dLoc.x, dLoc.y);
	
	// Append label to window
	manager.Append(controlSprite);
	
	// Draw to screen
	manager.Draw(settingsManager.getOffsetX(), settingsManager.getOffsetY());
}

void PlayControl::reskin()
{
	// Free images
	if(controlSprite) { delete controlSprite; }
	if(controlImage) { delete controlImage; }
	
	char tPath[256];
	char tGraphic[256];
	
	// Grab path to theme on sd card
	windowManager.getThemePath(tPath);
	
	// Get image path
	switch(playControl)
	{
		case CONTROL_PLAYPAUSE:
			// Pause/unpause
			windowManager.getString("PlayPause.FileName", tGraphic);
			
			break;
		case CONTROL_STOP:
			// Stop
			windowManager.getString("Stop.FileName", tGraphic);
			
			break;
		case CONTROL_NEXT:
			// Next song
			windowManager.getString("Next.FileName", tGraphic);
			
			break;
		case CONTROL_PREV:
			// Previous song
			windowManager.getString("Previous.FileName", tGraphic);
			
			break;
	}
	
	// Concatenate urls
	strcat(tPath, tGraphic);
	
	// Load the image
	controlImage = new Image();
	
	if(controlImage->LoadImage(tPath) != IMG_LOAD_ERROR_NONE)
	{
		switch(playControl)
		{
			case CONTROL_PLAYPAUSE:
				// Pause/unpause
				if(controlImage->LoadImage(DATAPATH "images/playpause.png") != IMG_LOAD_ERROR_NONE) exit(0);
				
				break;
			case CONTROL_STOP:
				// Stop
				if(controlImage->LoadImage(DATAPATH "images/stop.png") != IMG_LOAD_ERROR_NONE) exit(0);
				
				break;
			case CONTROL_NEXT:
				// Next song
				if(controlImage->LoadImage(DATAPATH "images/next.png") != IMG_LOAD_ERROR_NONE) exit(0);
				
				break;
			case CONTROL_PREV:
				// Previous song
				if(controlImage->LoadImage(DATAPATH "images/previous.png") != IMG_LOAD_ERROR_NONE) exit(0);
				
				break;
		}
	}
	
	// Grab new sprite
	controlSprite = new Sprite();
	
	if(playControl == CONTROL_PLAYPAUSE)
	{
		// Double height
		controlSprite->SetImage(controlImage, (controlImage->GetWidth()) / 3, (controlImage->GetHeight()) / 2);
		
		// Set width and height
		_rect.width = (controlImage->GetWidth()) / 3;
		_rect.height = (controlImage->GetHeight()) / 2;
	}
	else
	{
		// Single height
		controlSprite->SetImage(controlImage, (controlImage->GetWidth()) / 3, controlImage->GetHeight());
		
		// Set width and height
		_rect.width = (controlImage->GetWidth()) / 3;
		_rect.height = controlImage->GetHeight();
	}
}

void PlayControl::cursorDown(int x, int y, Action a)
{
	// Only primary click
	if(a != BUTTON_PRIMARY) { return; }
	
	// Figure out what to do based on the type
	switch(playControl)
	{
		case CONTROL_PLAYPAUSE:
			switch(sound.getSoundState())
			{
				case SOUND_UNLOADED:
					// Nothing we can do
					break;
				case SOUND_STOPPED:
					// Start playing media
					sound.playSound();
					break;
				default:
					// Pause/unpause media
					sound.pauseSound();
					break;
			}
			break;
		case CONTROL_STOP:
			// Stop sound
			sound.stopSound();
			break;
		case CONTROL_NEXT:
			// Don't do anything if no sound playing
			if(sound.getSoundState() == SOUND_UNLOADED) { break; }
			
			// Go to next song
			if(fileList)
			{
				((FileList *)fileList)->playFileRelative(1);
			}
			
			break;
		case CONTROL_PREV:
			// Don't do anything if no sound playing
			if(sound.getSoundState() == SOUND_UNLOADED) { break; }
			
			// Go to previous song
			if(fileList)
			{
				((FileList *)fileList)->playFileRelative(-1);
			}
			
			break;
	}
	
	// Add clicked state
	state += 2;
}

void PlayControl::cursorMove(int x, int y)
{
	// Already set hover?
	if(state & 1) { return; }
	
	// Set hover
	state++;
}

void PlayControl::cursorUp(int x, int y, Action a)
{
	// Only primary click
	if(a != BUTTON_PRIMARY) { return; }
	
	// Remove clicked state
	state -=2;
	
	// Ensure we didn't let go after going out of bounds
	if(state < 0) { state = 0; }
}

void PlayControl::cursorOut()
{
	// Unset all
	state = 0;
}
