using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace ImageBlockz.Core
{
    /// <summary>
    /// Implements methods and history list of <see cref="Bitmap"/> for "Undo" functionality. 
    /// </summary>
    public class History
    {
        private List<Bitmap> history = new List<Bitmap> { };

        /// <summary>
        /// Add <see cref="Bitmap"/> to the history list of bitmaps. 
        /// </summary>
        /// <param name="bmp">Input <see cref="Bitmap"/></param>
        public void Add(Bitmap bmp)
        {
            history.Add(bmp);
            OverflowAvoid();
        }

        /// <summary>
        /// Avoid against overflow the list. Count of items in the history list is limited. Oldest items are discarded.
        /// </summary>
        private void OverflowAvoid()
        {
            while (history.Count > 9)
            {
                history.RemoveAt(0);
            }
        }

        /// <summary>
        /// Clear all items in history.
        /// </summary>
        public void Clear()
        {
            history.Clear();
        }

        /// <summary>
        /// Get items count in history list.
        /// </summary>
        /// <returns></returns>
        public int Counter()
        {
            int count = history.Count();
            return count;
        }
        
        /// <summary>
        /// Undo method discard current bitmap in the history list and return new current bitmap.
        /// </summary>
        /// <returns>New current bitmap.</returns>
        public Bitmap Undo()
        {
            history.RemoveAt(history.Count-1);
            Bitmap bmp = history[history.Count - 1];

            return bmp;
        }

        /// <summary>
        /// Get current <see cref="Bitmap"/> from the list.
        /// </summary>
        /// <returns>Current Bitmap from the history list.</returns>
        public Bitmap Current()
        {
            Bitmap bmp = history[history.Count - 1];

            return bmp;
        }
    }
}
