using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using AOT;
using UnityEngine;

public class UnityPhoneUtils
{
    private UnityPhoneUtils() { }
    private static readonly UnityPhoneUtils singleInstance = new UnityPhoneUtils();
    public static UnityPhoneUtils Instance
    {
        get
        {
            return singleInstance;
        }
    }

    private AndroidJavaObject activity;
    public AndroidJavaObject GetActivity()
    {
        if (activity == null)
        {
            var unityPlayer = new AndroidJavaClass(
            "com.unity3d.player.UnityPlayer");
            activity = unityPlayer.GetStatic<AndroidJavaObject>(
           "currentActivity");
        }
        return activity;
    }

    private AndroidJavaObject phoneUtil;
    private AndroidJavaObject GetPhoneUtil()
    {
        if (phoneUtil == null)
        {
            phoneUtil = new AndroidJavaClass("com.ym.common.UnityPhoneUtils").CallStatic<AndroidJavaObject>("getInstance");
        }
        return phoneUtil;
    }

    public string GetDevice()
    {
        if (null != Constant.Instance.device && !Constant.Instance.device.Equals(""))
        {
            return Constant.Instance.device;
        }
        Constant.Instance.device = PlayerPrefs.GetString("user_device", "");
        if (Constant.Instance.device.Equals(""))
        {
#if UNITY_EDITOR
            Constant.Instance.device = "debug_test_003";
#elif UNITY_ANDROID
             Constant.Instance.device = GetPhoneUtil().Call<string>("getDeviceId", GetActivity());
#endif

            PlayerPrefs.SetString("user_device", Constant.Instance.device);
        }
        Debug.unityLogger.Log("GetDevice " + Constant.Instance.device);
        return Constant.Instance.device;
    }


}
