/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * 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 "plotitem.h"
#include <cmath>
#include <limits>
#include <vector>
#include <memory>
#include <QStyleOption>
#include <QColor>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_renderer.h>
#include <qwt_plot_grid.h>
#include <util.h>

Q_DECLARE_METATYPE (QList<QPointF>)

namespace LeechCraft
{
namespace Util
{
	PlotItem::PlotItem (QDeclarativeItem *parent)
	: QDeclarativeItem { parent }
	, Color_ { "#FF4B10" }
	{
		setFlag (QGraphicsItem::ItemHasNoContents, false);
	}

	QList<QPointF> PlotItem::GetPoints () const
	{
		return Points_;
	}

	void PlotItem::SetPoints (const QList<QPointF>& pts)
	{
		if (pts == Points_)
			return;

		Points_ = pts;
		emit pointsChanged ();
		update ();
	}

	QVariant PlotItem::GetMultipoints () const
	{
		QVariantList result;
		for (const auto& set : Multipoints_)
			result << Util::MakeMap<QString, QVariant> ({
					{ "color", QVariant::fromValue (set.Color_) },
					{ "points", QVariant::fromValue (set.Points_) }
				});
		return result;
	}

	void PlotItem::SetMultipoints (const QVariant& variant)
	{
		Multipoints_.clear ();

		for (const auto& set : variant.toList ())
		{
			const auto& map = set.toMap ();
			Multipoints_.append ({
					map ["color"].toString (),
					map ["points"].value<QList<QPointF>> ()
				});
		}
		update ();
	}

	double PlotItem::GetMinYValue () const
	{
		return MinYValue_;
	}

	void PlotItem::SetMinYValue (double val)
	{
		SetNewValue (val, MinYValue_, [this] { emit minYValueChanged (); });
	}

	double PlotItem::GetMaxYValue () const
	{
		return MaxYValue_;
	}

	void PlotItem::SetMaxYValue (double val)
	{
		SetNewValue (val, MaxYValue_, [this] { emit maxYValueChanged (); });
	}

	bool PlotItem::GetYGridEnabled () const
	{
		return YGridEnabled_;
	}

	void PlotItem::SetYGridEnabled (bool val)
	{
		SetNewValue (val, YGridEnabled_, [this] { emit yGridChanged (); });
	}

	bool PlotItem::GetYMinorGridEnabled () const
	{
		return YMinorGridEnabled_;
	}

	void PlotItem::SetYMinorGridEnabled (bool val)
	{
		SetNewValue (val, YMinorGridEnabled_, [this] { emit yMinorGridChanged (); });
	}

	double PlotItem::GetAlpha () const
	{
		return Alpha_;
	}

	void PlotItem::SetAlpha (double a)
	{
		Alpha_ = a;
		emit alphaChanged ();
	}

	QColor PlotItem::GetColor () const
	{
		return Color_;
	}

	void PlotItem::SetColor (const QColor& color)
	{
		SetNewValue (color, Color_, [this] { emit colorChanged (); });
	}

	bool PlotItem::GetLeftAxisEnabled () const
	{
		return LeftAxisEnabled_;
	}

	void PlotItem::SetLeftAxisEnabled (bool enabled)
	{
		SetNewValue (enabled, LeftAxisEnabled_, [this] { emit leftAxisEnabledChanged (); });
	}

	bool PlotItem::GetBottomAxisEnabled () const
	{
		return BottomAxisEnabled_;
	}

	void PlotItem::SetBottomAxisEnabled (bool enabled)
	{
		SetNewValue (enabled, BottomAxisEnabled_, [this] { emit bottomAxisEnabledChanged (); });
	}

	QString PlotItem::GetLeftAxisTitle () const
	{
		return LeftAxisTitle_;
	}

	void PlotItem::SetLeftAxisTitle (const QString& title)
	{
		SetNewValue (title, LeftAxisTitle_, [this] { emit leftAxisTitleChanged (); });
	}

	QString PlotItem::GetBottomAxisTitle () const
	{
		return BottomAxisTitle_;
	}

	void PlotItem::SetBottomAxisTitle (const QString& title)
	{
		SetNewValue (title, BottomAxisTitle_, [this] { emit bottomAxisTitleChanged (); });
	}

	QString PlotItem::GetPlotTitle () const
	{
		return PlotTitle_;
	}

	void PlotItem::SetPlotTitle (const QString& title)
	{
		SetNewValue (title, PlotTitle_, [this] { emit plotTitleChanged (); });
	}

	QColor PlotItem::GetBackground () const
	{
		return BackgroundColor_;
	}

	void PlotItem::SetBackground (const QColor& bg)
	{
		SetNewValue (bg, BackgroundColor_, [this] { emit backgroundChanged (); });
	}

	QColor PlotItem::GetTextColor () const
	{
		return TextColor_;
	}

	void PlotItem::SetTextColor (const QColor& color)
	{
		SetNewValue (color, TextColor_, [this] { emit textColorChanged (); });
	}

	void PlotItem::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget*)
	{
		QwtPlot plot;
		plot.setFrameShape (QFrame::NoFrame);
		plot.enableAxis (QwtPlot::yLeft, LeftAxisEnabled_);
		plot.enableAxis (QwtPlot::xBottom, BottomAxisEnabled_);
		plot.setAxisTitle (QwtPlot::yLeft, LeftAxisTitle_);
		plot.setAxisTitle (QwtPlot::xBottom, BottomAxisTitle_);
		plot.resize (option->rect.size ());

		auto setPaletteColor = [&plot] (const QColor& color, QPalette::ColorRole role) -> void
		{
			if (!color.isValid ())
				return;

			auto pal = plot.palette ();
			pal.setColor (role, { color });
			plot.setPalette (pal);
		};

		setPaletteColor (BackgroundColor_, QPalette::Window);
		setPaletteColor (TextColor_, QPalette::WindowText);
		setPaletteColor (TextColor_, QPalette::Text);

		if (!PlotTitle_.isEmpty ())
			plot.setTitle (QwtText { PlotTitle_ });

		if (MinYValue_ < MaxYValue_)
		{
			plot.setAxisAutoScale (QwtPlot::yLeft, false);
			plot.setAxisScale (QwtPlot::yLeft, MinYValue_, MaxYValue_);
		}
		plot.setAutoFillBackground (false);
		plot.setCanvasBackground (Qt::transparent);

		if (YGridEnabled_)
		{
			auto grid = new QwtPlotGrid;
			grid->enableYMin (YMinorGridEnabled_);
			grid->enableX (false);
#if QWT_VERSION >= 0x060100
			grid->setMinorPen (QPen (Qt::gray, 1, Qt::DashLine));
#else
			grid->setMinPen (QPen (Qt::gray, 1, Qt::DashLine));
#endif
			grid->attach (&plot);
		}

		auto items = Multipoints_;
		if (items.isEmpty ())
			items.push_back ({ Color_, Points_ });

		const auto ptsCount = items.first ().Points_.size ();
		if (ptsCount)
			plot.setAxisScale (QwtPlot::xBottom, 0, ptsCount - 1);

		std::vector<std::unique_ptr<QwtPlotCurve>> curves;
		for (const auto& item : items)
		{
			auto curve = new QwtPlotCurve;

			curve->setPen (QPen (item.Color_));
			auto transpColor = item.Color_;
			transpColor.setAlphaF (Alpha_);
			curve->setBrush (transpColor);

			curve->setRenderHint (QwtPlotItem::RenderAntialiased);
			curve->attach (&plot);

			curve->setSamples (item.Points_.toVector ());
		}
		plot.replot ();

		QwtPlotRenderer {}.render (&plot, painter, option->rect);
	}

	template<typename T>
	void PlotItem::SetNewValue (T val, T& ourVal, const std::function<void ()>& notifier)
	{
		if (val == ourVal)
			return;

		ourVal = val;
		notifier ();
		update ();
	}
}
}
