using skyscraper5.Skyscraper.IO.CrazycatStreamReader;
using skyscraper5.Skyscraper.IO.RemoteStreamReader;
using System.Net;

class Program
{
    public static void Main(string[] args)
    {
        new Program().Run();
    }

    private int? dvbDeviceToUse;
    private void CheckForDvbCallback(int index, string name, STD_TYPE type)
    {
        if (type == STD_TYPE.STD_DVBS || type == STD_TYPE.STD_DVBS2)
        {
            dvbDeviceToUse = index;
        }
    }

    private void Run()
    {
        // See https://aka.ms/new-console-template for more information
        RemoteStreamReaderClient client = new RemoteStreamReaderClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6970));

        if (!client.CheckForDVBExEx(CheckForDvbCallback))
        {
            Console.WriteLine("no devices");
            client.Dispose();
            return;
        }
        if (!client.StartDvbEx(dvbDeviceToUse.Value))
        {
            Console.WriteLine("failed to start");
            client.Dispose();
            return;
        }

        if (!client.SetChannel(11221000, 30000000, 1, VITERBIRATE_TYPE.VR_5_6, 9750000, 106000000, 117000000))
        {
            Console.WriteLine("set channel failed");
            client.Dispose();
            return;
        }

        SearchResult searchResult = new SearchResult();
        if (!client.SignalInfo(ref searchResult))
        {
            Console.WriteLine("signal info failed");
            client.Dispose();
            return;
        }

        bool pPresent = true, pLock = true;
        int pRFLevel = Int32.MaxValue;
        float pSNR = float.NaN;
        float pBER = float.NaN;
        sbyte[] iq = new sbyte[8];

        char[][] buf = new char[Console.WindowWidth][];
        for (int i = 0; i < buf.Length; i++)
        {
            buf[i] = new char[Console.WindowHeight];
            Array.Fill(buf[i], (char)0x21);
        }

        for (int t = 0; t < 9001; t++)
        {
            if (!client.GetSignalExEx(ref pPresent, ref pLock, ref pRFLevel, ref pSNR, ref pBER))
            {
                Console.WriteLine("get signal ex ex failed");
                client.Dispose();
                return;
            }
            if (!client.IQScan(0, iq, 4))
            {
                Console.WriteLine("iq scan failed");
                client.Dispose();
                return;
            }

            Console.CursorLeft = 0;
            Console.CursorTop = 0;
            Console.Write("RF = {0}, SNR = {1}, BER = {2}   ", pRFLevel, pSNR, pBER);

            for (int i = 0; i < iq.Length; i += 2)
            {
                double x = iq[i + 0] + sbyte.MaxValue;
                double y = iq[i + 1] + sbyte.MaxValue;
                double xScale = (double)255 / (double)Console.WindowWidth;
                double yScale = (double)255 / (double)Console.WindowHeight;

                x /= xScale;
                y /= yScale;

                int finalX = (int)x;
                int finalY = (int)y;

                Console.CursorTop = (int)y;
                Console.CursorLeft = (int)x;
                Console.Write(buf[finalX][finalY]++);
            }
            Thread.Sleep(100);
        }


        client.StopDVB();
        client.Dispose();

    }
}
