/**********************************************************************
 * 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 "eqconfiguratordialog.h"
#include <QtDebug>

#ifdef WITH_QWT
#include <qwt_plot.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_scale_engine.h>
#include <qwt_curve_fitter.h>
#endif

#include <util/sll/prelude.h>
#include "eqbandwidget.h"

namespace LC
{
namespace LMP
{
namespace Fradj
{
	EqConfiguratorDialog::EqConfiguratorDialog (const BandInfos_t& bands,
			const QList<double>& gains, const QStringList& presets, QWidget *parent)
	: QDialog { parent }
#ifdef WITH_QWT
	, Plot_ { new QwtPlot }
	, FreqCurve_ { new QwtPlotCurve }
#endif
	{
		Ui_.setupUi (this);
		Ui_.Preset_->addItems (presets);
		Ui_.Preset_->setCurrentIndex (-1);

		for (const auto& bandInfo : bands)
		{
			auto band = new EqBandWidget { bandInfo };
			Ui_.BandsLayout_->addWidget (band);
			Bands_ << band;

			connect (band,
					SIGNAL (valueChanged (double)),
					this,
					SLOT (rebuildPlot ()));
		}

		SetupPlot ();

		SetGains (gains);
	}

	QList<double> EqConfiguratorDialog::GetGains () const
	{
		return Util::Map (Bands_, &EqBandWidget::GetGain);
	}

	void EqConfiguratorDialog::SetGains (const QList<double>& gains)
	{
		if (gains.size () != Bands_.size ())
		{
			qWarning () << Q_FUNC_INFO
					<< "gains count"
					<< gains.size ()
					<< "doesn't equal to bands count"
					<< Bands_.size ();
			return;
		}

		for (int i = 0; i < gains.size (); ++i)
			Bands_.at (i)->SetGain (gains.at (i));

		rebuildPlot ();
	}

	void EqConfiguratorDialog::SetupPlot ()
	{
#ifdef WITH_QWT
		Ui_.DialogLayout_->insertWidget (Ui_.DialogLayout_->count () - 1, Plot_);

		Plot_->setAxisTitle (QwtPlot::xBottom, tr ("Frequency, Hz"));
		Plot_->setAxisScaleEngine (QwtPlot::xBottom, new QwtLogScaleEngine { 2 });

		Plot_->setAxisAutoScale (QwtPlot::yLeft, false);
		Plot_->setAxisScale (QwtPlot::yLeft, -24, 12);
		Plot_->setAxisTitle (QwtPlot::yLeft, tr ("Gain, dB"));

		FreqCurve_->setRenderHint (QwtPlotItem::RenderAntialiased);

		FreqCurve_->setCurveAttribute (QwtPlotCurve::Fitted);
		auto fitter = new QwtSplineCurveFitter;
		FreqCurve_->setCurveFitter (fitter);

		FreqCurve_->attach (Plot_);

		auto grid = new QwtPlotGrid;
		grid->enableXMin (true);
		grid->setMajorPen (QPen (Qt::gray, 1, Qt::DashLine));
		grid->setMinorPen (QPen (Qt::gray, 1, Qt::DashLine));
		grid->attach (Plot_);
#endif
	}

	void EqConfiguratorDialog::rebuildPlot ()
	{
#ifdef WITH_QWT
		QVector<double> xData, yData;
		for (const auto& band : Bands_)
		{
			xData << band->GetFrequency ();
			yData << band->GetGain ();
		}
		Plot_->setAxisScale (QwtPlot::xBottom, xData.first (), xData.last ());
		FreqCurve_->setSamples (xData, yData);

		Plot_->replot ();
#endif
	}

	void EqConfiguratorDialog::on_Preset__currentIndexChanged (const QString& preset)
	{
		emit presetRequested (preset);
	}
}
}
}
