﻿using System;
using System.Collections.Generic;
using Db1K.Collections;

namespace Db1K.PriorityQueueBenchmark.Naive
{
    public class NaivePriorityQueue<T> : IPriorityQueue<T, double> where T : IComparable<T>, IPriorityQueueNode<double>
    {
        private readonly List<T> _queue = new List<T>();

        public void Clear()
        {
            _queue.Clear();
        }

        public void Enqueue(T item)
        {
            _queue.Add(item);
            _queue.Sort();
        }

        public T Dequeue()
        {
            T item = _queue[0];
            _queue.RemoveAt(0);
            return item;
        }

        public bool Remove(T item)
        {
            var removed = _queue.Remove(item);
            if (removed)
            {
                _queue.Sort();
            }

            return removed;
        }

        public void UpdatePriority(T item, double priority)
        {
            if (_queue.Contains(item))
            {
                item.Priority = priority;
                _queue.Sort();
            }
        }

        public T Peek()
        {
            T item = _queue[0];
            return item;
        }

        public int Length
        {
            get { return _queue.Count; }
        }
    }
}
