/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "common/logger.hpp"

#include <cstdlib>

using namespace polygun::utils;

Logger* Logger::s_instance = nullptr;

Logger::Logger(bool output_to_file, bool enable_verbose) :
	m_output_to_file(output_to_file),
	m_enable_verbose(enable_verbose),
	m_output_file(nullptr),
	m_access_mutex()
{
	if(output_to_file) {
		m_output_file = fopen("log.txt", "w");
		if(!m_output_file) {
			m_output_to_file = false;
			LOG_ERROR("Failed to open 'log.txt' for writing, logging to file won't be enabled");
		}
	}
}

Logger::~Logger() {
	if(m_output_to_file)
		fclose(m_output_file);
}

void Logger::info(const char* file, size_t line, const char* fmt, ...) {
	m_access_mutex.lock();
	va_list args;
	va_start(args, fmt);
	print_internal(stdout, "", "info", file, line, fmt, args);
	va_end(args);
	if(m_output_to_file) {
		va_start(args, fmt);
		print_internal(m_output_file, "", "info", file, line, fmt, args);
		va_end(args);
	}
	m_access_mutex.unlock();
}

void Logger::warning(const char* file, size_t line, const char* fmt, ...) {
	m_access_mutex.lock();
	va_list args;
	va_start(args, fmt);
	print_internal(stdout, "\033[33m", "warning", file, line, fmt, args);
	va_end(args);
	if(m_output_to_file) {
		va_start(args, fmt);
		print_internal(m_output_file, "", "warning", file, line, fmt, args);
		va_end(args);
	}
	m_access_mutex.unlock();
}

void Logger::error(const char* file, size_t line, const char* fmt, ...) {
	m_access_mutex.lock();
	va_list args;
	va_start(args, fmt);
	print_internal(stderr, "\033[31m", "error", file, line, fmt, args);
	va_end(args);
	if(m_output_to_file) {
		va_start(args, fmt);
		print_internal(m_output_file, "", "error", file, line, fmt, args);
		va_end(args);
	}
	m_access_mutex.unlock();
}

void Logger::fatal(const char* file, size_t line, const char* fmt, ...) {
	m_access_mutex.lock();
	va_list args;
	va_start(args, fmt);
	print_internal(stderr, "\033[91m", "fatal", file, line, fmt, args);
	va_end(args);
	if(m_output_to_file) {
		va_start(args, fmt);
		print_internal(m_output_file, "", "fatal", file, line, fmt, args);
		va_end(args);
	}
	m_access_mutex.unlock();
	exit(1);
}

void Logger::verbose(const char* file, size_t line, const char* fmt, ...) {
	if(!m_enable_verbose)
		return;
	m_access_mutex.lock();
	va_list args;
	va_start(args, fmt);
	print_internal(stdout, "\033[90m", "verbose", file, line, fmt, args);
	va_end(args);
	if(m_output_to_file) {
		va_start(args, fmt);
		print_internal(m_output_file, "", "verbose", file, line, fmt, args);
		va_end(args);
	}
	m_access_mutex.unlock();
}

void Logger::create(bool output_to_file, bool enable_verbose) {
	if(s_instance)
		cleanup();
	s_instance = new Logger(output_to_file, enable_verbose);
}

void Logger::print_internal(FILE* output, const char* color, const char* mode, const char* file, size_t line, const char* fmt, va_list args) {
	fprintf(output, "%s[%s] %s:%zu: ", color, mode, file, line);
	vfprintf(output, fmt, args);
	fprintf(output, "\n%s", output==m_output_file?"":"\033[0m");
	fflush(output);
}
