/***************************************************************************
 *   Copyright (C) 2009 by Artur Guzik                                     *
 *   a.guzik88@gmail.com                                                   *
 *                                                                         *
 *   Copyright (C) 2009-2025 by Ilya Kotov                                 *
 *   forkotov02@ya.ru                                                      *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#ifdef QMMP_WS_X11

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QTimer>
#include <QPixmap>
#include <QFileInfo>
#include <QApplication>
#include <QScreen>
#include <QSpacerItem>
#include <QProgressBar>
#include <QSettings>
#include <qmmp/soundcore.h>
#include <qmmp/metadatamanager.h>
#include <qmmpui/metadataformatter.h>
#include "statusiconcoverwidget.h"
#include "statusiconpopupwidget.h"

StatusIconPopupWidget::StatusIconPopupWidget(QWidget * parent)
        : QFrame(parent)
{
    setWindowFlags(Qt::X11BypassWindowManagerHint |
                   Qt::WindowStaysOnTopHint | Qt::ToolTip | Qt::FramelessWindowHint);
    setFrameStyle(QFrame::NoFrame | QFrame::Plain);
    setAttribute(Qt::WA_QuitOnClose, false);

    m_hLayout = new QHBoxLayout();
    m_vLayout = new QVBoxLayout();

    m_cover = new StatusIconCoverWidget(this);
    m_hLayout->addWidget(m_cover);

    m_textLabel = new QLabel(this);
    m_vLayout->addWidget(m_textLabel);

    m_spacer = new QSpacerItem(20, 0, QSizePolicy::Expanding,QSizePolicy::Expanding);
    m_vLayout->addItem(m_spacer);

    m_bar = new TimeBar(this);
    m_vLayout->addWidget(m_bar);

    m_hLayout->addLayout(m_vLayout);
    setLayout(m_hLayout);

    m_timer = new QTimer(this);
    m_timer->setSingleShot(true);

    m_bar->setMinimumWidth(110);

    connect(m_timer, &QTimer::timeout, this, &StatusIconPopupWidget::deleteLater);
    connect(SoundCore::instance(), &SoundCore::elapsedChanged, this, &StatusIconPopupWidget::updateTime);

    QSettings settings;
    settings.beginGroup(u"Tray"_s);
    m_timer->setInterval(settings.value(u"tooltip_delay"_s, 2000).toInt());
    setWindowOpacity(1.0 - settings.value(u"tooltip_transparency"_s, 0).toInt() / 100.0);
    int size = settings.value(u"tooltip_cover_size"_s, 100).toInt();
    m_cover->setFixedSize(size, size);
    m_showProgress = settings.value(u"tooltip_progress"_s, true).toBool();
    settings.endGroup();
}

StatusIconPopupWidget::~StatusIconPopupWidget()
{
}

void StatusIconPopupWidget::mousePressEvent(QMouseEvent *)
{
    deleteLater();
}

void StatusIconPopupWidget::updateMetaData(const QString &message)
{
    m_timer->stop();
    SoundCore *core = SoundCore::instance();
    m_textLabel->setText(message);
    if(core->state() == Qmmp::Playing || core->state() == Qmmp::Paused)
    {
        QImage cover = MetaDataManager::instance()->getCover(core->trackInfo().path());
        m_cover->show();
        m_bar->show();
        if(cover.isNull())
            m_cover->setImage(QImage(u":/empty_cover.png"_s));
        else
            m_cover->setImage(cover);
        updateTime(core->elapsed());
        m_bar->setVisible(m_showProgress);
    }
    else
    {
        m_cover->hide();
        m_bar->hide();
    }
    m_textLabel->setText(message);
    qApp->processEvents();
    resize(sizeHint());
    qApp->processEvents();
    if(isVisible())
        updatePosition(m_lastTrayX, m_lastTrayY);
    m_timer->start();
}

void StatusIconPopupWidget::updateTime(qint64 elapsed)
{
    m_bar->setMaximum(SoundCore::instance()->duration() / 1000);
    m_bar->setValue(elapsed / 1000);
    m_bar->update();
}

void StatusIconPopupWidget::updatePosition(int trayx, int trayy)
{
    QRect screenGeometry = QGuiApplication::primaryScreen()->availableGeometry();
    int xpos = 0;
    int ypos = 0;

    xpos = screenGeometry.x() + trayx - 5;
    if(xpos + width() > screenGeometry.width())
        xpos = screenGeometry.width() - width() - 5;

    if(trayy < screenGeometry.y()) //tray on top of screen
    {
        ypos = screenGeometry.y() + 5;
    }

    if(trayy > screenGeometry.y()) //tray on bottom
    {
        ypos = screenGeometry.y() + screenGeometry.height() - height() -5;
    }
    move(xpos,ypos);
    return;
}

void StatusIconPopupWidget::showInfo(int x, int y, const QString &message)
{
    m_timer->stop();
    m_lastTrayX = x;
    m_lastTrayY = y;
    updateMetaData(message);
    qApp->processEvents();
    updatePosition(x,y);
    qApp->processEvents();
    show();
    m_timer->start();
}

TimeBar::TimeBar(QWidget *parent) : QProgressBar(parent)
{}

QString TimeBar::text() const
{
    return QStringLiteral("%1:%2").arg(value() / 60, 2, 10, QLatin1Char('0')).arg(value() % 60, 2, 10, QLatin1Char('0'));
}

#endif
