using System;
using Microsoft.SPOT;
using CPF;

public class EventQueueOverflowException : System.Exception
{    
    public EventQueueOverflowException() : base() { }
    public EventQueueOverflowException(string message) : base(message) { }
    public EventQueueOverflowException(string message, System.Exception inner) : base(message, inner) { }
}

public class EventQueueEmptyException : System.Exception
{
    public EventQueueEmptyException() : base() { }
    public EventQueueEmptyException(string message) : base(message) { }
    public EventQueueEmptyException(string message, System.Exception inner) : base(message, inner) { }
}

public class EventQueue
{
    protected   Event[]     _events;
    private     int         _head;
    private     int         _tail;

    public EventQueue(int capacity) 
    {
        // validate capacity
        if (capacity <= 0) 
            throw new ArgumentException("Must be greater than zero", "capacity");
        // set capacity and init the cache
        Capacity = capacity;
        _events = new Event[capacity];
        for (int i = 0; i < capacity; i++ )
            _events[i] = new Event();
        Clear();
    }

    public void Clear()
    {
        Count = 0;
        _head = 0;
        _tail = 0;
    }

    public int Capacity { get; private set; }

    public int Count { get; private set; }

    public bool Empty()
    {
        return (Count == 0) ? true : false;
    }

    public bool Full()
    {
        return (Count == Capacity) ? true : false;
    }
      
    public void Insert(Signal s, double dval, int ival) 
    {
        if (!Full())
        {
            _events[_tail]._signal = s;
            _events[_tail]._double = dval;
            _events[_tail]._integer = ival;
            _tail = (_tail + 1) % _events.Length;
            Count++;
        } 
        else
        {
            throw new EventQueueOverflowException();
        }
    }

    public Event Delete()
    {
        if (!Empty())
        {
            Event e = _events[_head];
            _head = (_head + 1) % _events.Length;
            Count--;
            return e;
        }
        else
        {
            throw new EventQueueEmptyException();
        }
    }
}


