/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2012  Georg Rudoy
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#include "visualnotificationsview.h"
#include <QFile>
#include <QDeclarativeContext>
#include <QDeclarativeError>
#include <QDeclarativeEngine>
#include <QtDebug>
#include <util/util.h>
#include <util/sys/paths.h>
#include <util/qml/colorthemeproxy.h>
#include <util/qml/themeimageprovider.h>
#include <interfaces/core/icoreproxy.h>
#include "eventproxyobject.h"
#include "../core.h"

namespace LeechCraft
{
namespace AdvancedNotifications
{
	VisualNotificationsView::VisualNotificationsView (QWidget *parent)
	: QDeclarativeView (0)
	{
		setStyleSheet ("background: transparent");
		setWindowFlags (Qt::WindowStaysOnTopHint | Qt::ToolTip);
		setAttribute (Qt::WA_TranslucentBackground);

		connect (this,
				SIGNAL (statusChanged (QDeclarativeView::Status)),
				this,
				SLOT (handleStatusChanged (QDeclarativeView::Status)));

		const auto& fileLocation = Util::GetSysPath (Util::SysPath::QML, "advancednotifications", "visualnotificationsview.qml");

		if (fileLocation.isEmpty ())
		{
			qWarning () << Q_FUNC_INFO
					<< "visualnotificationsview.qml isn't found";
			return;
		}

		qDebug () << Q_FUNC_INFO << "created";

		Location_ = QUrl::fromLocalFile (fileLocation);

		auto proxy = Core::Instance ().GetProxy ();
		rootContext ()->setContextProperty ("colorProxy",
				new Util::ColorThemeProxy (proxy->GetColorThemeManager (), this));
		engine ()->addImageProvider ("ThemeIcons", new Util::ThemeImageProvider (proxy));

		for (const auto& cand : Util::GetPathCandidates (Util::SysPath::QML, ""))
			engine ()->addImportPath (cand);
	}

	void VisualNotificationsView::SetEvents (const QList<EventData>& events)
	{
		QObjectList oldEvents = LastEvents_;

		LastEvents_.clear ();
		Q_FOREACH (const EventData& ed, events)
		{
			EventProxyObject *obj = new EventProxyObject (ed, this);
			connect (obj,
					SIGNAL (actionTriggered (const QString&, int)),
					this,
					SIGNAL (actionTriggered (const QString&, int)));
			connect (obj,
					SIGNAL (dismissEventRequested (const QString&)),
					this,
					SIGNAL (dismissEvent (const QString&)));
			LastEvents_ << obj;
		}

		rootContext ()->setContextProperty ("eventsModel",
				QVariant::fromValue<QList<QObject*>> (LastEvents_));

		setSource (Location_);

		qDeleteAll (oldEvents);
	}

	void VisualNotificationsView::handleStatusChanged (QDeclarativeView::Status status)
	{
		qDebug () << Q_FUNC_INFO
				<< status;

		if (status == Error)
		{
			qWarning () << Q_FUNC_INFO
					<< "got errors:"
					<< errors ().size ();
			Q_FOREACH (const QDeclarativeError& error, errors ())
				qWarning () << error.toString ()
						<< "["
						<< error.description ()
						<< "]";
		}
	}
}
}
