/* 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/>.
 */

#pragma once

#include "SQLiteCpp/Statement.h"
#include <optional>
#include <utility>
#include <type_traits>

namespace oro {
namespace detail {
	template <typename T> struct RowRetInfo;
	template <typename... R, template <typename...> class Ret>
	struct RowRetInfo<Ret<R...>> {
		typedef Ret<R...> type;
		constexpr static const std::size_t count = sizeof...(R);
	};

	template <typename T>
	struct NoCopy {
		explicit NoCopy (T& v) :
			val(v)
		{ }

		T& val;
	};

	template <typename To, typename From>
	struct Cast {
		explicit Cast (From& v) :
			val(v)
		{ }

		From& val;
	};

	template <typename CastT=void, typename T=void>
	void bind_impl (SQLite::Statement& st, int idx, Cast<CastT, T>&& val);

	template <typename CastT=void, typename T=void>
	void bind_impl (SQLite::Statement& st, int idx, NoCopy<T>&& val);

	template <typename CastT=void, typename T=void>
	void bind_impl (SQLite::Statement& st, int idx, T&& val);

	template <typename CastT=void, typename T=void>
	void bind_impl (SQLite::Statement& st, int idx, const std::optional<T>& val);



	template <typename CastT=void, typename T=void>
	inline void bind_impl (SQLite::Statement& st, int idx, Cast<CastT, T>&& val) {
		bind_impl<CastT, T>(st, idx, val.val);
	}

	template <typename CastT=void, typename T=void>
	inline void bind_impl (SQLite::Statement& st, int idx, NoCopy<T>&& val) {
		st.bindNoCopy(idx, val.val);
	}

	template <typename CastT=void, typename T=void>
	inline void bind_impl (SQLite::Statement& st, int idx, T&& val) {
		if constexpr (std::is_same_v<std::decay_t<T>, std::decay_t<CastT>> or std::is_same_v<void, CastT>) {
			st.bind(idx, std::forward<T>(val));
		}
		else {
			st.bind(idx, static_cast<CastT>(val));
		}
	}

	template <typename CastT=void, typename T=void>
	inline void bind_impl (SQLite::Statement& st, int idx, const std::optional<T>& val) {
		if (val)
			detail::bind_impl<CastT> (st, idx, *val);
		else
			st.bind(idx, nullptr);
	}

	template <int... Indices, typename... Args>
	inline void bind_all (SQLite::Statement& st, std::integer_sequence<int, Indices...>, Args&&... args) {
		const int dummy = (
			bind_impl(st, Indices + 1, std::forward<Args>(args)), ..., 0
		);
		static_cast<void>(dummy);
	}
} //namespace detail

template <typename T>
inline detail::NoCopy<T> no_copy (T& val) {
	return detail::NoCopy<T>{val};
}

template <typename To, typename From>
inline detail::Cast<To, From> cast (From& val) {
	return detail::Cast<To, From>(val);
}

template <typename... Args>
inline void bind_all (SQLite::Statement& st, Args&&... args) {
	detail::bind_all(st, std::make_integer_sequence<int, sizeof...(Args)>{}, std::forward<Args>(args)...);
}

template <typename CastT=void, typename T=void>
inline void bind (SQLite::Statement& st, int index, T&& val) {
	detail::bind_impl<CastT>(st, index, std::forward<T>(val));
}

template <typename T, typename... Args>
inline std::optional<T> exec_first_res (SQLite::Statement& st, Args&&... args) {
	detail::bind_all(st, std::make_integer_sequence<int, sizeof...(Args)>{}, std::forward<Args>(args)...);

	std::optional<T> retval;
	if (st.executeStep()) {
		retval = st.getColumn(0);
	}

	st.reset();
	return retval;
}

template <typename Ret, typename... Args>
inline std::optional<Ret> exec_first_row (SQLite::Statement& st, Args&&... args) {
	detail::bind_all(st, std::make_integer_sequence<int, sizeof...(Args)>{}, std::forward<Args>(args)...);

	std::optional<Ret> retval;
	if (st.executeStep()) {
		retval = st.getColumns<Ret, detail::RowRetInfo<Ret>::count>();
	}

	st.reset();
	return retval;
}
} //namespace oro
