/***************************************************************************
                          CdboPlayerPnl.cpp  -  description
                             -------------------
    begin                : Wed Jan 30 2002
    copyright            : (C) 2002 by Alex Podolsky
    email                : alexpod@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "CdboPlayerPnl.h"
#include "CdboPlayerPnl.moc"

#include <qframe.h>
#include <qlcdnumber.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qlabel.h>
#include <klocale.h>

#include <kiconloader.h>

#include "CdboAudioActionFactory.h"

/*
 *  Constructs a CdboPlayerPnl which is a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'
 */
CdboPlayerPnl::CdboPlayerPnl( QWidget* parent,  const char* name, WFlags fl )
    : QWidget( parent, name, fl )
{
    if ( !name )
	setName( "CdboPlayerPnl" );
   // resize( 408, 30 );
    setCaption(  "Form4"  );
    CdboPlayerPnlLayout = new QHBoxLayout( this );
    CdboPlayerPnlLayout->setSpacing( 0 );
    CdboPlayerPnlLayout->setMargin( 0 );

    cntPnl = new QFrame( this, "cntPnl" );
    cntPnl->setFrameShape( QFrame::StyledPanel );
    cntPnl->setFrameShadow( QFrame::Plain );
    cntPnlLayout = new QHBoxLayout( cntPnl );
    cntPnlLayout->setSpacing( 0 );
    cntPnlLayout->setMargin( 0 );

    playBtn = new QPushButton( cntPnl, "playBtn" );
    playBtn->setMaximumSize( QSize( 50, 25 ) );
     playBtn->setMinimumSize( QSize( 35, 25 ) );
    playBtn->setText( "..." );
     playBtn->setPixmap( BarIcon("1rightarrow", KIcon::SizeSmall ) );
    QToolTip::add(  playBtn, i18n( "Play/Pause" ) );
    cntPnlLayout->addWidget( playBtn );

    stopBtn = new QPushButton( cntPnl, "stopBtn" );
    stopBtn->setMaximumSize( QSize( 50, 25 ) );
     stopBtn->setMinimumSize( QSize( 35, 25 ) );
    stopBtn->setText( "..." );
    stopBtn->setPixmap( BarIcon("player_stop", KIcon::SizeSmall ) );
    QToolTip::add(  stopBtn, i18n( "Stop" ) );
    cntPnlLayout->addWidget( stopBtn );

    backBtn = new QPushButton( cntPnl, "backBtn" );
    backBtn->setMaximumSize( QSize( 50, 25 ) );
     backBtn->setMinimumSize( QSize( 35, 25 ) );
    backBtn->setText( "..." );
    backBtn->setPixmap( BarIcon("2leftarrow", KIcon::SizeSmall ) );
    QToolTip::add(  backBtn, i18n( "30 Secs Backward" ) );
    cntPnlLayout->addWidget( backBtn );
	
	forwardBtn = new QPushButton( cntPnl, "forwardBtn" );
    forwardBtn->setMaximumSize( QSize( 50, 25 ) );
    forwardBtn->setMinimumSize( QSize( 35, 25 ) );
    forwardBtn->setText( "..." );
    forwardBtn->setPixmap( BarIcon("2rightarrow", KIcon::SizeSmall ) );
    QToolTip::add(  forwardBtn, i18n( "30 Secs Forward" ) );
    cntPnlLayout->addWidget( forwardBtn );


    prevBtn = new QPushButton( cntPnl, "prevBtn" );
    prevBtn->setMaximumSize( QSize( 50, 25 ) );
     prevBtn->setMinimumSize( QSize( 35, 25 ) );
    prevBtn->setText( "..." );
    prevBtn->setPixmap( BarIcon("player_start", KIcon::SizeSmall ) );
    QToolTip::add(  prevBtn, i18n( "Previous" ) );
    cntPnlLayout->addWidget( prevBtn );

    nextBtn = new QPushButton( cntPnl, "nextBtn" );
    nextBtn->setMaximumSize( QSize( 50, 25 ) );
     nextBtn->setMinimumSize( QSize( 35, 25 ) );
    nextBtn->setText( "..." );
    nextBtn->setPixmap( BarIcon("player_end", KIcon::SizeSmall ) );

    QToolTip::add(  nextBtn, i18n( "Next" ) );
    cntPnlLayout->addWidget( nextBtn );
    QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
    cntPnlLayout->addItem( spacer );

	curLbl = new QLabel( cntPnl, "curLbl" );
	//QFont curLbl_font(  curLbl->font() );
   // curLbl_font.setPointSize( 10 );
   // curLbl->setFont( curLbl_font );
   // timeLbl->setText( "00:00" );
	curLbl->setMaximumSize( QSize( 32767, 25 ) );
	curLbl->setFrameShape( QLabel::Box );
    curLbl->setFrameShadow( QLabel::Plain );
   curLbl->setPalette( QPalette( Qt::white, Qt::black) );


	cntPnlLayout->addWidget( curLbl );

	timeLbl = new QLabel( cntPnl, "timeLbl" );
	QFont timeLbl_font(  timeLbl->font() );
    timeLbl_font.setPointSize( 18 );
    timeLbl_font.setFamily( "Lurabiefont" );
    timeLbl->setFont( timeLbl_font );
	timeLbl->setAlignment(AlignHCenter);
    timeLbl->setText( " 00:00 "  );
	timeLbl->setMaximumSize( QSize( 32767, 25 ) );
	timeLbl->setFrameShape( QLabel::Box );
    timeLbl->setFrameShadow( QLabel::Plain );
    timeLbl->setPalette( QPalette( Qt::white, Qt::black ) );


	cntPnlLayout->addWidget( timeLbl );
	QSpacerItem* spacer1 = new QSpacerItem( 1, 1, QSizePolicy::Minimum, QSizePolicy::Minimum );
    cntPnlLayout->addItem( spacer1 );
	
	

    CdboPlayerPnlLayout->addWidget( cntPnl );
//////////////////////////////////////////////// not gui /////////////////////////////////////////////////////
	device = "";
	plaing = false;
	factory = 0;
	continuesPlay = true;//////////////////////////////////////
	curLbl->hide();/////////////////////////////////////
		
	
	connect( playBtn, SIGNAL( clicked() ), this, SLOT( play() ) );
	connect( stopBtn, SIGNAL( clicked() ), this, SLOT( stop() ) );
	connect( forwardBtn, SIGNAL( clicked() ), this, SLOT( forward() ) );
	connect( backBtn, SIGNAL( clicked() ), this, SLOT( back() ) );
	connect( prevBtn, SIGNAL( clicked() ), this, SLOT( prev() ) );
	connect( nextBtn, SIGNAL( clicked() ), this, SLOT( next() ) );	

}

//============================================================
CdboPlayerPnl::~CdboPlayerPnl(){
	stop();
}
//===================================================
void CdboPlayerPnl::setDevice(QString d, int t){
	device = d;
	totalTracks = t;
	curTrack = 1;
	plaing = false;
}
//---------------------------------------------------------------------------------------------
void CdboPlayerPnl::timerEvent(QTimerEvent *event){
	if(event->timerId() == timeOutId){
		//stop();
	}
}
//---------------------------------------------------------------------------------------------
void CdboPlayerPnl::play(int track){
	stop();
	curTrack = track;
	play();
}
//---------------------------------------------------------------------------------------------
void CdboPlayerPnl::play(){
	if(!factory || device == "")
		return;
	if(plaing)
		stop();
	QString tracks = QString::number(curTrack);
	curLbl->show();
	curLbl->setText( i18n( "playing track %1").arg(tracks) );
	
	if(continuesPlay)
		tracks += "+" + QString::number(totalTracks);
	
	factory->play(device, tracks, 0);
	plaing = true;
	
}
//---------------------------------------------------------------------------------------------
void CdboPlayerPnl::stop(){
	if(!plaing)
		return;
	
	factory->killCurrentTask();
	killTimer(timeOutId);
	plaing = false;
	curLbl->hide();
}
//---------------------------------------------------------------------------------------------
void CdboPlayerPnl::forward(){
	//plugin->seek(int second);
}
//---------------------------------------------------------------------------------------------
void CdboPlayerPnl::back(){
	//plugin->seek(int second);
}
//---------------------------------------------------------------------------------------------
void CdboPlayerPnl::prev(){
	stop();
	curTrack--;
	if(curTrack == 0)
		curTrack = totalTracks;
	plaing = false;
	play();
}
//---------------------------------------------------------------------------------------------
void CdboPlayerPnl::next(){
	stop();
	curTrack++;
	if(curTrack > totalTracks)
		curTrack = 1;
	plaing = false;
	play();
}
//---------------------------------------------------------------------------------------------
void CdboPlayerPnl::slotSongDone(){
	curTrack++;
	curLbl->setText( i18n( "track %1").arg(QString::number(curTrack)) );
}
//---------------------------------------------------------------------------------------------
void CdboPlayerPnl::slotPlayDone(){
	curTrack++;
	if(curTrack > totalTracks)
		curTrack = 1;
	plaing = false;
	if(continuesPlay)
		play();
	
	if(!plaing)
		curLbl->hide();
}
//---------------------------------------------------------------------------------------------
void CdboPlayerPnl::setFactory(CdboAudioActionFactory *f){
	factory = f;
	connect( factory, SIGNAL( songDone() ), this, SLOT( slotSongDone() ) );
	connect( factory, SIGNAL( playDone() ), this, SLOT( slotPlayDone() ) );
}

