#include <pch.hpp>

#include <core.hpp>
#include <logging.hpp>

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

namespace OpenEngine {

    Ref<spdlog::logger> Logger::core_logger;
    Ref<spdlog::logger> Logger::client_logger;

    void Logger::Init()
    {
        OE_PROFILE_FUNCTION();

        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)
    {
        OE_PROFILE_FUNCTION();

        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;
    }
}
