using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Android;

namespace SmartBowSDK
{
    public class BluetoothHelperAndroid
    {
        public static bool IsBluetoothEnabled()
        {
            using (var classBluetoothAdapter = new AndroidJavaClass("android.bluetooth.BluetoothAdapter"))
            using (var bluetoothAdapter = classBluetoothAdapter.CallStatic<AndroidJavaObject>("getDefaultAdapter"))
            {
                if (bluetoothAdapter == null)
                {
                    Debug.Log("当前设备不支持蓝牙功能");
                    return false;
                }
                bool isEnabled = bluetoothAdapter.Call<bool>("isEnabled");
                return isEnabled;
            }
        }

        public static bool RequestBluetoothPermissions(UnityAction onAllGranted, UnityAction<string> onDenied)
        {
            using (var buildVersion = new AndroidJavaClass("android.os.Build$VERSION"))
            {
                int sdkInt = buildVersion.GetStatic<int>("SDK_INT");
                List<string> permissionListA = new List<string>();
                List<string> permissionListB = new List<string>();
                if (sdkInt >= 23)
                {
                    permissionListA.Add(Permission.CoarseLocation);
                    permissionListA.Add(Permission.FineLocation);
                    if (sdkInt < 31)
                    {
                        permissionListB.Add("android.permission.BLUETOOTH");
                        permissionListB.Add("android.permission.BLUETOOTH_ADMIN");
                    }
                    else
                    {
                        permissionListB.Add("android.permission.BLUETOOTH_SCAN");
                        permissionListB.Add("android.permission.BLUETOOTH_ADVERTISE");
                        permissionListB.Add("android.permission.BLUETOOTH_CONNECT");
                    }
                }
                if (IsPermissionsGranted(permissionListA)
                    && IsPermissionsGranted(permissionListB)) return false;
                RequestUserPermissions(permissionListA.ToArray(), () =>
                {
                    RequestUserPermissions(permissionListB.ToArray(), onAllGranted, onDenied);
                }, onDenied);
                return true;
            }
        }

        private static bool IsPermissionsGranted(List<string> permissions)
        {
            foreach (var permission in permissions)
                if (!Permission.HasUserAuthorizedPermission(permission)) return false;
            return true;
        }

        private static void RequestUserPermissions(string[] permissions, UnityAction onAllGranted, UnityAction<string> onDenied)
        {
            bool hasExecuteOnDenied = false;
            List<string> permissionsNeedRequest = new List<string>();
            foreach (var permission in permissions)
                if (!Permission.HasUserAuthorizedPermission(permission)) permissionsNeedRequest.Add(permission);
            if (permissionsNeedRequest.Count > 0)
            {
                var requestCallback = new PermissionCallbacks();
                requestCallback.PermissionGranted += (permission) =>
                {
                    Debug.Log("用户同意" + permission);
                    permissionsNeedRequest.Remove(permission);
                    if (permissionsNeedRequest.Count == 0) onAllGranted?.Invoke();
                };
                requestCallback.PermissionDenied += (permission) =>
                {
                    Debug.LogWarning("用户拒绝" + permission);
                    if (!hasExecuteOnDenied)
                    {
                        hasExecuteOnDenied = true;
                        onDenied?.Invoke(permission);
                    }
                };
                requestCallback.PermissionDeniedAndDontAskAgain += (permission) =>
                {
                    Debug.LogWarning("用户拒绝且要求不再询问" + permission);
                    if (!hasExecuteOnDenied)
                    {
                        hasExecuteOnDenied = true;
                        onDenied?.Invoke(permission);
                    }
                };
                Permission.RequestUserPermissions(permissionsNeedRequest.ToArray(), requestCallback);
            }
            else onAllGranted?.Invoke();
        }
    }
}
