using System;
using RudderSdk.Core.Abstractions;

namespace RudderSdk.Unity
{
  internal class UnityDeviceIdProvider : IDeviceIdProvider
  {
    public string DeviceId => SystemInfoHelper.GetDeviceId();
  }

  internal static class SystemInfoHelper
  {
    private static string _cachedDeviceId;

    public static string GetDeviceId()
    {
      if (_cachedDeviceId != null) return _cachedDeviceId;
      _cachedDeviceId = UnityEngine.PlayerPrefs.GetString("rudder_device_id", null);
      if (string.IsNullOrEmpty(_cachedDeviceId))
      {
        _cachedDeviceId = Guid.NewGuid().ToString("N");
        UnityEngine.PlayerPrefs.SetString("rudder_device_id", _cachedDeviceId);
        UnityEngine.PlayerPrefs.Save();
      }
      return _cachedDeviceId;
    }
  }
}
