#include <logging.hpp>

#include <spdlog/spdlog.h>
#include <spdlog/common.h>
#include <spdlog/logger.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>

namespace OpenEngine {

    std::shared_ptr<spdlog::logger> Logger::core_logger;
    std::shared_ptr<spdlog::logger> Logger::client_logger;

    void Logger::Init()
    {
        spdlog::set_pattern("%^[%H:%M:%S] [%l] %n: %v%$");
        core_logger = spdlog::stdout_color_mt("OpenEngine");
        core_logger->set_level(spdlog::level::trace);

        client_logger = spdlog::stdout_color_mt("App");
        client_logger->set_level(spdlog::level::trace);
    }

    int setupMultisinkLogger(const std::string &file_path)
    {
        if (spdlog::get("open-engine_logger"))
            return 1;

        auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
        auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(file_path);
        console_sink->set_level(spdlog::level::info);
        file_sink->set_level(spdlog::level::debug);

        auto logger = std::make_shared<spdlog::logger>("open-engine_logger", spdlog::sinks_init_list({console_sink, file_sink}));
        logger->set_pattern("[%H:%M:%S] [%^%l%$] %n: %v");
        logger->flush_on(spdlog::level::info);

        if (!logger) {
            std::cerr << "FATAL: Failed to get logger." << std::endl;
            return 1;
        }

        spdlog::register_logger(logger);
        spdlog::set_default_logger(logger);

        logger->info("Logger ready.");

        return 0;
    }
}
