//From https://www.techiedelight.com/queue-implementation-cpp/
#include <string>
using namespace std;

#include "FIFO.hpp"

// Utility function to remove front element from the queue
int32_t FIFO::dequeue(string &outTimestamp, string &deQString)
{
	// check for queue underflow
	if(isEmpty())
	{
		cout << "\t\t" << qName << " FIFO empty" << endl;
		return (-1);
	}

	outTimestamp = fifoTimestamp[head];
	deQString = (char*)fifoArray[head];
	cout << "\t\tFIFO count = " << count << "  Dequeued:  " << outTimestamp << "  " << deQString << endl;

	head = (head + 1) % capacity;
	count--;
	return (count);
}

// Utility function to add an item to the queue
void FIFO::enqueue(string inTimestamp, uint8_t inMsg[256])
{
	// check for queue overflow
	if(isFull())
	{
		cout << "\t\tOverFlow\nProgram Terminated\n";
		exit(EXIT_FAILURE);
	}

	tail = (tail + 1) % capacity;
	
	fifoTimestamp[tail] = inTimestamp;
	for(int i = 0; i<256; i++)
		fifoArray[tail][i] = inMsg[i];

	cout << "\t\tFIFO count: " << count << "  Enqueued " << fifoArray[tail] << endl;
	
	count++;
}

// Utility function to return front element in the queue
//int FIFO::peek()
//{
//	if (isEmpty())
//	{
//		cout << "UnderFlow\nProgram Terminated\n";
//		exit(EXIT_FAILURE);
//	}
//	return arr[front];
//}

// Utility function to return the size of the queue
int FIFO::getCount()
{
	return count;
}

// Utility function to check if the queue is empty or not
bool FIFO::isEmpty()
{
	return (getCount() <= 0);
}

// Utility function to check if the queue is full or not
bool FIFO::isFull()
{
	return (getCount() == capacity);
}
