using System;
using Microsoft.SPOT;

using System.Text;

namespace CPF
{
    public static class SBQue
    {
        private const int defaultCapacity = 512;
        private const int defaultMessageLength = 256;

        private static StringBuilder[] que = new StringBuilder[defaultCapacity];
        private static StringBuilder sbTemp = new StringBuilder(defaultMessageLength);

        private static int i = 0;
        private static int head = 0;
        private static int tail = 0;
        private static int count = 0;
        private static int capacity = defaultCapacity;
  
        public static void init()
        {
            for (i = 0; i < 512; i++)
                que[i] = new StringBuilder(defaultMessageLength);
        }

        public static void Enqueue(StringBuilder sbIn)
        {
             if (count >= capacity)
            {
                throw new InvalidOperationException("Queue is full");
            }
            else
            {
                que[head].Clear();
                que[head].Append(sbIn);
                head = (head + 1) % capacity;
                count++;
            }
        }

        public static StringBuilder Dequeue()
        {
            if (count == 0)
            {
                throw new InvalidOperationException("Queue is empty");
            }
            else
            {
                sbTemp.Clear();
                sbTemp.Append(que[tail]);
                tail = (tail + 1) % capacity;
                count--;
                return (sbTemp);
            }
        }

        public static int Count
        {
            get { return count; }
        }

        public static void Clear()
        {
            count = 0;
            tail = head;
        }
    }
}
