using System;

using Microsoft.SPOT;

namespace SWModules
{

    public sealed class MessageQueue : FIFOQueue
    {
        private const int depth = 128;
        public const int width = 512;

        private MessageQueue()
            : base(depth, width)
        {
        }


        private static MessageQueue _instance;

        public static MessageQueue Instance
        {
            get
            {
                _instance = _instance ?? (new MessageQueue());
                return _instance;
            }
        }


        private QRecord _qRecord;

        private const int MaxQueueProcess = 64;

        /// <summary>
        /// 
        /// </summary>
        public override void processQ(TimeSpan startTime, TimeSpan cyclePeriod)
        {

            for (byte i = 0; i < MaxQueueProcess; i++)
            {
                //We're out of messages
                if (Count <= 0) break;

                //We're out of time
                if (Microsoft.SPOT.Hardware.Utility.GetMachineTime() - startTime > cyclePeriod)
                {
                    Microsoft.SPOT.Debug.Print("PROCESS Q out of time: " + Count.ToString());
                    break;
                }
               
                _qRecord = Dequeue();
                _qRecord.processMessage(_qRecord);
            }
        }

        public override void processQ()
        {
            processQ(MaxQueueProcess);            
        }

        private byte _i;
        public override void processQ(int maxMessages)
        {
            if (Count == 0) return;

            if (maxMessages > 100)
                maxMessages = 100;
            if (maxMessages < 1)
                maxMessages = 1;
            
            for (_i = 0; _i < maxMessages; _i++)
            {
                //We're out of messages
                if (Count <= 0)
                {
                    break;
                }

                _qRecord = Dequeue();
                _qRecord.processMessage(_qRecord);
            }
        }

       
        public void processQMaxLimit()
        {
            processQ(100);
        }
    }

    public sealed class FileQueue : FIFOQueue
    {

        private const int depth = 10;
        private const int width = 64;


        public FileQueue() :
            base(depth, width)
        {

        }

        public override void processQ()
        {
            throw new System.NotImplementedException();
        }

        public override void processQ(TimeSpan startTime, TimeSpan period)
        {
            throw new System.NotImplementedException();
        }

        public override void processQ(int maxMessages)
        {
            throw new System.NotImplementedException();
        }

    }
}