// The macOS Editor defines UNITY_IOS too while iOS is the build target, and loads the macOS library.
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_IOS
using System;
using System.Runtime.InteropServices;

namespace Aidlab.BLE
{
    /// <summary>CoreBluetooth bridge of Plugins/iOS/aidlab_ble.mm: one native manager per connection.</summary>
    public static class AppleBLEApi
    {
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
        private const string LibraryName = "aidlab_ble_macos";
#else
        // The iOS player links the bridge statically.
        private const string LibraryName = "__Internal";
#endif

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate void DataCallback(IntPtr context, IntPtr data, int size);

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate void EventCallback(IntPtr context, int evt, IntPtr message);

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        [return: MarshalAs(UnmanagedType.I1)]
        public delegate bool DiscoveryCallback(IntPtr context, IntPtr identifier, IntPtr name);

        [DllImport(LibraryName, EntryPoint = "aidlab_ble_create", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr Create(IntPtr context, DataCallback dataCallback, EventCallback eventCallback,
                                           DiscoveryCallback discoveryCallback);

        [DllImport(LibraryName, EntryPoint = "aidlab_ble_destroy", CallingConvention = CallingConvention.Cdecl)]
        public static extern void Destroy(IntPtr manager);

        [DllImport(LibraryName, EntryPoint = "aidlab_ble_write", CallingConvention = CallingConvention.Cdecl)]
        [return: MarshalAs(UnmanagedType.I1)]
        public static extern bool Write(IntPtr manager, byte[] data, int size);

        [DllImport(LibraryName, EntryPoint = "aidlab_ble_get_max_write_length", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetMaxWriteLength(IntPtr manager);
    }
}
#endif
