#include "hud.h"
#include "cl_util.h"
#include "const.h"
#include "entity_state.h"
#include "cl_entity.h"
#include "vgui_TeamFortressViewport.h"
#include "vgui_mp3menu.h"

#define BUTTONS 8
#define BUTTON_SPACE 2

class StopMp3Handler : public ActionSignal
{
public:
	virtual void actionPerformed(Panel* panel)
	{	gHUD.m_Mp3Player.Stop();
	}
};

class PlayMp3Handler : public ActionSignal
{
public:
	virtual void actionPerformed(Panel* panel)
	{	gHUD.m_Mp3Player.Play();
	}
};

class PauseMp3Handler : public ActionSignal
{
public:
	virtual void actionPerformed(Panel* panel)
	{	gHUD.m_Mp3Player.Pause();
	}
};

class NextMp3Handler : public ActionSignal
{
public:
	virtual void actionPerformed(Panel* panel)
	{	gHUD.m_Mp3Player.Next();
	}
};

class PreviousMp3Handler : public ActionSignal
{
public:
	virtual void actionPerformed(Panel* panel)
	{	gHUD.m_Mp3Player.Previous();
	}
};

class ModeMp3Handler : public ActionSignal
{
public:
	virtual void actionPerformed(Panel* panel)
	{	gHUD.m_Mp3Player.Mode();
	}
};

void CMp3MenuPanel::SetMode(const char *mode)
{
	m_pMode->setText(mode);	
}

void CMp3MenuPanel::SetSong(const char *song)
{
	m_pSongName->setText(song);	
}

void CMp3MenuPanel::SetTime(int min,int sec)
{
	char buf[255];
	sprintf(buf,"%02d:%02d",min,sec);
	m_pTime->setText(buf);	
}

CMp3MenuPanel :: CMp3MenuPanel(int iTrans, int iRemoveMe, int x, int y, int wide, int tall) : CMenuPanel(iTrans, iRemoveMe, x,y,wide,tall)
{	
	m_pPanel = new CTransparentPanel( iTrans, x, y, wide, tall);
	m_pPanel->setParent( this );
	m_pPanel->setBorder( new LineBorder( Color(255 * 0.7,170 * 0.7,0,0) ) );
	
	//update width/height with border size
	wide-=2;
	tall-=2;
	int iButtonWidth = (wide - BUTTON_SPACE * (BUTTONS+1))/BUTTONS;
	int iButtonHeight = (tall / 3)-BUTTON_SPACE;
	
	m_pTitle = new Label( "", x, y, wide, iButtonHeight);
	m_pTitle->setParent( m_pPanel );
	m_pTitle->setBgColor( 200, 0, 0, 150 );
	m_pTitle->setFgColor( 200, 200, 200, 150 );
	m_pTitle->setContentAlignment( vgui::Label::a_center );
	m_pTitle->setText( "ESF MP3 Player" );

	int iButtony = BUTTON_SPACE + iButtonHeight;
	int iButtonx = BUTTON_SPACE;
	m_pSongName = new Label( "Songname.mp3", iButtonx, iButtony, BUTTON_SPACE*4+iButtonWidth*5, iButtonHeight);
	m_pSongName->setBgColor( 0, 0, 200, 150 );
	m_pSongName->setFgColor( 200, 200, 200, 150 );
	m_pSongName->setParent( m_pPanel );
	m_pSongName->setContentAlignment( vgui::Label::a_center );
	
	iButtonx += BUTTON_SPACE * 5 + iButtonWidth * 5;
	m_pMode = new Label( "", iButtonx, iButtony, iButtonWidth * 1.5 + BUTTON_SPACE * 0.5, iButtonHeight);
	m_pMode->setBgColor( 0, 0, 200, 150 );
	m_pMode->setFgColor( 200, 200, 200, 150 );
	m_pMode->setParent( m_pPanel );
	m_pMode->setContentAlignment( vgui::Label::a_center );
	
	iButtonx += iButtonWidth * 1.5 + BUTTON_SPACE * 1.5;
	m_pTime = new Label( "", iButtonx, iButtony, iButtonWidth * 1.5 + BUTTON_SPACE * 0.5, iButtonHeight);
	m_pTime->setBgColor( 0, 0, 200, 150 );
	m_pTime->setFgColor( 200, 200, 200, 150 );
	m_pTime->setParent( m_pPanel );
	m_pTime->setContentAlignment( vgui::Label::a_center );
	
	iButtonx = BUTTON_SPACE;
	iButtony += BUTTON_SPACE + iButtonHeight;
	m_pPlayButton = new CommandButton( ">", iButtonx, iButtony, iButtonWidth, iButtonHeight);
	m_pPlayButton->setContentAlignment(vgui::Label::a_center);//center test
	m_pPlayButton->setParent( m_pPanel );
	m_pPlayButton->addActionSignal( new PlayMp3Handler );

	iButtonx += BUTTON_SPACE + iButtonWidth;
	m_pPauseButton = new CommandButton( "||", iButtonx, iButtony, iButtonWidth, iButtonHeight);
	m_pPauseButton->setContentAlignment(vgui::Label::a_center);
	m_pPauseButton->setParent( m_pPanel );
	m_pPauseButton->addActionSignal( new PauseMp3Handler );

	iButtonx += BUTTON_SPACE + iButtonWidth;
	char text[2];
	text[0] = (signed char)0x90;
	text[1] = 0;
	m_pStopButton = new CommandButton( text, iButtonx, iButtony, iButtonWidth, iButtonHeight);
	m_pStopButton->setContentAlignment(vgui::Label::a_center);
	m_pStopButton->setParent( m_pPanel );
	m_pStopButton->addActionSignal( new StopMp3Handler );

	iButtonx += BUTTON_SPACE + iButtonWidth;
	m_pPreviousButton = new CommandButton( "<<" , iButtonx, iButtony, iButtonWidth, iButtonHeight);
	m_pPreviousButton->setContentAlignment(vgui::Label::a_center);
	m_pPreviousButton->setParent( m_pPanel );
	m_pPreviousButton->addActionSignal( new PreviousMp3Handler );	

	iButtonx += BUTTON_SPACE + iButtonWidth;
	m_pNextButton = new CommandButton( ">>", iButtonx, iButtony, iButtonWidth, iButtonHeight);
	m_pNextButton->setContentAlignment(vgui::Label::a_center);
	m_pNextButton->setParent( m_pPanel );
	m_pNextButton->addActionSignal( new NextMp3Handler );
	
	iButtonx += BUTTON_SPACE + iButtonWidth;
	m_pModeButton =  new CommandButton("Mode",iButtonx,iButtony,iButtonWidth * 1.5 + BUTTON_SPACE * 0.5 ,iButtonHeight);
	m_pModeButton->setContentAlignment(vgui::Label::a_center);
	m_pModeButton->setParent( m_pPanel );
	m_pModeButton->addActionSignal( new ModeMp3Handler );

	iButtonx += iButtonWidth * 1.5 + BUTTON_SPACE * 1.5;
	m_pCancelButton = new CommandButton( "Hide", iButtonx, iButtony, iButtonWidth * 1.5 + BUTTON_SPACE * 0.5, iButtonHeight);
	m_pCancelButton->setContentAlignment(vgui::Label::a_center);
	m_pCancelButton->setParent( m_pPanel );
	m_pCancelButton->addActionSignal( new CMenuHandler_TextWindow(HIDE_TEXTWINDOW) );
}