/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2010-2012  Oleg Linkin
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "lmpsystemtrayicon.h"
#include <QWheelEvent>
#include <QHelpEvent>
#include <QToolTip>
#include "core.h"
#include "playertab.h"
#include "util.h"
#include "player.h"
#include "engine/output.h"

namespace LeechCraft
{
namespace LMP
{
	LMPSystemTrayIcon::LMPSystemTrayIcon (const QIcon& icon, QObject *parent)
	: QSystemTrayIcon (icon, parent)
	, PlayerTab_ (qobject_cast<PlayerTab*> (parent))
	{
	}

	bool LMPSystemTrayIcon::event (QEvent *event)
	{
		if (event->type () == QEvent::Wheel)
		{
			QWheelEvent *wheel = static_cast<QWheelEvent*> (event);
			emit changedVolume (wheel->delta ());

			return true;
		}
		else if (event->type () == QEvent::ToolTip)
		{
			QHelpEvent *help = static_cast<QHelpEvent*> (event);
			QString text;

			if (PlayerTab_ &&
					!CurrentSong_.Title_.isEmpty ())
			{
				const QString& trackText = tr ("%1 (%2)")
						.arg ("<b>" + CurrentSong_.Title_ + "</b>")
						.arg ("<b>" + QTime ().addSecs (CurrentSong_.Length_).toString ("mm:ss") + "</b>");
				auto ao = PlayerTab_->GetPlayer ()->GetAudioOutput ();
				int vol = 0;
				if (ao)
				{
					qreal volume = ao->GetVolume ();
					vol = volume * 100;
				}
				const QString& volumeText = tr ("Volume: %1%")
						.arg (vol);

				text = QString ("<table border='0'>"
						"<tr><td align='center' valign='top' rowspan='5'><img src='%1' width='%2' height='%3'></td></tr>"
						"<tr><td><p style='white-space:pre;'>%4</p></td></tr>"
						"<tr><td><p style='white-space:pre;'>%5</p></td></tr>"
						"<tr><td><p style='white-space:pre;'>%6</p></td></tr>"
						"<tr><td><p style='white-space:pre;'>%7</p></td></tr>"
						"</table>")
						.arg (CurrentAlbumArt_)
						.arg (130)
						.arg (130)
						.arg (trackText)
						.arg ("<b>" + CurrentSong_.Album_ + "</b>")
						.arg ("<b>" + CurrentSong_.Artist_ + "</b>")
						.arg ("<em>" + volumeText + "</em>");
			}
			else if (CurrentSong_.Title_.isEmpty ())
				text = QString ("<table border='0'><tr>"
						"<td align='center' valign='middle'><img src='%1' width='%2' height='%3'></td>"
						"<td align='center' valign='middle'><b>%4</b><br>%5</td>"
						"</tr></table>")
						.arg ("lcicons:/lmp/resources/images/lmp.svg")
						.arg (48)
						.arg (48)
						.arg ("LMP")
						.arg (tr ("No track playing"));

			QToolTip::showText (help->globalPos (), text);

			return true;
		}

		return QSystemTrayIcon::event (event);
	}

	void LMPSystemTrayIcon::handleSongChanged (const MediaInfo& song)
	{
		CurrentSong_ = song;
		CurrentAlbumArt_ = FindAlbumArtPath (song.LocalPath_);
	}

}
}
