using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ImageBlockz.Gui
{
    /// <summary>
    /// Class provides generating of menu paths in Main Window and SequenceGui.
    /// </summary>
    class BuildMenuPath
    {
        /// <summary>
        /// Build path along to string path parameter and ID of operation.
        /// </summary>
        /// <param name="path">Path in menu.</param>
        /// <param name="operationId">ID of operation.</param>
        /// <param name="menuStrip"></param>
        /// <returns><see cref="OperationMenuItem"/></returns>
        public static OperationMenuItem BuildPath(string path, string operationId, ToolStrip menuStrip)
        {
            OperationMenuItem operationItem = new OperationMenuItem(operationId);

            List<string> words = new List<string>(path.Split(new char[] { '/' }));
            
            string operation = words.Last();
            words.Remove(operation);

            ToolStripItemCollection items = menuStrip.Items;

            foreach (string word in words)
            {
                bool itemExists = false;

                foreach (ToolStripItem item in items)
                {
                    if (word == item.Text)
                    {
                        items = ((ToolStripMenuItem)item).DropDownItems;
                        itemExists = true;
                        break;
                    }
                }

                if (itemExists == false)
                {
                    ToolStripMenuItem newItem = new ToolStripMenuItem(word);
                    items.Add(newItem);

                    items = newItem.DropDownItems;
                }
            }

            operationItem.Text = operation; 

            items.Add(operationItem);

            return operationItem;
        }
    }
}
