// 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.</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 data, int size);

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

        [DllImport(LibraryName, EntryPoint = "aidlab_ble_set_data_callback", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetDataCallback(DataCallback callback);

        [DllImport(LibraryName, EntryPoint = "aidlab_ble_set_event_callback", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetEventCallback(EventCallback callback);

        [DllImport(LibraryName, EntryPoint = "aidlab_ble_start", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool Start([MarshalAs(UnmanagedType.LPStr)] string deviceName);

        [DllImport(LibraryName, EntryPoint = "aidlab_ble_stop", CallingConvention = CallingConvention.Cdecl)]
        public static extern void Stop();

        [DllImport(LibraryName, EntryPoint = "aidlab_ble_write", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool Write(byte[] data, int size);

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