// #TODO(Low priority) #Logger module
// [ ] Add colored prints (See: Print_Color.jai)

// See Logger.jai in our jiim-dev-gui project for how to do fancy colored text.
enum class Log_Level : s32 {
  TODO        = -2,
  Trace       = -1,
  None        = 0,
  Info        = 1,
  Warning     = 2,
  Error       = 3,
  Fatal_Error = 4,
};

// log_function pointer
typedef void (*Logger_Proc)(string log_message, Log_Level level, void* data);
void default_logger_proc (string log_message, Log_Level level, void* data); //fwd declared

struct Logger {
  Logger_Proc proc;
  void* data;
};

struct Default_Logger {
  Logger_Proc proc = default_logger_proc;
  String_Builder* sb = nullptr;
  
  Mutex* mutex;
#if OS_WINDOWS
  void* windows_standard_output;
  void* windows_standard_error;
#endif
};

global Default_Logger default_logger;

// default_logger_proc()...
#if OS_WINDOWS
  void os_write_string_unsynchronized(string s, bool to_standard_error) {
    u32 written = 0;
    void* handle = (to_standard_error) ? 
          default_logger.windows_standard_error : 
          default_logger.windows_standard_output;
    Assert(handle != nullptr);
    bool result = (bool)WriteFile(handle, s.data, (u32)s.count, (LPDWORD)&written, nullptr);
  }
#endif

void default_logger_proc (string log_message, Log_Level level, void* data) {
  bool to_standard_error = level == Log_Level::Error || level == Log_Level::Fatal_Error;

  lock(default_logger.mutex);
  os_write_string_unsynchronized(log_message, to_standard_error);
  unlock(default_logger.mutex);
}

void default_logger_initialize() {
  // see: Bootstrap_Main_Thread_Context
  default_logger.mutex = New<Mutex>(true);
  mutex_init(default_logger.mutex);

#if OS_WINDOWS
  default_logger.windows_standard_output = GetStdHandle(STD_OUTPUT_HANDLE);
  default_logger.windows_standard_error  = GetStdHandle(STD_ERROR_HANDLE);
#endif
}

void log (string fmt, ...);
void print (string message);
void print_error (string error_message);
