/*
* Author:          Patrick-Christopher Mattulat
* Co-Author:       Claude Sonnet 4.6 (LLM)
* Company:         Lynar Studios
* E-Mail:          webmaster@lynarstudios.com
* Created:         2023-03-31
* Changed:         2026-06-23
*
* */

#if defined(unix) || defined(__APPLE__)
  #include <ctime>
#endif
#include <ls-std/time/common/DateParameterMapper.hpp>

using ls::standard::time::DateParameter;
using ls::standard::time::DateParameterMapper;
using ls::standard::time::type::UnixTimestamp;

DateParameterMapper::DateParameterMapper() = default;

DateParameterMapper::~DateParameterMapper() = default;

#if defined(unix) || defined(__APPLE__)
UnixTimestamp DateParameterMapper::toUnixTimestamp(const DateParameter &_dateParameter)
{
  time_t rawTime{};
  ::time(&rawTime);
  struct tm timeInfo
  {
  };
  localtime_r(&rawTime, &timeInfo);

  timeInfo.tm_year = _dateParameter.getYear() - 1900;
  timeInfo.tm_mon = _dateParameter.getMonth() - 1;
  timeInfo.tm_mday = _dateParameter.getDay();
  timeInfo.tm_hour = _dateParameter.getHour();
  timeInfo.tm_min = _dateParameter.getMinute();
  timeInfo.tm_sec = _dateParameter.getSecond();
  timeInfo.tm_isdst = -1;

  return static_cast<UnixTimestamp>(mktime(&timeInfo));
}
#endif

#ifdef _WIN32
SYSTEMTIME DateParameterMapper::toWindowsSystemTime(const DateParameter &_dateParameter)
{
  SYSTEMTIME systemTime{};
  systemTime.wYear = _dateParameter.getYear();
  systemTime.wMonth = _dateParameter.getMonth();
  systemTime.wDayOfWeek = 0;
  systemTime.wDay = _dateParameter.getDay();
  systemTime.wHour = _dateParameter.getHour();
  systemTime.wMinute = _dateParameter.getMinute();
  systemTime.wSecond = _dateParameter.getSecond();
  systemTime.wMilliseconds = 0;

  return systemTime;
}
#endif
