/*
  This file is part of Ingen.
  Copyright 2007-2016 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 <cstdlib>
#include <cassert>

#include "ingen/Log.hpp"
#include "ingen/URIs.hpp"

#include "ArcImpl.hpp"
#include "BlockImpl.hpp"
#include "Buffer.hpp"
#include "BufferFactory.hpp"
#include "Engine.hpp"
#include "GraphImpl.hpp"
#include "InputPort.hpp"
#include "RunContext.hpp"
#include "mix.hpp"

using namespace std;

namespace Ingen {
namespace Server {

InputPort::InputPort(BufferFactory&      bufs,
                     BlockImpl*          parent,
                     const Raul::Symbol& symbol,
                     uint32_t            index,
                     uint32_t            poly,
                     PortType            type,
                     LV2_URID            buffer_type,
                     const Atom&         value,
                     size_t              buffer_size)
	: PortImpl(bufs, parent, symbol, index, poly, type, buffer_type, value, buffer_size, false)
	, _num_arcs(0)
{
	const Ingen::URIs& uris = bufs.uris();

	if (parent->graph_type() != Node::GraphType::GRAPH) {
		add_property(uris.rdf_type, uris.lv2_InputPort.urid);
	}
}

bool
InputPort::apply_poly(RunContext& context, uint32_t poly)
{
	bool ret = PortImpl::apply_poly(context, poly);
	if (!ret)
		poly = 1;

	assert(_voices->size() >= poly);

	return true;
}

bool
InputPort::get_buffers(BufferFactory&      bufs,
                       PortImpl::GetFn     get,
                       const MPtr<Voices>& voices,
                       uint32_t            poly,
                       size_t              num_in_arcs) const
{
	if (is_a(PortType::ATOM) && !_value.is_valid()) {
		poly = 1;
	}

	if (is_a(PortType::AUDIO) && num_in_arcs == 0) {
		// Audio input with no arcs, use shared zero buffer
		for (uint32_t v = 0; v < poly; ++v) {
			voices->at(v).buffer = bufs.silent_buffer();
		}
		return false;
	}

	// Otherwise, allocate local buffers
	for (uint32_t v = 0; v < poly; ++v) {
		voices->at(v).buffer.reset();
		voices->at(v).buffer = (bufs.*get)(
			buffer_type(), _value.type(), _buffer_size);
		voices->at(v).buffer->clear();
		if (_value.is_valid()) {
			voices->at(v).buffer->set_value(_value);
		}
	}
	return true;
}

bool
InputPort::pre_get_buffers(BufferFactory& bufs,
                           MPtr<Voices>&  voices,
                           uint32_t       poly) const
{
	return get_buffers(bufs, &BufferFactory::get_buffer, voices, poly, _num_arcs);
}

bool
InputPort::setup_buffers(RunContext& ctx, BufferFactory& bufs, uint32_t poly)
{
	if (is_a(PortType::ATOM) && !_value.is_valid()) {
		poly = 1;
	}

	if (_arcs.size() == 1 && !is_a(PortType::ATOM) && !_arcs.front().must_mix()) {
		// Single non-mixing connection, use buffers directly
		for (uint32_t v = 0; v < poly; ++v) {
			_voices->at(v).buffer = _arcs.front().buffer(v);
		}
		return false;
	}

	return get_buffers(bufs, &BufferFactory::claim_buffer, _voices, poly, _arcs.size());
}

void
InputPort::add_arc(RunContext& context, ArcImpl& c)
{
	_arcs.push_front(c);
}

void
InputPort::remove_arc(ArcImpl& arc)
{
	_arcs.erase(_arcs.iterator_to(arc));
}

uint32_t
InputPort::max_tail_poly(RunContext& context) const
{
	return parent_block()->parent_graph()->internal_poly_process();
}

void
InputPort::pre_process(RunContext& context)
{
	if (_arcs.empty()) {
		// No incoming arcs, just handle user-set value
		for (uint32_t v = 0; v < _poly; ++v) {
			// Update set state
			update_set_state(context, v);

			// Prepare for write in case a set event executes this cycle
			if (!_parent->is_main()) {
				buffer(v)->prepare_write(context);
			}
		}
	} else if (direct_connect()) {
		// Directly connected, use source's buffer directly
		for (uint32_t v = 0; v < _poly; ++v) {
			_voices->at(v).buffer = _arcs.front().buffer(v);
		}
	} else {
		// Mix down to local buffers in pre_run()
		for (uint32_t v = 0; v < _poly; ++v) {
			buffer(v)->prepare_write(context);
		}
	}
}

void
InputPort::pre_run(RunContext& context)
{
	if ((_user_buffer || !_arcs.empty()) && !direct_connect()) {
		const uint32_t src_poly   = max_tail_poly(context);
		const uint32_t max_n_srcs = _arcs.size() * src_poly + 1;

		for (uint32_t v = 0; v < _poly; ++v) {
			if (!buffer(v)->get<void>()) {
				continue;
			}

			// Get all sources for this voice
			const Buffer* srcs[max_n_srcs];
			uint32_t      n_srcs = 0;

			if (_user_buffer) {
				// Add buffer with user/UI input for this cycle
				srcs[n_srcs++] = _user_buffer.get();
			}

			for (const auto& arc : _arcs) {
				if (_poly == 1) {
					// P -> 1 or 1 -> 1: all tail voices => each head voice
					for (uint32_t w = 0; w < arc.tail()->poly(); ++w) {
						assert(n_srcs < max_n_srcs);
						srcs[n_srcs++] = arc.buffer(w, context.offset()).get();
						assert(srcs[n_srcs - 1]);
					}
				} else {
					// P -> P or 1 -> P: tail voice => corresponding head voice
					assert(n_srcs < max_n_srcs);
					srcs[n_srcs++] = arc.buffer(v, context.offset()).get();
					assert(srcs[n_srcs - 1]);
				}
			}

			// Then mix them into our buffer for this voice
			mix(context, buffer(v).get(), srcs, n_srcs);
			update_values(context.offset(), v);
		}
	} else if (is_a(PortType::CONTROL)) {
		for (uint32_t v = 0; v < _poly; ++v) {
			update_values(context.offset(), v);
		}
	}
}

SampleCount
InputPort::next_value_offset(SampleCount offset, SampleCount end) const
{
	SampleCount earliest = end;

	if (_user_buffer) {
		earliest = _user_buffer->next_value_offset(offset, end);
	}

	for (const auto& arc : _arcs) {
		const SampleCount o = arc.tail()->next_value_offset(offset, end);
		if (o < earliest) {
			earliest = o;
		}
	}

	return earliest;
}

void
InputPort::post_process(RunContext& context)
{
	if (!_arcs.empty() || _force_monitor_update) {
		monitor(context, _force_monitor_update);
		_force_monitor_update = false;
	}

	/* Finished processing any user/UI messages for this cycle, drop reference
	   to user buffer. */
	_user_buffer.reset();
}

bool
InputPort::direct_connect() const
{
	return _arcs.size() == 1
		&& !_parent->is_main()
		&& !_arcs.front().must_mix()
		&& buffer(0)->type() != _bufs.uris().atom_Sequence;
}

} // namespace Server
} // namespace Ingen