using System.Runtime.InteropServices;
using UnityEngine;

public class CallingNativeMethods
{
    private CallingNativeMethods() { }
    private static readonly CallingNativeMethods singleInstance = new CallingNativeMethods();
    public static CallingNativeMethods Instance
    {
        get
        {
            return singleInstance;
        }
    }
#if UNITY_ANDROID
    private AndroidJavaObject activity;
    public AndroidJavaObject GetActivity()
    {
        if (activity == null)
        {
            var unityPlayer = new AndroidJavaClass(
            "com.unity3d.player.UnityPlayer");
            activity = unityPlayer.GetStatic<AndroidJavaObject>(
           "currentActivity");
        }
        return activity;
    }
#endif

#if UNITY_IOS
    //判断微信是否安装
    [DllImport("__Internal")]
    private static extern bool _isWechatInstalled();
    [DllImport("__Internal")]
    private static extern void _WechatLogin(string appid, string state, string universalLink);
    [DllImport("__Internal")]
    private static extern void ShowIOSWebView(string urlStr, string titleStr);
#endif

    /// <summary>
    /// 注册初始化微信登录
    /// </summary>
    public void InitWxLogin()
    {
#if UNITY_EDITOR
        Debug.unityLogger.Log("初始化微信登录" + Constant.Instance.WX_APP_ID);
#elif UNITY_ANDROID
        AndroidJavaObject WxLoginUtil = new AndroidJavaClass("com.ym.unityandroid.UnityWxLoginUtil").CallStatic<AndroidJavaObject>("getInstance");
        WxLoginUtil.Call("InitWxLogin", GetActivity(), Constant.Instance.WX_APP_ID);
#elif UNITY_IOS

#endif

    }

    /// <summary>
    /// 微信登录
    /// </summary>
    public void WeChatLogin()
    {
#if UNITY_EDITOR
        Debug.unityLogger.Log("吊起微信登录");
#elif UNITY_ANDROID
        AndroidJavaObject WxLoginUtil = new AndroidJavaClass("com.ym.unityandroid.UnityWxLoginUtil").CallStatic<AndroidJavaObject>("getInstance");
        WxLoginUtil.Call("wxLogin", GetActivity());
#elif UNITY_IOS

        SendWxLogin();

#endif
    }

#if UNITY_IOS
    /// <summary>
    /// iOS 微信登录
    /// </summary>
    public void SendWxLogin()
    {
        bool isInstalled = false;
        isInstalled = _isWechatInstalled();
        if (isInstalled)
        {
            _WechatLogin(Constant.Instance.WX_APP_ID, "state", Constant.Instance.universalLink);
        }
        else
        {
            ToastPlugin.ToastHelper.ShowToast("请先安装微信客户端！");
        }
    }

    /// <summary>
    /// iOS微信登录回调
    /// </summary>
    /// <param name="msg"></param>
    public void LoginCallBack(string msg)
    {
        Debug.Log("微信登录的回调在这里: " + msg);
    }
#endif

    /// <summary>
    /// 
    /// </summary>
    /// <param name="url">H5 URL</param>
    /// <param name="title">H5 Title</param>
    public void OpenWebPage(string url, string title)
    {
#if UNITY_EDITOR
        Debug.unityLogger.Log("打开Web页面Url" + url + "    Title" + title);
#elif UNITY_ANDROID
        AndroidJavaObject WxLoginUtil = new AndroidJavaClass("com.ym.unityandroid.UnityWxLoginUtil").CallStatic<AndroidJavaObject>("getInstance");
        WxLoginUtil.Call("OpenH5Page", GetActivity(),url,title);
#elif UNITY_IOS

        ShowIOSWebView(url, title);

#endif
    }














}
