/**********************************************************************
 * 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 "rgfilter.h"
#include <QStringList>
#include "util/lmp/gstutil.h"
#include "../gstfix.h"
#include "rgfiltercontroller.h"

namespace LeechCraft
{
namespace LMP
{
	RGFilter::RGFilter (Path *path)
	: Elem_ (gst_bin_new ("rgbin"))
	, TagInject_ (gst_element_factory_make ("taginject", "taginject"))
	, RGVol_ (gst_element_factory_make ("rgvolume", "rgvol"))
	, RGLimiter_ (gst_element_factory_make ("rglimiter", "rglim"))
	, Controller_ (new RGFilterController (this, path))
	{
		const auto convIn = gst_element_factory_make ("audioconvert", "convIn");
		const auto convOut = gst_element_factory_make ("audioconvert", "convOut");

		gst_bin_add_many (GST_BIN (Elem_), TagInject_, RGVol_, RGLimiter_, convIn, convOut, nullptr);
		gst_element_link_many (convIn, TagInject_, RGVol_, RGLimiter_, convOut, nullptr);

		GstUtil::AddGhostPad (convIn, Elem_, "sink");
		GstUtil::AddGhostPad (convOut, Elem_, "src");
	}

	QByteArray RGFilter::GetEffectId () const
	{
		return "org.LeechCraft.LMP.RG";
	}

	QByteArray RGFilter::GetInstanceId () const
	{
		return GetEffectId ();
	}

	IFilterConfigurator* RGFilter::GetConfigurator () const
	{
		return Controller_.get ();
	}

	void RGFilter::SetRG (const RGData& data)
	{
		QStringList pairs;
		auto addPair = [&pairs] (const QString& tagName, double value)
		{
			pairs += tagName + '=' + QString::number (value, 'f', 2);
		};

		addPair (GST_TAG_TRACK_GAIN, data.TrackGain_);
		addPair (GST_TAG_TRACK_PEAK, data.TrackPeak_);
		addPair (GST_TAG_ALBUM_GAIN, data.AlbumGain_);
		addPair (GST_TAG_ALBUM_PEAK, data.AlbumPeak_);
		addPair (GST_TAG_REFERENCE_LEVEL, 89);

		g_object_set (TagInject_, "tags", pairs.join (",").toUtf8 ().constData (), nullptr);
	}

	void RGFilter::SetAlbumMode (bool albumMode)
	{
		g_object_set (RGVol_, "album-mode", static_cast<gboolean> (albumMode), nullptr);
	}

	void RGFilter::SetLimiterEnabled (bool enabled)
	{
		g_object_set (RGLimiter_, "enabled", static_cast<gboolean> (enabled), nullptr);
	}

	void RGFilter::SetPreamp (double preamp)
	{
		g_object_set (RGVol_, "pre-amp", static_cast<gdouble> (preamp), nullptr);
	}

	GstElement* RGFilter::GetElement () const
	{
		return Elem_;
	}
}
}
