#pragma once

#include <seallib/log.h>

#include <fstream>
#include <string>
#include <mutex>
#include <memory>

#include "utils.h"

using namespace seallib;

class FileSink : public ILogSink
{
    public:
        FileSink(const std::string& filename)
        {
            _file.open(utils::getExePath() + filename, std::ios::out | std::ios::app);
        }

        ~FileSink()
        {
            if (_file.is_open())
                _file.close();
        }

        virtual void receiveLog(LogType type, std::string_view loggerName, std::string_view msg) override
        {
            std::lock_guard<std::mutex> lock(_mutex);
            if (_file.is_open())
            {
                _file << "[" << loggerName << "] ["
                      << seallib::getLogTypeName(type) << "] "
                      << msg << std::endl;
            }
        }

        static std::shared_ptr<FileSink> getSharedInstance()
        {
            static std::shared_ptr<FileSink> instance = std::make_shared<FileSink>("hex-unlocked.log");
            return instance;
        }

    private:
        std::ofstream _file;
        std::mutex _mutex;
};
