using System;
using Microsoft.SPOT;

namespace CPF
{
    class Utils
    {

        public static long MillisecondsSinceLastReboot()
        {
            return (Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks / System.TimeSpan.TicksPerMillisecond);
        }

        public static bool Match(byte[] s, int length, byte[] match, ref int pos)
        {
            do 
            {
                if (pos >= match.Length)
                    return true;
                if (pos >= length)
                    return false;
            } while (s[pos] == match[pos++]);
            return false;
        }

        public static bool ByteArrayCompare(byte[] a1, byte[] a2, int length)
        {
            for (int i = 0; i < length; i++)
                if (a1[i] != a2[i])
                    return false;

            return true;
        }

        public static string StringFromBytes(byte[] bytes, int length)
        {
            string s;
            if (length != 0)
            {
                char[] chars = new char[length];
                for (int i = 0; i < length; ++i)
                {
                    chars[i] = (char) bytes[i];
                }
                s = new string(chars);
            }
            else
            {
                s = "";
            }
            return s;
        }

        public static string StringFromByteArray(byte[] bytes)
        {
            int pos = 0;
            string s;
            char[] chars = new char[bytes.Length];
            while (bytes[pos] != 0)
                chars[pos++] = (char)bytes[pos];
            if (pos > 0)
                s = new string(chars);
            else
                s = "";
            return s;
        }

        public static string StringFromByte(byte ch)
        {
            string s;

            if (ch == IridiumStrings.CR[0])
                return "CR";
            else if (ch == IridiumStrings.LF[0])
                return "LF";
            else
            {
                char[] chars = new char[1];
                chars[0] = (char)ch;
                s = new string(chars);
                return s;
            }
        }
    }
}
