/* Logger.h */

/* Copyright (C) 2011-2024 Michael Lugmair (Lucio Carreras)
 *
 * This file is part of sayonara player
 *
 * This program 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.

 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LOGGER_H
#define LOGGER_H

#include <QMetaType>
#include <QString>

#include <memory>
#include <string>
#include <type_traits>
#include <typeinfo>

class QString;
class QStringList;
class QByteArray;
class QPoint;
class QChar;
class LogListener;
class QRegion;
class QMargins;
class QSize;
class QRect;

enum class Log :
	unsigned char
{
	Warning = 0,
	Error,
	Info,
	Debug,
	Verbose,
	Verbose2,
	Always
};

class Logger
{
	public:
		Logger(const Log& type, const QString& className);
		~Logger();

		static void registerLogListener(LogListener* logListener);

		Logger& operator<<(const QString& msg);
		Logger& operator<<(const QChar& c);
		Logger& operator<<(const QStringList& lst);
		Logger& operator<<(const QByteArray& arr);
		Logger& operator<<(const QPoint& point);
		Logger& operator<<(const QSize& size);
		Logger& operator<<(const QRect& size);
		Logger& operator<<(const char* str);
		Logger& operator<<(const Log& log_type);

		template<typename T>
		std::enable_if_t<std::is_floating_point_v<T>, Logger&>
		operator<<(const T& val)
		{
			if (!m_ignore)
			{
				*this << std::to_string(val).c_str();
			}

			return *this;
		}

		template<typename T>
		std::enable_if_t<std::is_integral_v<T>, Logger&>
		operator<<(const T& val)
		{
			if (!m_ignore)
			{
				*this << std::to_string(val).c_str();
			}

			return *this;
		}

		template<typename T, template<typename ELEM> class CONT>
		Logger& operator<<(const CONT<T>& list)
		{
			if (!m_ignore)
			{
				for(const T& item: list)
				{
					*this << item << ", ";
				}
			}

			return *this;
		}

	private:
		bool m_ignore{false};

		struct Private;
		std::unique_ptr<Private> m{nullptr};
};

Logger spLog(const Log& type, const char* data);

template<typename T>
std::enable_if_t<std::is_class_v<T>, Logger>
spLog(const Log& type, const T* /*inst*/)
{
	return spLog(type, typeid(T).name());
}

Q_DECLARE_METATYPE(Log)

#endif // LOGGER_H
