using System;
using Microsoft.SPOT;

using System.Text;
//using SWModules;

namespace GPSParser
{
    public class Program
    {
        public struct GPSPosition
        {
            public StringBuilder latSB;
            public StringBuilder lonSB;
            public int numSats;
            public StringBuilder gpsTimeOfFix;
            public DateTime gpsDateOfFix;
            public TimeSpan gpsTTFF;
            public StringBuilder rawMessage;
        } public GPSPosition latestPosition = new GPSPosition();

        public static void Main()
        {
            GPSPosition gpsPosition = new GPSPosition();

            StringBuilder pubxMsg = new StringBuilder("UBX,00,150208.00,3646.21831,N,12152.28182,W,0.000,D3,2.6,4.6,2.582,208.67,-0.041,,0.78,1.18,0.74,12,0,0*6A");
            parsePUBX00(UTF8Encoding.UTF8.GetBytes(pubxMsg.ToString()), ref gpsPosition);
        }

        static public void parsePUBX00(byte[] inBytes, ref GPSPosition position)
        {
            byte[] latBytes = new byte[16];
            byte[] lonBytes = new byte[16];
            byte[] numSatsBytes = new byte[16];


            byte[] fieldBytes = new byte[16];
            byte[] decMinBytes = new byte[16];
            byte[] degMinBytes = new byte[16];

            StringBuilder positionSB = new StringBuilder(64);
            try
            {
                Array.Clear(fieldBytes, 0, fieldBytes.Length);
                Utils.getField(inBytes, comma, 2, ref fieldBytes);
                position.gpsTimeOfFix.Clear();
                position.gpsTimeOfFix.Append(SafeEncoding.GetChars(fieldBytes));
                position.gpsDateOfFix = DateTime.Now;

                Array.Clear(fieldBytes, 0, fieldBytes.Length);
                Utils.getField(inBytes, comma, 3, ref fieldBytes);
                position.latSB.Clear();
                position.latSB.Append(SafeEncoding.GetChars(fieldBytes));

                sbTemp.Clear();
                sbTemp.Append(fieldBytes);
                position.latSB.Append(Literals.spaceChar);

                Array.Clear(fieldBytes, 0, fieldBytes.Length);
                Utils.getField(inBytes, comma, 4, ref fieldBytes);
                position.latSB.Append(SafeEncoding.GetChars(fieldBytes));

                Array.Clear(fieldBytes, 0, fieldBytes.Length);
                Utils.getField(inBytes, comma, 5, ref fieldBytes);
                position.lonSB.Clear();
                position.lonSB.Append(SafeEncoding.GetChars(fieldBytes));
                position.lonSB.Append(Literals.spaceChar);

                Array.Clear(fieldBytes, 0, fieldBytes.Length);
                Utils.getField(inBytes, comma, 4, ref fieldBytes);
                position.latSB.Append(SafeEncoding.GetChars(fieldBytes));

                Array.Clear(fieldBytes, 0, fieldBytes.Length);
                Utils.getField(inBytes, comma, 6, ref fieldBytes);
                position.lonSB.Append(SafeEncoding.GetChars(fieldBytes));

                Array.Clear(fieldBytes, 0, fieldBytes.Length);
                Utils.getField(inBytes, comma, 18, ref fieldBytes);
                position.numSats = bytesToInt32(fieldBytes);

                position.rawMessage.Clear();
                position.rawMessage.Append(SafeEncoding.GetChars(inBytes, 0, latestPosition.rawMessage.Capacity));
            }
            catch
            {
                EngrLogger.writeToColumns("[ERROR] parsing GPS PUBX00 message");
                position.latSB.Clear();
                position.latSB.Append(Literals.nanString);
                position.lonSB.Clear();
                position.lonSB.Append(Literals.nanString);
                position.numSats = -1;

                position.rawMessage.Clear();
                position.rawMessage.Append(SafeEncoding.GetChars(inBytes, 0, latestPosition.rawMessage.Capacity));
            }
        }

    }
}
