using System;
using Microsoft.SPOT;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using CPF;

using System.Text;
using System.Threading;
using HWModules;
using SWModules;

using System.IO.Ports;

namespace CPFUnitTests
{
    [TestClass]
    public class SBDUnitTests
    {
        private static MessageQueue messageQ = MessageQueue.Instance;
        private static BTConsole btConsole = BTConsole.Instance;
        private static SerialPort modemPort = new SerialPort("COM3", 19200, Parity.None, 8, StopBits.One);
        private static NAL_A3LAR modem = new NAL_A3LAR();

        [TestMethod]
        public void TestSBD()
        {
            HWModules.MotherBoard motherboard = MotherBoard.Instance;
            motherboard.EnableChannelPower((int)MotherBoard.ChannelNames.GPSChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.IridiumChannel);
            
            Thread.Sleep(2000);

            btConsole.powerUp();
            btConsole.init();
            modemPort.Open();
            modemPort.DataReceived += dataRXHandler;

            modemPort.DiscardInBuffer();
            modemPort.DiscardOutBuffer();

            GPS1 gps = GPS1.Instance;

            byte[] atD0 = Encoding.UTF8.GetBytes("AT&D0\r");
            Debug.Print("Sending D0");
            modemPort.Write(atD0, 0, atD0.Length);
            Thread.Sleep(1000);

            byte[] atK0 = Encoding.UTF8.GetBytes("AT&K0\r");
            Debug.Print("Sending K0");
            modemPort.Write(atK0, 0, atK0.Length);
            Thread.Sleep(1000);

            byte[] atSBDWT = Encoding.UTF8.GetBytes("AT+SBDWT\r");
            Debug.Print("Sending SBDWT");
            modemPort.Write(atSBDWT, 0, atSBDWT.Length);
            Thread.Sleep(1000);

            sbTemp.Clear();
            sbTemp.Append("2018-07-10T23:59:36Z,GPS TOF,000114.00Lat,0000.00000 NLon,00000.00000 ENum Sats,0,NAK SBD,SQ,-1,MO Stat,-,,Mission TO,24:00:00,Park Time,01:00:00,Park P,100,Prof Num,-1 of 10,Max P,0.000,RecMode,True,BBVolts,0.000,Can P,1010.193,Can Hum,51.034,,..,\r");
            Debug.Print("Loading Message into modem buffer");
            modemPort.Write(UTF8Encoding.UTF8.GetBytes(sbTemp.ToString()), 0, sbTemp.Length);
            Thread.Sleep(1000);

            for (int i = 0; i < 3; i++)
            {
                byte[] atSBDIX = Encoding.UTF8.GetBytes("AT+SBDIX\r");
                Debug.Print("Sending SBDIX");
                modemPort.Write(atSBDIX, 0, atSBDIX.Length);
                Thread.Sleep(5000);
            }
        }

        [TestMethod]
        public void TestCreateSBDMessage()
        {
            Debug.Print("Running TestCreateSBDMessage");
            
            GPS1 gps1 = GPS1.Instance;

            HWModules.MotherBoard motherboard = MotherBoard.Instance;
            motherboard.EnableChannelPower((int)MotherBoard.ChannelNames.GPSChannel);
            motherboard.EnableChannelPower(MotherBoard.ChannelNames.IridiumChannel);
            
            Thread.Sleep(2000);

            btConsole.powerUp();
            btConsole.init();

            GPS1.GPSPosition gpsPos = new GPS1.GPSPosition();

            byte[] pubxMsg = Encoding.UTF8.GetBytes("PUBX,00,230731.00,3648.15464,N,12147.27260,W,-58.909,NF,277,52,13.304,127.46,0.023,,99.99,23.91,31.45,4,0,0*28");
            gps1.parsePUBX00(pubxMsg, ref gpsPos);

        }



        private static StringBuilder sbTemp = new StringBuilder(256);
        private static byte[] portBytes = new byte[256];
        private static int portBytesIndex = 0;

        private void dataRXHandler(object sender, SerialDataReceivedEventArgs e)
        {
            int numBytesAtPort = modemPort.BytesToRead;

            do
            {
                for (int i = 0; i < numBytesAtPort; i++)
                {
                    portBytes[portBytesIndex] = (byte)modemPort.ReadByte();
                    if ( (portBytes[portBytesIndex] == 13) && (portBytes.Length > 0) )
                    {
                        sbTemp.Clear();
                        sbTemp.Append(DateTime.Now);
                        sbTemp.Append("->");
                        sbTemp.Append(SafeEncoding.GetChars(portBytes, 0, portBytesIndex));
                        sbTemp.Append("<-");
                        if (sbTemp.Length > 5)
                            Debug.Print(sbTemp.ToString());
                        
                        Array.Clear(portBytes, 0, portBytes.Length);
                        portBytesIndex = 0;
                    }
                    else
                        portBytesIndex++;
                }
                numBytesAtPort = modemPort.BytesToRead;
            } while (numBytesAtPort > 0);
        }
    }
}