﻿using JLGames.RocketDriver.CSharp.DateTimex;
using UnityEngine;

namespace JLGames.RocketDriver.Actions.Utils
{
    public static class IdentifyUtil
    {
        /// <summary>
        /// ASIdentifierManager iOS
        /// AdvertisingManager Windows Store Apps
        /// </summary>
        public static string DeviceUniqueIdentifier => SystemInfo.deviceUniqueIdentifier;

        /// <summary>
        /// Get guest id
        /// 取游客Id
        /// </summary>
        /// <param name="maxLen"></param>
        /// <returns></returns>
        public static string GetGuestId(int maxLen = 8)
        {
            var uuid = DeviceUniqueIdentifier;
            if (!string.IsNullOrEmpty(uuid))
            {
                return maxLen <= 0 ? $"{GetMachinePrefix()}_{uuid}" : $"{GetMachinePrefix()}_{uuid.Substring(0, maxLen)}";
            }

            return GetUserIdByTime();
        }

        /// <summary>
        /// Generate account ID from timestamp
        /// 通过时间戳生成账号Id
        /// </summary>
        /// <returns></returns>
        public static string GetUserIdByTime()
        {
            return $"{GetMachinePrefix()}_{GetIdByTime()}";
        }

        /// <summary>
        /// Generate User Nickname from timestamp
        /// 通过时间戳生成用户昵称
        /// </summary>
        /// <returns></returns>
        public static string GetNickNameByTime()
        {
            return $"User_{GetIdByTime()}";
        }

        /// <summary>
        /// Generate Id by time
        /// 通过时间生成Id
        /// </summary>
        /// <returns></returns>
        public static string GetIdByTime()
        {
            var ts = DateTimeUtil.NowMilliseconds1970;
            return $"{ts:x8}";
        }

        /// <summary>
        /// Get machine type prefix
        /// 获取机器类型前缀
        /// </summary>
        /// <returns></returns>
        public static string GetMachinePrefix()
        {
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
            return $"OSX";
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
            return $"WIN";
#elif UNITY_ANDROID
            return $"AND";
#elif UNITY_IOS
            return $"IOS";
#elif UNITY_WEBGL
            return $"WEB";
#else
            return $"Other";
#endif
        }
    }
}