/*  Mpcgui for UMPlayer.
    Copyright (C) 2008 matt_ <matt@endboss.org>

    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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "mpcgui.h"
#include "mpcstyles.h"
#include "widgetactions.h"
#include "floatingwidget.h"
#include "myaction.h"
#include "mplayerwindow.h"
#include "global.h"
#include "helper.h"
#include "toolbareditor.h"
#include "desktopinfo.h"
#include "colorutils.h"
#include "images.h"
#include "youtubesearchbox.h"

#include <QToolBar>
#include <QStatusBar>
#include <QLabel>
#include <QSlider>
#include <QApplication>

using namespace Global;


MpcGui::MpcGui( QWidget * parent, Qt::WindowFlags flags )
	: BaseGuiPlus( parent, flags )
{
	createActions();
	createControlWidget();
    createStatusBar();

        /*connect( this, SIGNAL(cursorNearBottom(QPoint)),
             this, SLOT(showFloatingControl(QPoint)) );

	connect( this, SIGNAL(cursorFarEdges()),
             this, SLOT(hideFloatingControl()) );*/

        connect( mplayerwindow, SIGNAL(floatShown()),
             this, SLOT(showFullscreenControls()));
        connect( mplayerwindow->videoLayer(), SIGNAL(floatShown()),
             this, SLOT(showFullscreenControls()) );

        connect(mplayerwindow, SIGNAL(floatHidden()),
             this, SLOT(hideFullscreenControls()) );
        connect(mplayerwindow->videoLayer(), SIGNAL(floatHidden()),
             this, SLOT(hideFullscreenControls()) );
        connect(core, SIGNAL(mediaStartPlay()),
                this, SLOT(setTimeSliderDuration()) );



	retranslateStrings();

	loadConfig();

}

MpcGui::~MpcGui() {
	saveConfig();
}

void MpcGui::createActions() {
	timeslider_action = createTimeSliderAction(this);
	timeslider_action->disable();
    timeslider_action->setCustomStyle( new MpcTimeSlideStyle() );

#if USE_VOLUME_BAR
	volumeslider_action = createVolumeSliderAction(this);
	volumeslider_action->disable();
    volumeslider_action->setCustomStyle( new MpcVolumeSlideStyle() );
    volumeslider_action->setFixedSize( QSize(50,18) );
	volumeslider_action->setTickPosition( QSlider::NoTicks );
#endif

	time_label_action = new TimeLabelAction(this);
	time_label_action->setObjectName("timelabel_action");

	connect( this, SIGNAL(timeChanged(QString)),
             time_label_action, SLOT(setText(QString)) );
}

void MpcGui::setupYoutubeBox()
{
    youtubeBox->setLeftImage(QPixmap(":/mpcgui/bg-search-box-left.png"));
    youtubeBox->setCenterImage(QPixmap(":/mpcgui/bg-search-box.png"));
    youtubeBox->setRightImage(QPixmap(":/mpcgui/bg-search-box-right.png"));
    youtubeBox->setArrowImage(QPixmap(":/mpcgui/search-arrow.png"));
    youtubeBox->setYoutubeImage(QPixmap(":/mpcgui/search-youtube.png"));
    youtubeBox->setShoutcastImage(QPixmap(":/mpcgui/search-shoutcast.png"));
    youtubeBox->setSearchImage(QPixmap(":/mpcgui/search-icon.png"));
    youtubeBox->setFixedWidth(180);
}


void MpcGui::createControlWidget() {
        setupYoutubeBox();
	controlwidget = new QToolBar( this );
	controlwidget->setObjectName("controlwidget");
        controlwidget->setMovable(false);
        //controlwidget->setFloatable(true);
	controlwidget->setAllowedAreas(Qt::BottomToolBarArea);
	controlwidget->addAction(playAct);
    controlwidget->addAction(pauseAct);
	controlwidget->addAction(stopAct);
        controlwidget->addAction(recordAct);
	controlwidget->addSeparator();
    controlwidget->addAction(playPrevAct);
    controlwidget->addAction(halveSpeedAct);
    controlwidget->addAction(doubleSpeedAct);
    controlwidget->addAction(playNextAct);
    controlwidget->addSeparator();
    controlwidget->addAction(frameStepAct);
    controlwidget->addSeparator();

    QLabel* pLabel = new QLabel(this);
    pLabel->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
    controlwidget->addWidget(pLabel);

    controlwidget->addAction(fullscreenAct);
    controlwidget->addAction(showPlaylistAct);
    controlwidget->addAction(equalizerAct);
	controlwidget->addAction(muteAct);
	controlwidget->addAction(volumeslider_action);

    timeslidewidget = new QToolBar( this );
    timeslidewidget->setObjectName("timeslidewidget");
    timeslidewidget->addAction(timeslider_action);
    timeslidewidget->setMovable(false);

    mainToolbar = new QToolBar(this);
    mainToolbar->setObjectName("mainToolBar");    
    QWidget* spacerWidget[9];
    for(int i=0; i < 9; ++i)
    {
        spacerWidget[i] = new QWidget(this);
        spacerWidget[i]->setFixedSize(1, 16);
    }
    mainToolbar->addAction(openFileAct);
    mainToolbar->addWidget(spacerWidget[0]);
    mainToolbar->addAction(openDirectoryAct);
    mainToolbar->addWidget(spacerWidget[1]);
    mainToolbar->addAction(openDVDAct);
    mainToolbar->addWidget(spacerWidget[2]);
    mainToolbar->addAction(openURLAct);
    mainToolbar->addWidget(spacerWidget[3]);
    mainToolbar->addAction(screenshotAct);
    mainToolbar->addWidget(spacerWidget[4]);
    mainToolbar->addAction(showPropertiesAct);    
    mainToolbar->addWidget(spacerWidget[8]);
    mainToolbar->addAction(showFindSubtitlesDialogAct);
    mainToolbar->addWidget(spacerWidget[5]);
    mainToolbar->addAction(showPreferencesAct);
    spacerWidget[6]->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
    spacerWidget[6]->setMinimumSize(0,0);
    spacerWidget[7]->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
    spacerWidget[7]->setMinimumSize(0,0);
    spacerWidget[6]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    mainToolbar->addWidget(spacerWidget[6]);
    mainToolbar->addWidget(youtubeBox);
    spacerWidget[7]->setFixedWidth(5);
    mainToolbar->addWidget(spacerWidget[7]);




    mainToolbar->setMovable(false);

    QColor SliderColor = palette().color(QPalette::Window);
    QColor SliderBorderColor = palette().color(QPalette::Dark);
    setIconSize( QSize( 16 , 16 ) );

    addToolBar(Qt::BottomToolBarArea, controlwidget);
    addToolBarBreak(Qt::BottomToolBarArea);
    addToolBar(Qt::BottomToolBarArea, timeslidewidget);
    addToolBar(Qt::TopToolBarArea, mainToolbar);

    controlwidget->setStyle(new  MpcToolbarStyle() );
    timeslidewidget->setStyle(new  MpcToolbarStyle() );

    statusBar()->show();
}

void MpcGui::retranslateStrings() {
	BaseGuiPlus::retranslateStrings();

	controlwidget->setWindowTitle( tr("Control bar") );
        timeslidewidget->setWindowTitle(tr("Seeker"));
        mainToolbar->setWindowTitle(tr("Main Toolbar"));

    setupIcons();
}

#if AUTODISABLE_ACTIONS
void MpcGui::enableActionsOnPlaying() {
	BaseGuiPlus::enableActionsOnPlaying();

	timeslider_action->enable();
#if USE_VOLUME_BAR
	volumeslider_action->enable();
#endif
}

void MpcGui::disableActionsOnStop() {
	BaseGuiPlus::disableActionsOnStop();

	timeslider_action->disable();
#if USE_VOLUME_BAR
	volumeslider_action->disable();
#endif
}
#endif // AUTODISABLE_ACTIONS

void MpcGui::aboutToEnterFullscreen() {
	BaseGuiPlus::aboutToEnterFullscreen();
        statusBar()->hide();
        controlwidget->hide();
        timeslidewidget->hide();
        mainToolbar->hide();

}

void MpcGui::aboutToExitFullscreen() {
	BaseGuiPlus::aboutToExitFullscreen();
        statusBar()->show();
        controlwidget->show();
        timeslidewidget->show();
        mainToolbar->show();

}

void MpcGui::showFloatingControl(QPoint /*p*/) {
}

void MpcGui::hideFloatingControl() {
}

#if USE_mpcMUMSIZE
QSize MpcGui::mpcmumSizeHint() const {
	return QSize(controlwidget->sizeHint().width(), 0);
}
#endif


void MpcGui::saveConfig() {
	QSettings * set = settings;

	set->beginGroup( "mpc_gui");

	if (pref->save_window_size_on_exit) {
		qDebug("MpcGui::saveConfig: w: %d h: %d", width(), height());
		set->setValue( "pos", pos() );
		set->setValue( "size", size() );
	}

	set->setValue( "toolbars_state", saveState(Helper::qtVersion()) );

/*
#if USE_CONFIGURABLE_TOOLBARS
	set->beginGroup( "actions" );
	set->setValue("controlwidget", ToolbarEditor::save(controlwidget) );
	set->endGroup();
#endif
*/

	set->endGroup();
}

void MpcGui::loadConfig() {
	QSettings * set = settings;

	set->beginGroup( "mpc_gui");

	if (pref->save_window_size_on_exit) {
		QPoint p = set->value("pos", pos()).toPoint();
		QSize s = set->value("size", size()).toSize();

		if ( (s.height() < 200) && (!pref->use_mplayer_window) ) {
			s = pref->default_size;
		}

		move(p);
		resize(s);

		if (!DesktopInfo::isInsideScreen(this)) {
			move(0,0);
			qWarning("MpcGui::loadConfig: window is outside of the screen, moved to 0x0");
		}
	}

	restoreState( set->value( "toolbars_state" ).toByteArray(), Helper::qtVersion() );

	set->endGroup();
}

void MpcGui::setupIcons() {
    playAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(0,0,16,16) );
    playOrPauseAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(0,0,16,16) );
    pauseAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(16,0,16,16) );
    pauseAndStepAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(16,0,16,16) );
    stopAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(32,0,16,16) );
    recordAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(48,0,16,16) );

    playPrevAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(80,0,16,16) );
    //rewind2Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(80,0,16,16) );
    halveSpeedAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(96,0,16,16) );
    doubleSpeedAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(112,0,16,16) );
    //forward2Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(96,0,16,16) );
    playNextAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(128,0,16,16) );
    frameStepAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(160,0,16,16) );
    muteAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(256,0,16,16) );
    showPlaylistAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(224,0,16,16) );
    fullscreenAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(208,0,16,16) );
    equalizerAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(240,0,16,16) );
    youtubeAct->setIcon(Images::icon("youtube-16", 16));
    shoutcastAct->setIcon(Images::icon("shoutcast", 16));

    openFileAct->setIcon(Images::icon("file"));
    openDirectoryAct->setIcon(Images::icon("folder"));
    openDVDAct->setIcon(Images::icon("dvd_drive"));
    openURLAct->setIcon(Images::icon("url"));
    screenshotAct->setIcon(Images::icon("screenshot"));
    showPropertiesAct->setIcon(Images::icon("info"));
    showPreferencesAct->setIcon(Images::icon("preferences"));

    pauseAct->setCheckable(true);
    playAct->setCheckable(true);
    stopAct->setCheckable(true);
	connect( muteAct, SIGNAL(toggled(bool)),
             this, SLOT(muteIconChange(bool)) );

	connect( core , SIGNAL(mediaInfoChanged()),
             this, SLOT(updateAudioChannels()) );

    connect( core , SIGNAL(stateChanged(Core::State)),
             this, SLOT(iconChange(Core::State)) );
}

void MpcGui::iconChange(Core::State state) {
    playAct->blockSignals(true);
    pauseAct->blockSignals(true);
    stopAct->blockSignals(true);

    if( state == Core::Paused )
    {
        playAct->setChecked(false);
        pauseAct->setChecked(true);
        stopAct->setChecked(false);
    }
    if( state == Core::Playing )
    {
        playAct->setChecked(true);
        pauseAct->setChecked(false);
        stopAct->setChecked(false);
    }
    if( state == Core::Stopped )
    {
        playAct->setChecked(false);
        pauseAct->setChecked(false);
        stopAct->setChecked(false);
    }

    playAct->blockSignals(false);
    pauseAct->blockSignals(false);
    stopAct->blockSignals(false);
}

void MpcGui::muteIconChange(bool b) {
    if( sender() == muteAct )
    {
        if(!b) {
            muteAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(256,0,16,16) );
        } else {
            muteAct->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(272,0,16,16) );
        }
    }

}


void MpcGui::createStatusBar() {

    // remove frames around statusbar items
    statusBar()->setStyleSheet("QStatusBar::item { border: 0px solid black }; ");

    // emulate mono/stereo display from mpc
    audiochannel_display = new QLabel( statusBar() );
    audiochannel_display->setContentsMargins(0,0,0,0);
    audiochannel_display->setAlignment(Qt::AlignRight);
    audiochannel_display->setPixmap( QPixmap(":/mpcgui/mpc_stereo.png") );
    audiochannel_display->setMinimumSize(audiochannel_display->sizeHint());
    audiochannel_display->setMaximumSize(audiochannel_display->sizeHint());
    audiochannel_display->setPixmap( QPixmap("") );
    
	time_display = new QLabel( statusBar() );
	time_display->setAlignment(Qt::AlignRight);
	time_display->setText(" 88:88:88 / 88:88:88 ");
	time_display->setMinimumSize(time_display->sizeHint());
    time_display->setContentsMargins(15,2,1,1);

	frame_display = new QLabel( statusBar() );
	frame_display->setAlignment(Qt::AlignRight);
	frame_display->setText("88888888");
	frame_display->setMinimumSize(frame_display->sizeHint());
    frame_display->setContentsMargins(15,2,1,1);

	statusBar()->setAutoFillBackground(TRUE);   

	ColorUtils::setBackgroundColor( statusBar(), QColor(0,0,0) );
	ColorUtils::setForegroundColor( statusBar(), QColor(255,255,255) );
	ColorUtils::setBackgroundColor( time_display, QColor(0,0,0) );
	ColorUtils::setForegroundColor( time_display, QColor(255,255,255) );
	ColorUtils::setBackgroundColor( frame_display, QColor(0,0,0) );
	ColorUtils::setForegroundColor( frame_display, QColor(255,255,255) );
	ColorUtils::setBackgroundColor( audiochannel_display, QColor(0,0,0) );
	ColorUtils::setForegroundColor( audiochannel_display, QColor(255,255,255) );
	statusBar()->setSizeGripEnabled(FALSE);

    

	statusBar()->addPermanentWidget( frame_display, 0 );
	frame_display->setText( "0" );

    statusBar()->addPermanentWidget( time_display, 0 );
	time_display->setText(" 00:00:00 / 00:00:00 ");

    statusBar()->addPermanentWidget( audiochannel_display, 0 );

	time_display->show();
	frame_display->hide();

	connect( this, SIGNAL(timeChanged(QString)),
             this, SLOT(displayTime(QString)) );

	connect( this, SIGNAL(frameChanged(int)),
             this, SLOT(displayFrame(int)) );

    connect( this, SIGNAL(cursorNearBottom(QPoint)),
             this, SLOT(showFullscreenControls()) );

    connect( this, SIGNAL(cursorFarEdges()),
             this, SLOT(hideFullscreenControls()) );
}

void MpcGui::displayTime(QString text) {
	time_display->setText( text );
	time_label_action->setText(text );
}

void MpcGui::displayFrame(int frame) {
	if (frame_display->isVisible()) {
		frame_display->setNum( frame );
	}
}

void MpcGui::updateAudioChannels() {
    if( core->mdat.audio_nch == 1 ) {
        audiochannel_display->setPixmap( QPixmap(":/mpcgui/mpc_mono.png") );
    }
    else {
        audiochannel_display->setPixmap( QPixmap(":/mpcgui/mpc_stereo.png") );
    }
}

void MpcGui::showFullscreenControls() {

    if(pref->fullscreen && controlwidget->isHidden() && timeslidewidget->isHidden() )
    {
        controlwidget->show();
        timeslidewidget->show();
        statusBar()->show();
    }
}

void MpcGui::hideFullscreenControls() {

    if(pref->fullscreen && controlwidget->isVisible() && timeslidewidget->isVisible() )
    {
        controlwidget->hide();
        timeslidewidget->hide();
        statusBar()->hide();
    }
}

void MpcGui::setJumpTexts() {
	rewind1Act->change( tr("-%1").arg(Helper::timeForJumps(pref->seeking1)) );
	rewind2Act->change( tr("-%1").arg(Helper::timeForJumps(pref->seeking2)) );
	rewind3Act->change( tr("-%1").arg(Helper::timeForJumps(pref->seeking3)) );

	forward1Act->change( tr("+%1").arg(Helper::timeForJumps(pref->seeking1)) );
	forward2Act->change( tr("+%1").arg(Helper::timeForJumps(pref->seeking2)) );
	forward3Act->change( tr("+%1").arg(Helper::timeForJumps(pref->seeking3)) );

	if (qApp->isLeftToRight()) {
        rewind1Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(80,0,16,16) );
        rewind2Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(80,0,16,16) );
        rewind3Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(64,0,16,16) );

        forward1Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(96,0,16,16) );
        forward2Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(96,0,16,16) );
        forward3Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(112,0,16,16) );

	} else {
        rewind1Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(96,0,16,16) );
        rewind2Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(96,0,16,16) );
        rewind3Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(112,0,16,16) );

        forward1Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(80,0,16,16) );
        forward2Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(80,0,16,16) );
        forward3Act->setIcon( QPixmap(":/mpcgui/mpc_toolbar.png").copy(64,0,16,16) );
	}
}

void MpcGui::updateWidgets() {

    BaseGui::updateWidgets();

	// Frame counter
	frame_display->setVisible( pref->show_frame_counter );
}

void MpcGui::toggleFullscreen(bool b)
{
    BaseGui::toggleFullscreen(b);
    if(pref->fullscreen)
    {
        showFullscreenControls();                
        mplayerwindow->startCheckMouseTimer();
        mplayerwindow->videoLayer()->startCheckMouseTimer();
    }
    else
    {
        mplayerwindow->stopCheckMouseTimer();
        mplayerwindow->videoLayer()->stopCheckMouseTimer();
    }
}

void MpcGui::setTimeSliderDuration()
{
    timeslider_action->setDuration(core->mdat.duration);
}

#include "moc_mpcgui.cpp"

