// See https://aka.ms/new-console-template for more information
using DSRemoteCSharp;
using System.Text;
using System;
class MainClass
{
    private static bool keepRunning = true;
    public static void Main(string[] args)
    {
        byte[] dsconVersion = new byte[50];
        DSRemoteWrapper.dsconGetVersion(dsconVersion, 50);
        Console.WriteLine(Encoding.ASCII.GetString(dsconVersion, 0, 50));

        IntPtr dsconInstance = (IntPtr)0;
        DSRemoteWrapper.dsconCreateInstance(ref dsconInstance, 1);

        string connectionString = "localhost:8999:8001";
        DSRemoteWrapper.dsconConnect(dsconInstance, connectionString);

        UIntPtr numChannels = UIntPtr.Zero;
        DSRemoteWrapper.dsconGetChannelCount(dsconInstance, ref numChannels);
        Console.WriteLine(numChannels.ToUInt64());

        List<IntPtr> channels = new List<IntPtr>();
        DSRemoteWrapper.dsconInitChannelIdEnum(dsconInstance);

        int idSize = 0;
        DSRemoteWrapper.dsconGetNextChannelIdSize(dsconInstance, ref idSize);
        byte[] id = new byte[idSize];
        string idString = "";
        IntPtr chInstance = (IntPtr)0;

        while (DSRemoteWrapper.dsconGetNextChannelId(dsconInstance, id) == 0)
        {
            idString = Encoding.ASCII.GetString(id, 0, idSize);

            DSRemoteWrapper.dsconCreateChannelInstance(dsconInstance, id, ref chInstance);

            channels.Add(chInstance);

            DSRemoteWrapper.dsconGetNextChannelIdSize(dsconInstance, ref idSize);
            id = new byte[idSize];
        }
        idString = Encoding.ASCII.GetString(id, 0, idSize);
        DSRemoteWrapper.dsconCreateChannelInstance(dsconInstance, id, ref chInstance);
        channels.Add(chInstance);

        foreach (IntPtr channel in channels)
        {
            byte[] chName = new byte[50];
            byte[] chUnit = new byte[50];
            double sampleRate = 0.0;
            int chType = 0;

            DSRemoteWrapper.dsconChannelGetName(channel, chName, 50);
            DSRemoteWrapper.dsconGetChUnit(channel, chUnit, 50);
            DSRemoteWrapper.dsconGetSampleRate(channel, ref sampleRate);
            DSRemoteWrapper.dsconGetChannelType(channel, ref chType);

            Console.WriteLine(Encoding.ASCII.GetString(chName, 0, 50));
            Console.WriteLine(Encoding.ASCII.GetString(chUnit, 0, 50));
            Console.WriteLine(sampleRate);
            Console.WriteLine(chType);
        }

        Console.CancelKeyPress += new ConsoleCancelEventHandler(CancelEventHandler);

        double[] data = new double[10000];
        double[] time = new double[10000];

        DSRemoteWrapper.dsconStartMeasurement(dsconInstance);

        UIntPtr count = (UIntPtr)10000;
        while (MainClass.keepRunning)
        {
            count = (UIntPtr)10000;
            DSRemoteWrapper.dsconChannelReadScalarData_2(channels[0], data, time, ref count);
            if (count.ToUInt64() > 0)
            { 
                Console.WriteLine("Channel data:");
                for (uint i = 0; i < count.ToUInt64(); i++)
                    Console.WriteLine(" {0}", data[i]);
                Console.WriteLine();
            }
        }

        DSRemoteWrapper.dsconStopMeasurement(dsconInstance);

        foreach (IntPtr channel in channels)
            DSRemoteWrapper.dsconFreeChannelInstance(channel);

        DSRemoteWrapper.dsconDisconnect(dsconInstance);
        DSRemoteWrapper.dsconDestroyInstance(dsconInstance);
    }

    protected static void CancelEventHandler (object sender, ConsoleCancelEventArgs e)
    {
        e.Cancel = true;
        MainClass.keepRunning = false;
    }

}