/*
 * PackingAbstraction.hpp
 *
 *  Created on: 29. 5. 2018
 *	  Author: Jan Travnicek
 */

#ifndef _PACKING_ABSTRACTION_HPP_
#define _PACKING_ABSTRACTION_HPP_

#include <alib/memory>
#include <alib/array>

#include <abstraction/OperationAbstraction.hpp>
#include <abstraction/Value.hpp>

namespace abstraction {

template < size_t NumberOfParams >
class PackingAbstraction : public OperationAbstraction {
	struct ConnectionTarget {
		size_t targetId;
		size_t paramPosition;
	};

	ext::vector < std::shared_ptr < abstraction::LazyValue > > m_abstractions;
	ext::array < ext::vector < ConnectionTarget >, NumberOfParams > m_connections;
	size_t m_resultId;

public:
	PackingAbstraction ( ext::vector < std::shared_ptr < abstraction::OperationAbstraction > > abstractions, size_t resultId ) : m_resultId ( resultId ) {
		for ( std::shared_ptr < abstraction::OperationAbstraction > & abstraction : abstractions )
			m_abstractions.push_back ( std::make_shared < abstraction::LazyValue > ( std::move ( abstraction ) ) );
	}

	void setInnerConnection ( size_t sourceId, size_t targetId, size_t paramPosition, bool move ) {
		m_abstractions [ targetId ]->getLifeReference ( )->attachInput ( m_abstractions [ sourceId ], paramPosition, move );
	}

	void clearInnerConnection ( size_t targetId, size_t paramPosition ) {
		m_abstractions [ targetId ]->getLifeReference ( )->detachInput ( paramPosition );
	}

	void setOuterConnection ( size_t sourceId, size_t targetId, size_t paramPosition ) {
		m_connections [ sourceId ].push_back ( ConnectionTarget { targetId, paramPosition } );
	}

private:
	void attachInput ( const std::shared_ptr < abstraction::Value > & input, size_t index, bool move ) override {
		try {
			for ( const ConnectionTarget & target : m_connections [ index ] )
				m_abstractions [ target.targetId ]->getLifeReference ( )->attachInput ( input, target.paramPosition, move );
		} catch ( ... ) {
			this->detachInput ( index );
			throw;
		}
	}

	void detachInput ( size_t index ) override {
		for ( const ConnectionTarget & target : m_connections [ index ] )
			m_abstractions [ target.targetId ]->getLifeReference ( )->detachInput ( target.paramPosition );
	}

public:
	bool inputsAttached ( ) const override {
		for ( const std::shared_ptr < abstraction::LazyValue > & operation : m_abstractions )
			if ( ! operation->getLifeReference ( )->inputsAttached ( ) )
				return false;

		return true;
	}

	std::shared_ptr < abstraction::Value > eval ( ) override {
		if ( ! inputsAttached ( ) )
			return nullptr;

		return m_abstractions [ m_resultId ]->getProxyAbstraction ( );
	}

	size_t numberOfParams ( ) const override {
		return NumberOfParams;
	}

	ext::type_index getParamTypeIndex ( size_t index ) const override {
		return m_abstractions [ m_connections.at ( index ) [ 0 ].targetId ]->getLifeReference ( )->getParamTypeIndex ( m_connections.at ( index ) [ 0 ].paramPosition );
	}

	ext::set < abstraction::ParamQualifiers::ParamQualifier > getParamTypeQualifiers ( size_t index ) const override {
		return m_abstractions [ m_connections.at ( index ) [ 0 ].targetId ]->getLifeReference ( )->getParamTypeQualifiers ( m_connections.at ( index ) [ 0 ].paramPosition );
	}

	ext::type_index getReturnTypeIndex ( ) const override {
		return m_abstractions [ m_resultId ]->getLifeReference ( )->getReturnTypeIndex ( );
	}

	ext::set < abstraction::ParamQualifiers::ParamQualifier > getReturnTypeQualifiers ( ) const override {
		return m_abstractions [ m_resultId ]->getLifeReference ( )->getReturnTypeQualifiers ( );
	}

	std::shared_ptr < abstraction::OperationAbstraction > getProxyAbstraction ( ) override {
		return m_abstractions [ m_resultId ]->getLifeReference ( )->getProxyAbstraction ( );
	}

};

} /* namespace abstraction */

extern template class abstraction::PackingAbstraction < 2 >;
extern template class abstraction::PackingAbstraction < 3 >;

#endif /* _PACKING_ABSTRACTION_HPP_ */
