using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Android;
using XRTool.Util;

namespace XRTool.Util
{
    /// <summary>
    /// 安卓与Unity接口类交互
    /// </summary>
    public class AndroidLib : UnitySingleton<AndroidLib>
    {

        private static AndroidJavaObject currentActivity;
        /// <summary>
        /// 安卓的Activity
        /// </summary>
        public static AndroidJavaObject MainActivity
        {
            get
            {
#if UNITY_ANDROID
                if (currentActivity == null)
                {
                    AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                    currentActivity = up.GetStatic<AndroidJavaObject>("currentActivity");
                }
#endif
                return currentActivity;
            }
        }
        protected override void Awake()
        {
            name = typeof(AndroidLib).Name;
            base.Awake();
            DontDestroyOnLoad(gameObject);
        }

        /// <summary>
        /// 安卓回调接口，安卓调用Unity
        /// 统一接口？
        /// 
        /// </summary>
        public void AndroidCallBack(string funName)
        {

        }
        /// <summary>
        /// 调用安卓代码
        /// </summary>
        public static T CallAndLibMethod<T>(AndroidJavaObject andObj, bool isStatic, string meshod, params object[] args)
        {
            if (andObj != null)
            {
                if (isStatic)
                {
                    return andObj.CallStatic<T>(meshod, args);
                }
                return andObj.Call<T>(meshod, args);
            }
            return default(T);
        }
        /// <summary>
        /// 调用安卓代码
        /// </summary>
        public static T CallAndLibMethod<T>(AndroidJavaObject andObj, bool isStatic, string meshod)
        {
            if (andObj != null)
            {
                if (isStatic)
                {
                    return andObj.CallStatic<T>(meshod);
                }
                return andObj.Call<T>(meshod);
            }
            return default(T);
        }
        /// <summary>
        /// 调用安卓代码
        /// </summary>
        public static void CallAndLibMethod(AndroidJavaObject andObj, bool isStatic, string meshod, params object[] args)
        {
            if (andObj != null)
            {
                if (isStatic)
                {
                    andObj.CallStatic(meshod, args);
                }
                else
                {
                    andObj.Call(meshod, args);
                }
            }
        }
        public static bool CheckCamera()
        {
            if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
            {
                Permission.RequestUserPermission(Permission.Camera);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 调用安卓代码
        /// </summary>
        public static void CallAndLibMethod(AndroidJavaObject andObj, bool isStatic, string meshod)
        {
            if (andObj != null)
            {
                if (isStatic)
                {
                    andObj.CallStatic(meshod);
                }
                else
                {
                    andObj.Call(meshod);
                }
            }
        }


        /// <summary>
        /// 调用安卓代码
        /// </summary>
        public static T CallAndLibMethod<T>(bool isStatic, string meshod, params object[] args)
        {
            return CallAndLibMethod<T>(MainActivity,isStatic, meshod, args);
        }
        /// <summary>
        /// 调用安卓代码
        /// </summary>
        public static T CallAndLibMethod<T>( bool isStatic, string meshod)
        {
            return CallAndLibMethod<T>(MainActivity, isStatic, meshod);
        }
        /// <summary>
        /// 调用安卓代码
        /// </summary>
        public static void CallAndLibMethod(bool isStatic, string meshod, params object[] args)
        {
            CallAndLibMethod(MainActivity, isStatic, meshod, args);
        }
        
        /// <summary>
        /// 调用安卓代码
        /// </summary>
        public static void CallAndLibMethod(bool isStatic, string meshod)
        {
            CallAndLibMethod(MainActivity, isStatic, meshod);
        }

    }
}
