////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "ClientConfig.h"
#include <limits>

namespace
{
  const uint32_t ERROR_VERSION_VALUE = std::numeric_limits<uint32_t>::max();

  // Find the client version position in the client id
  // Client id format - GAME_PRODUCT_ID ":" GAME_GGI ":" CLIENT_VERSION_MAJOR "." CLIENT_VERSION_MINOR "." CLIENT_VERSION_PATCH ":" GAME_PLATFORM ":" GAME_PLATFORM_STORE
  std::string::size_type GetClientVersionPositionInString(const std::string & a_sClientId)
  {
    std::string::size_type sep_pos = a_sClientId.find_first_of(':');
    if (sep_pos != std::string::npos)
    {
      return a_sClientId.find_first_of(':', sep_pos + 1);
    }
    return std::string::npos;
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

JSON_SERIALIZE_BASE_CLASS(ClientConfig)
JSON_AREA_BEGIN("internals")
JSON_SERIALIZE(m_clientSvnRevision, "clientSvnRevision");
JSON_SERIALIZE(m_clientId, "clientId");
JSON_SERIALIZE(m_deviceId, "deviceId");
JSON_SERIALIZE(m_gameDbChecksum, "dbChecksum");
JSON_SERIALIZE(m_allowClientInactivityDetection, "inactivityDetect");
JSON_SERIALIZE(m_ReportEmailAddress, "emailreport");
JSON_SERIALIZE(m_sendServerTrace, "serverTrace");
JSON_SERIALIZE(m_countryCode, "countryCode");
JSON_AREA_END()
JSON_SERIALIZE_END()

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void ClientConfig::ExtractClientVersionsFromClientId(uint32_t & a_uiMaj, uint32_t & a_uiMin) const
{
  // Extract the client major version from the client id
  a_uiMaj = ERROR_VERSION_VALUE;
  a_uiMin = ERROR_VERSION_VALUE;
  std::string::size_type sep_pos = GetClientVersionPositionInString(GetClientId());
  if (sep_pos == std::string::npos)
  {
    return;
  }
  std::string::size_type dot_pos = GetClientId().find_first_of('.', sep_pos + 1);
  if (dot_pos == std::string::npos)
  {
    return;
  }
  const std::string majorVersionAsStr = GetClientId().substr(sep_pos + 1, dot_pos - sep_pos - 1);
  a_uiMaj = std::strtoul(majorVersionAsStr.c_str(), nullptr, 10);

  sep_pos = GetClientId().find_first_of('.', dot_pos + 1);
  if (sep_pos == std::string::npos)
  {
    return;
  }
  const std::string minorVersionAsStr = GetClientId().substr(dot_pos + 1, sep_pos - dot_pos - 1);
  a_uiMin = std::strtoul(minorVersionAsStr.c_str(), nullptr, 10);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
