using System;
using System.Runtime.InteropServices;

namespace Aidlab.BLE
{
    public static class MacBLEApi
    {
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate void DataCallback(IntPtr data, int size);

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

#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
        private const string LibraryName = "aidlab_ble_macos";

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

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

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

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

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

        [DllImport(LibraryName, EntryPoint = "aidlab_ble_macos_get_max_write_length", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetMaxWriteLength();
#else
        public static void SetDataCallback(DataCallback callback) => throw new PlatformNotSupportedException("MacBLEApi is available only on macOS.");
        public static void SetEventCallback(EventCallback callback) => throw new PlatformNotSupportedException("MacBLEApi is available only on macOS.");
        public static bool Start(string deviceName) => throw new PlatformNotSupportedException("MacBLEApi is available only on macOS.");
        public static void Stop() => throw new PlatformNotSupportedException("MacBLEApi is available only on macOS.");
        public static bool Write(byte[] data, int size) => throw new PlatformNotSupportedException("MacBLEApi is available only on macOS.");
        public static int GetMaxWriteLength() => throw new PlatformNotSupportedException("MacBLEApi is available only on macOS.");
#endif
    }
}
