/*
  This file is part of Ingen.
  Copyright 2007-2015 David Robillard <http://drobilla.net/>

  Ingen is free software: you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free
  Software Foundation, either version 3 of the License, or any later version.

  Ingen 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 Affero General Public License for details.

  You should have received a copy of the GNU Affero General Public License
  along with Ingen.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <string>

#include "ingen/Log.hpp"
#include "ingen/URIs.hpp"
#include "lv2/lv2plug.in/ns/ext/presets/presets.h"

#include "Engine.hpp"
#include "LV2Block.hpp"
#include "LV2Plugin.hpp"

using namespace std;

namespace Ingen {
namespace Server {

LV2Plugin::LV2Plugin(World* world, const LilvPlugin* lplugin)
	: PluginImpl(world->uris(),
	             world->uris().lv2_Plugin.urid,
	             Raul::URI(lilv_node_as_uri(lilv_plugin_get_uri(lplugin))))
	, _world(world)
	, _lilv_plugin(lplugin)
{
	set_property(_uris.rdf_type, _uris.lv2_Plugin);

	update_properties();
}

void
LV2Plugin::update_properties()
{
	LilvNode* minor = lilv_world_get(_world->lilv_world(),
	                                 lilv_plugin_get_uri(_lilv_plugin),
	                                 _uris.lv2_minorVersion,
	                                 NULL);
	LilvNode* micro = lilv_world_get(_world->lilv_world(),
	                                 lilv_plugin_get_uri(_lilv_plugin),
	                                 _uris.lv2_microVersion,
	                                 NULL);

	if (lilv_node_is_int(minor) && lilv_node_is_int(micro)) {
		set_property(_uris.lv2_minorVersion,
		             _world->forge().make(lilv_node_as_int(minor)));
		set_property(_uris.lv2_microVersion,
		             _world->forge().make(lilv_node_as_int(micro)));
	}

	lilv_node_free(minor);
	lilv_node_free(micro);
}

const Raul::Symbol
LV2Plugin::symbol() const
{
	string working = uri();
	if (working.back() == '/')
		working = working.substr(0, working.length() - 1);

	while (working.length() > 0) {
		size_t last_slash = working.find_last_of("/");
		const string symbol = working.substr(last_slash+1);
		if ( (symbol[0] >= 'a' && symbol[0] <= 'z')
		     || (symbol[0] >= 'A' && symbol[0] <= 'Z') )
			return Raul::Symbol::symbolify(symbol);
		else
			working = working.substr(0, last_slash);
	}

	return Raul::Symbol("lv2_symbol");
}

BlockImpl*
LV2Plugin::instantiate(BufferFactory&      bufs,
                       const Raul::Symbol& symbol,
                       bool                polyphonic,
                       GraphImpl*          parent,
                       Engine&             engine,
                       const LilvState*    state)
{
	LV2Block* b = new LV2Block(
		this, symbol, polyphonic, parent, engine.sample_rate());

	if (!b->instantiate(bufs, state)) {
		delete b;
		return NULL;
	} else {
		return b;
	}
}

void
LV2Plugin::load_presets()
{
	const URIs& uris    = _world->uris();
	LilvWorld*  lworld  = _world->lilv_world();
	LilvNodes*  presets = lilv_plugin_get_related(_lilv_plugin, uris.pset_Preset);

	if (presets) {
		LILV_FOREACH(nodes, i, presets) {
			const LilvNode* preset = lilv_nodes_get(presets, i);
			lilv_world_load_resource(lworld, preset);

			LilvNodes* labels = lilv_world_find_nodes(
				lworld, preset, uris.rdfs_label, NULL);
			if (labels) {
				const LilvNode* label = lilv_nodes_get_first(labels);

				_presets.insert(
					std::make_pair(Raul::URI(lilv_node_as_uri(preset)),
					               lilv_node_as_string(label)));

				lilv_nodes_free(labels);
			} else {
				_world->log().error(
					fmt("Preset <%1%> has no rdfs:label\n")
					% lilv_node_as_string(lilv_nodes_get(presets, i)));
			}
		}

		lilv_nodes_free(presets);
	}

	PluginImpl::load_presets();
}

} // namespace Server
} // namespace Ingen