/*
 * Dot.cpp
 *
 *  Created on: 16. 8. 2017
 *	  Author: Jan Travnicek
 */

#include "Dot.h"
#include <registration/AlgoRegistration.hpp>
#include <cstdlib>
#include <alib/algorithm>
#include <cctype>

#include <exception/CommonException.h>

namespace cli {

namespace builtin {

std::vector < std::string> Dot::m_DotAllowedTypes { "dot", "xdot", "ps", "pdf", "svg", "svgz", "fig", "png", "gif", "jpg", "jpeg", "json", "imap", "cmapx" };

void Dot::dot ( const std::string & data ) {
	int res = std::system( ( "dot -Tx11 <<DOTDATA\n" + data + "\nDOTDATA" ).c_str ( ) );

	if ( res )
		throw exception::CommonException ( "The dot command exited unsuccessfuly. Return value was " + ext::to_string ( res ) + "." );
}

void Dot::dot ( const std::string & data, const std::string & outputType, const std::string & outputFile ) {
	std::string outputTypeLower;
	std::transform ( outputType.begin( ), outputType.end(), std::back_inserter ( outputTypeLower ), ::tolower);

	if ( std::find ( m_DotAllowedTypes.begin( ), m_DotAllowedTypes.end ( ), outputType ) == m_DotAllowedTypes.end ( ) )
		throw exception::CommonException ( "Dot outputType is invalid." );

	int res = std::system ( ( "dot -T" + outputTypeLower + " -o " + outputFile + "<<DOTDATA\n" + data + "\nDOTDATA" ).c_str ( ) );

	if ( res )
		throw exception::CommonException ( "The dot command exited unsuccessfuly. Return value was " + ext::to_string ( res ) + "." );
}

auto DotTx11 = registration::AbstractRegister < Dot, void, const std::string & > ( Dot::dot, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, "dot_data" );
auto DotFile = registration::AbstractRegister < Dot, void, const std::string &, const std::string &, const std::string & > ( Dot::dot, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, "dot_data", "output_type", "file_name" );

} /* namespace builtin */

} /* namespace cli */