using System;
using System.Runtime.InteropServices;

namespace skyscraper5.Skyscraper.IO.LibDvbV5
{
    internal static class LibDvbV5
    {
        public const int O_RDONLY = 0;
        public const int O_RDWR = 2;
        
        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_dev_alloc", SetLastError = true)]
        internal static extern IntPtr DvbDevAlloc();

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_dev_close")]
        internal static extern void DvbDevClose(IntPtr openDev);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_dev_find")]
        internal static extern int DvbDevFind(IntPtr dvb, int enableMonitor);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_dev_free")]
        internal static extern void DvbDevFree(IntPtr dvb);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_dev_open")]
        internal static extern IntPtr DvbDevOpen(IntPtr dvb, string sysname, int flags);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_dev_seek_by_sysname")]
        internal static extern IntPtr DvbDevSeekBySysname(IntPtr dvb, uint adapter, uint num, DvbDeviceType type);
        
        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_dev_set_log")]
        internal static extern IntPtr DvbDevSetLog(IntPtr dvb, uint verbose, [MarshalAs(UnmanagedType.FunctionPtr)] LibDvbV5LogFunction logfunc);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_fe_open")]
        internal static extern IntPtr DvbFrontendOpen(uint adapter, uint frontend, uint verbose, uint useLegacyCall);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_fe_close")]
        internal static extern void DvbFrontendClose(IntPtr ptr);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_fe_set_parms")]
        internal static extern int DvbFrontendSetParameters(IntPtr ptr);
        
        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_fe_store_parm")]
        internal static extern int DvbFrontentStoreParameter(IntPtr ptr, uint cmd, uint value);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_print_all_lnb")]
        internal static extern void DvbPrintAllLnb();

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_sat_get_lnb")]
        internal static extern IntPtr DvbSatGetLnb(int index);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_sat_search_lnb")]
        internal static extern int DvbSatSearchLnb(string name);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_fe_get_stats")]
        internal static extern int DvbFrontendGetStatistics(IntPtr parms);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_fe_retrieve_stats")]
        internal static extern int DvbFrontendRetrieveStats(IntPtr parms, uint cmd, ref uint value);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_dev_set_bufsize")]
        internal static extern int DvbDeviceSetBufferSize(IntPtr openDev, int bufferSize);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_dev_dmx_set_pesfilter")]
        internal static extern int DvbDeviceDemuxerSetPesFilter(IntPtr openDev, int pid, DemuxerPesType type,DemuxerOutput output, int bufferSize);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_dev_read")]
        internal static extern int DvbDeviceRead(IntPtr openDev, byte[] buf, int count);

        [DllImport("libdvbv5.so.0", EntryPoint = "dvb_set_sys")]
        internal static extern int DvbSetSystem(IntPtr parms, LibDvbV5FrontendDeliverySystem sys);
    }
    
    public delegate void LibDvbV5LogFunction(int level, string fmt);
}