/**********************************************************************
 * 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.
 **********************************************************************/

#pragma once

#include <functional>
#include <QDeclarativeItem>
#include "qmlconfig.h"

namespace LeechCraft
{
namespace Util
{
	class UTIL_QML_API PlotItem : public QDeclarativeItem
	{
		Q_OBJECT

		Q_PROPERTY (QList<QPointF> points READ GetPoints WRITE SetPoints NOTIFY pointsChanged)

		Q_PROPERTY (QVariant multipoints READ GetMultipoints WRITE SetMultipoints NOTIFY multipointsChanged)

		Q_PROPERTY (double minYValue READ GetMinYValue WRITE SetMinYValue NOTIFY minYValueChanged)
		Q_PROPERTY (double maxYValue READ GetMaxYValue WRITE SetMaxYValue NOTIFY maxYValueChanged)

		Q_PROPERTY (bool yGridEnabled READ GetYGridEnabled WRITE SetYGridEnabled NOTIFY yGridChanged)
		Q_PROPERTY (bool yMinorGridEnabled READ GetYMinorGridEnabled WRITE SetYMinorGridEnabled NOTIFY yMinorGridChanged)

		Q_PROPERTY (double alpha READ GetAlpha WRITE SetAlpha NOTIFY alphaChanged)
		Q_PROPERTY (QColor color READ GetColor WRITE SetColor NOTIFY colorChanged)
		Q_PROPERTY (bool leftAxisEnabled READ GetLeftAxisEnabled WRITE SetLeftAxisEnabled NOTIFY leftAxisEnabledChanged)
		Q_PROPERTY (bool bottomAxisEnabled READ GetBottomAxisEnabled WRITE SetBottomAxisEnabled NOTIFY bottomAxisEnabledChanged)
		Q_PROPERTY (QString leftAxisTitle READ GetLeftAxisTitle WRITE SetLeftAxisTitle NOTIFY leftAxisTitleChanged)
		Q_PROPERTY (QString bottomAxisTitle READ GetBottomAxisTitle WRITE SetBottomAxisTitle NOTIFY bottomAxisTitleChanged)

		Q_PROPERTY (QString plotTitle READ GetPlotTitle WRITE SetPlotTitle NOTIFY plotTitleChanged)

		Q_PROPERTY (QColor background READ GetBackground WRITE SetBackground NOTIFY backgroundChanged)
		Q_PROPERTY (QColor textColor READ GetTextColor WRITE SetTextColor NOTIFY textColorChanged)

		QList<QPointF> Points_;

		struct PointsSet
		{
			QColor Color_;
			QList<QPointF> Points_;
		};
		QList<PointsSet> Multipoints_;

		double MinYValue_ = -1;
		double MaxYValue_ = -1;

		bool YGridEnabled_ = false;
		bool YMinorGridEnabled_ = false;

		double Alpha_ = 0.3;

		QColor Color_;

		bool LeftAxisEnabled_ = false;
		bool BottomAxisEnabled_ = false;

		QString LeftAxisTitle_;
		QString BottomAxisTitle_;

		QString PlotTitle_;

		QColor BackgroundColor_;
		QColor TextColor_;
	public:
		PlotItem (QDeclarativeItem* = 0);

		QList<QPointF> GetPoints () const;
		void SetPoints (const QList<QPointF>&);

		QVariant GetMultipoints () const;
		void SetMultipoints (const QVariant&);

		double GetMinYValue () const;
		void SetMinYValue (double);
		double GetMaxYValue () const;
		void SetMaxYValue (double);

		bool GetYGridEnabled () const;
		void SetYGridEnabled (bool);
		bool GetYMinorGridEnabled () const;
		void SetYMinorGridEnabled (bool);

		double GetAlpha () const;
		void SetAlpha (double);

		QColor GetColor () const;
		void SetColor (const QColor&);

		bool GetLeftAxisEnabled () const;
		void SetLeftAxisEnabled (bool);
		bool GetBottomAxisEnabled () const;
		void SetBottomAxisEnabled (bool);

		QString GetLeftAxisTitle () const;
		void SetLeftAxisTitle (const QString&);
		QString GetBottomAxisTitle () const;
		void SetBottomAxisTitle (const QString&);

		QString GetPlotTitle () const;
		void SetPlotTitle (const QString&);

		QColor GetBackground () const;
		void SetBackground (const QColor&);
		QColor GetTextColor () const;
		void SetTextColor (const QColor&);

		void paint (QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override;
	private:
		template<typename T>
		void SetNewValue (T val, T& ourVal, const std::function<void ()>& notifier);
	signals:
		void pointsChanged ();
		void multipointsChanged ();

		void minYValueChanged ();
		void maxYValueChanged ();

		void yGridChanged ();
		void yMinorGridChanged ();

		void alphaChanged ();

		void colorChanged ();

		void leftAxisEnabledChanged ();
		void bottomAxisEnabledChanged ();

		void leftAxisTitleChanged ();
		void bottomAxisTitleChanged ();

		void plotTitleChanged ();

		void backgroundChanged ();
		void textColorChanged ();
	};
}
}
