/* Copyright (C) Itseez3D, Inc. - All Rights Reserved
* You may not use this file except in compliance with an authorized license
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* UNLESS REQUIRED BY APPLICABLE LAW OR AGREED BY ITSEEZ3D, INC. IN WRITING, SOFTWARE DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED
* See the License for the specific language governing permissions and limitations under the License.
* Written by Itseez3D, Inc. <support@avatarsdk.com>, August 2026
*/

using System;
using System.IO;
using System.Text;
using UnityEngine;

namespace AvatarSDKMove
{
	public static class PlayerUidStorage
	{
		public static string Load(string clientId)
		{
			try
			{
				string path = GetPath(clientId);
				if (!File.Exists(path))
					return null;

				return Encoding.UTF8.GetString(Convert.FromBase64String(File.ReadAllText(path)));
			}
			catch (Exception ex)
			{
				Debug.LogWarningFormat("Could not read player UID: {0}", ex.Message);
				return null;
			}
		}

		public static void Store(string clientId, string playerUID)
		{
			try
			{
				File.WriteAllText(GetPath(clientId), Convert.ToBase64String(Encoding.UTF8.GetBytes(playerUID)));
			}
			catch (Exception ex)
			{
				Debug.LogWarningFormat("Could not store player UID: {0}", ex.Message);
			}
		}

		private static string GetPath(string clientId)
		{
			string filename = string.Format("player_uid_{0}.dat", EncryptionUtils.GetMd5Hash(clientId));
			return Path.Combine(Application.persistentDataPath, filename);
		}
	}
}
