/* Copyright 2020, Michele Santullo
 * This file is part of orotool.
 *
 * Orotool is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Orotool 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Orotool.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "api.hpp"
#include "orotool_config.hpp"
#if defined(OROTOOL_WITH_RESTCCPP)
#	include "private/api_restccpp.hpp"
#elif defined(OROTOOL_WITH_NAP)
#	include "private/api_nap.hpp"
#else
#	error "No REST backend library specified"
#endif
#if !defined(NDEBUG)
#	include <iostream>
#endif

namespace oro {

Api::Api (
	std::string&& root_address,
	std::string&& api_key,
	std::string&& client_name,
	std::string&& client_purpose
) :
	m_prefix(std::move(root_address)),
	m_api_key(std::move(api_key)),
	m_client_name(std::move(client_name)),
	m_client_purpose(std::move(client_purpose))
{
	if (not m_prefix.empty() and m_prefix[m_prefix.size() - 1] != '/')
		m_prefix.push_back('/');
#if !defined(NDEBUG)
	std::cout << "OriginsRO API settings\n" <<
		"\troot_address: \"" << m_prefix << "\"\n" <<
		"\tapi_key: \"" << m_api_key << "\"\n" <<
		"\tclient_name: \"" << m_client_name << "\"\n" <<
		"\tclient_purpose: \"" << m_client_purpose << "\"\n" <<
		"";
#endif
}

Api::~Api() noexcept = default;

std::unique_ptr<Api> make_api (
	std::string&& root_address,
	std::string&& api_key,
	std::string&& client_name,
	std::string&& client_purpose
) {
#if defined(OROTOOL_WITH_RESTCCPP)
	typedef ApiRestcCpp RetType;
#elif defined(OROTOOL_WITH_NAP)
	typedef ApiNap RetType;
#else
#	error "No REST backend library specified"
#endif

	return std::make_unique<RetType>(
		std::move(root_address),
		std::move(api_key),
		std::move(client_name),
		std::move(client_purpose)
	);
}

} //namespace oro
