//**************************************************************************
//*                     This file is part of the                           *
//*                MMC - Mpxplay Multimedia Commander                      *
//*                   The source code of MMC is                            *
//*        (C) copyright 1998-2014 by PDSoft (Attila Padar)                *
//*                http://mpxplay.sourceforge.net                          *
//*                  email: mpxplay@freemail.hu                            *
//**************************************************************************
//*  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.                  *
//*  Please contact with the author (with me) if you want to use           *
//*  or modify this source.                                                *
//**************************************************************************
//function: (bottom) video control bar

//#define MPXPLAY_USE_DEBUGF 1
#define DISPQT_DEBUG_OUTPUT stdout

#include <QtGui>
#include <QtWidgets>
#include <QtWinExtras/QtWinExtras>
#include "disp_qt.h"
#include "moc_video_ctrlbar.h"
#include "moc_seekbar.h"
#include "moc_mainwindow.h"
#include "moc_video_qt.h"

#define DISPQT_VIDEOCTRLBAR_SIZE_Y 80

extern "C" {
 extern struct mainvars mvps;
 extern unsigned int refdisp, timemode;
 extern keyconfig kb[];
}

DispQtVideoControlbar::DispQtVideoControlbar(MainWindow *main_window, QWidget *parent) : QDialog(parent, Qt::CoverWindow | Qt::WindowDoesNotAcceptFocus)
{
	this->main_window = main_window;
	this->is_show_enabled = false;

	this->setFocusPolicy(Qt::NoFocus);
	this->setContentsMargins(0,0,5,20);

	this->videoctrlbar_set_size_and_pos();

	this->seek_slider = new DispQtMainwinSeekslider(main_window, this);
	this->videoctrlbar_config_style_apply(main_window->mainwin_is_translucent_enabled());

	this->number = new QLabel;
	this->number->setContentsMargins(0,0,0,0);
	QFont font = this->number->font();
	font.setPointSize(12);
	this->number->setFont(font);

	optionToolBar = new QToolBar(tr("VidOpt"), this);
	optionToolBar->setParent(this);
	optionToolBar->setContentsMargins(0,0,0,0);
	optionToolBar->addAction(main_window->action_view_videofs);
	optionToolBar->addAction(main_window->action_list_replay);
	optionToolBar->addAction(main_window->action_play_highlight);

	playToolBar = new QToolBar(tr("VidPlay"), this);
	playToolBar->setParent(this);
	playToolBar->setContentsMargins(0,0,0,0);
	playToolBar->addAction(main_window->action_play_skipback);
	playToolBar->addAction(main_window->action_play_seekback);
	playToolBar->addAction(main_window->action_play_start);
	playToolBar->addAction(main_window->action_play_stop);
	playToolBar->addAction(main_window->action_play_seekforward);
	playToolBar->addAction(main_window->action_play_skipforward);

	QToolButton *button_rewind = dynamic_cast<QToolButton*>(playToolBar->widgetForAction(main_window->action_play_seekback));
	button_rewind->setAutoRepeat(true);
	button_rewind->setAutoRepeatDelay(100);
	button_rewind->setAutoRepeatInterval(1000/30);
	QToolButton* button_forward = dynamic_cast<QToolButton*>(playToolBar->widgetForAction(main_window->action_play_seekforward));
	button_forward->setAutoRepeat(true);
	button_forward->setAutoRepeatDelay(100);
	button_forward->setAutoRepeatInterval(1000/30);

	QVBoxLayout *layout_ctrlbar = new QVBoxLayout;
	layout_ctrlbar->setMargin(0);
	layout_ctrlbar->setSpacing(0);
	layout_ctrlbar->addWidget(this->seek_slider, 1);

	QWidget *widget_play = new QWidget;
	QHBoxLayout *layout_play = new QHBoxLayout;
	layout_play->setMargin(0);
	layout_play->setSpacing(0);
	layout_play->addWidget(optionToolBar, 1, Qt::AlignLeft | Qt::AlignVCenter);
	layout_play->addWidget(playToolBar, 1, Qt::AlignCenter);
	layout_play->addWidget(this->number, 1, Qt::AlignRight | Qt::AlignVCenter);
	widget_play->setLayout(layout_play);
	layout_ctrlbar->addWidget(widget_play, 0);

	this->setLayout(layout_ctrlbar);

	connect(this, SIGNAL(signal_videoctrlbar_config_style_apply(bool)), this, SLOT(videoctrlbar_config_style_apply(bool)));
	connect(this, SIGNAL(signal_videoctrlbar_set_show(bool)), this, SLOT(videoctrlbar_set_show(bool)));
	connect(this, SIGNAL(signal_videoctrlbar_set_timepos(int)), this, SLOT(videoctrlbar_set_timepos(int)));
	connect(this, SIGNAL(signal_videoctrlbar_option_timemode_change(unsigned int)), this, SLOT(videoctrlbar_option_timemode_change(unsigned int)));
	this->hide();
}

bool DispQtVideoControlbar::event(QEvent *event)
{
	bool cursor_hide = false;
	switch(event->type()){
		case QEvent::Enter: break;
		case QEvent::Leave: cursor_hide = true; break;
		case QEvent::Move: this->videoctrlbar_set_size_and_pos(); break;
		default: goto err_out_event;
	}
	if(this->main_window->mainwin_is_on_video() && this->main_window->qt_video_player)
		this->main_window->qt_video_player->video_mouse_cursor_show(cursor_hide);

err_out_event:
	return QDialog::event(event);
}

void DispQtVideoControlbar::keyPressEvent(QKeyEvent *event)
{
	this->main_window->handle_keypressevent(event);
}

void DispQtVideoControlbar::wheelEvent(QWheelEvent *event)
{
    this->main_window->mainwin_handle_keypresscode(kb[(event->delta() < 0)? 0 : 2].c);  // KeyRewind1 / KeyForward1
	event->accept();
}

void DispQtVideoControlbar::mousePressEvent(QMouseEvent *event)
{
	Qt::MouseButton ebutton = event->button();
    if((ebutton & Qt::MidButton) || (ebutton & Qt::RightButton))
        this->main_window->mainwin_handle_mousepress_skip_or_seek_event(event);
    else
        QDialog::mousePressEvent(event);
}

void DispQtVideoControlbar::mouseReleaseEvent(QMouseEvent *event)
{
	Qt::MouseButton ebutton = event->button();
    if((ebutton & Qt::MidButton) || (ebutton & Qt::RightButton))
        this->main_window->mainwin_handle_mousepress_skip_or_seek_event(event);  // to handle press or click
    else
        QDialog::mouseReleaseEvent(event);
}

void DispQtVideoControlbar::videoctrlbar_set_size_and_pos(void)
{
	int max_x = QApplication::desktop()->screenGeometry().width();
	int max_y = QApplication::desktop()->screenGeometry().height();

	this->setFixedSize(max_x / 2, DISPQT_VIDEOCTRLBAR_SIZE_Y);
	this->move(max_x / 4, max_y - DISPQT_VIDEOCTRLBAR_SIZE_Y - ((main_window->mainwin_is_translucent_enabled())? 5 : 0));
}

void DispQtVideoControlbar::videoctrlbar_config_style_apply(bool lucent_enable)
{
	if(lucent_enable){
		this->setWindowFlags(Qt::CoverWindow | Qt::WindowDoesNotAcceptFocus);
		QtWin::extendFrameIntoClientArea(this, -1, -1, -1, -1);
		setAttribute(Qt::WA_TranslucentBackground, true);
		setAttribute(Qt::WA_NoSystemBackground, false);
		setStyleSheet("QDialog { background: transparent } QLabel { color:white }");
	} else {
		this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);
		setAttribute(Qt::WA_TranslucentBackground, false);
		setStyleSheet("QDialog {background: rgba(30,30,30,10%); border-style: solid; border-radius: 6px;} QLabel {color:white}");
		this->setWindowOpacity(0.8);
	}
	this->setWindowTitle(" ");
	//this->seek_bar->mainwinseekbar_config_style_apply(lucent_enable);
}

void DispQtVideoControlbar::videoctrlbar_set_show(bool enable)
{
	this->is_show_enabled = enable;
}

bool DispQtVideoControlbar::videoctrlbar_check_show(int x, int y)
{
	if(!this->is_show_enabled)
		return false;
	int pos_x = this->pos().x(), pos_y = this->pos().y(), size_x = this->width(), size_y = this->height();
	if((x >= pos_x) && (x < (pos_x + size_x)) && (y >= pos_y) && (y < (pos_y + size_y))) {
		this->show();
		//this->raise();   // doesn't help
		//this->setFocus();
		return true;
	} else
		this->hide();
	return false;
}

void DispQtVideoControlbar::videoctrlbar_set_currfileinfos(struct dispqt_currfileinfo_s *cur)
{
	this->time_len_ms = cur->currtimelen;
	this->seek_slider->mainwin_seekslider_set_timelen(cur->currtimelen);
	this->setWindowTitle(QString::fromUtf8(cur->curr_title));
	mpxplay_debugf(DISPQT_DEBUG_OUTPUT,"title: %s", cur->curr_title);
}

void DispQtVideoControlbar::videoctrlbar_set_timepos(int new_timepos_ms)
{
	char sout[128];
	emit this->seek_slider->mainwin_seekslider_set_timepos(new_timepos_ms);
	if(this->time_len_ms < 3600000) {
		snprintf(sout, sizeof(sout), "%d:%2.2d / %d:%2.2d", (new_timepos_ms / 60000), ((new_timepos_ms % 60000) / 1000),
					(this->time_len_ms / 60000), ((this->time_len_ms % 60000) / 1000) );
	} else {
		snprintf(sout, sizeof(sout), "%d:%2.2d:%2.2d / %d:%2.2d:%2.2d", (new_timepos_ms / 3600000), (new_timepos_ms / 60000) % 60, ((new_timepos_ms % 60000) / 1000),
					(this->time_len_ms / 3600000), (this->time_len_ms / 60000) % 60, ((this->time_len_ms % 60000) / 1000) );
	}
	this->number->setText(QString::fromLatin1(sout));
}

void DispQtVideoControlbar::videoctrlbar_option_timemode_change(unsigned int mode)
{
	//emit this->seek_bar->signal_mainwinseekbar_option_timemode_change(mode);
}
