using System;
using System.Runtime.InteropServices;

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

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

#if UNITY_IOS || UNITY_TVOS
        private const string LibraryName = "__Internal";

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

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

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

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

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

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