// CheckedMenuRibbonUICommand.cs
// Copyright (c) 2016 Bentley Systems, Incorporated. All Rights Reserved.

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using Telerik.Windows.Controls;

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// A ribbon UI command that will keep checked menus that are linked to the command in sync in terms of their IsChecked value.
    /// </summary>
    public class CheckedMenuRibbonUICommand : RibbonUICommand
    {
        #region Constructors
        /// <summary>
        /// Instantiates a new CheckedMenuRibbonUICommand.
        /// </summary>
        /// <param name="command">The associated ICommand.</param>
        /// <param name="name">The *unique* name of the ribbon UI command.</param>
        /// <param name="labelKey">The resource key used to look up the ribbon UI command label from TextManager.</param>
        /// <param name="toolTipTitleKey">The resource key used to look up the ribbon UI command label from TextManager.</param>
        /// <param name="toolTipDescriptionKey">The resource key used to look up the ribbon UI command label from TextManager.</param>
        /// <param name="graphicResourceKey">The graphic resource key for the regular (small) image size.</param>
        public CheckedMenuRibbonUICommand(ICommand command, string name, string labelKey, string toolTipTitleKey, string toolTipDescriptionKey,
            string graphicResourceKey)
            : base(command, name, labelKey, toolTipTitleKey, toolTipDescriptionKey, graphicResourceKey, String.Empty)
        {
        }

        /// <summary>
        /// Instantiates a new CheckedMenuRibbonUICommand.
        /// </summary>
        /// <param name="command">The associated ICommand.</param>
        /// <param name="name">The *unique* name of the ribbon UI command.</param>
        /// <param name="labelKey">The resource key used to look up the ribbon UI command label from TextManager.</param>
        /// <param name="toolTipTitleKey">The resource key used to look up the ribbon UI command label from TextManager.</param>
        /// <param name="toolTipDescriptionKey">The resource key used to look up the ribbon UI command label from TextManager.</param>
        /// <param name="graphicResourceKey">The graphic resource key for the regular (small) image size.</param>
        /// <param name="largeGraphicResourceKey">The graphic resource key for the large image size.</param>
        public CheckedMenuRibbonUICommand(ICommand command, string name, string labelKey, string toolTipTitleKey, string toolTipDescriptionKey,
            string graphicResourceKey, string largeGraphicResourceKey)
            : base(command, name, labelKey, toolTipTitleKey, toolTipDescriptionKey, graphicResourceKey, largeGraphicResourceKey, RibbonHotKey.None)
        {
        }

        /// <summary>
        /// Instantiates a new CheckedMenuRibbonUICommand.
        /// </summary>
        /// <param name="command">The associated ICommand.</param>
        /// <param name="name">The *unique* name of the ribbon UI command.</param>
        /// <param name="labelKey">The resource key used to look up the ribbon UI command label from TextManager.</param>
        /// <param name="toolTipTitleKey">The resource key used to look up the ribbon UI command label from TextManager.</param>
        /// <param name="toolTipDescriptionKey">The resource key used to look up the ribbon UI command label from TextManager.</param>
        /// <param name="graphicResourceKey">The graphic resource key for the regular (small) image size.</param>
        /// <param name="largeGraphicResourceKey">The graphic resource key for the large image size.</param>
        /// <param name="hotKey">The ribbon hot key to use for the command.</param>
        public CheckedMenuRibbonUICommand(ICommand command, string name, string labelKey, string toolTipTitleKey, string toolTipDescriptionKey,
            string graphicResourceKey, string largeGraphicResourceKey, RibbonHotKey hotKey) : 
            base(command, name, labelKey, toolTipTitleKey, toolTipDescriptionKey, graphicResourceKey, largeGraphicResourceKey, hotKey)
        {
        }
        #endregion

        #region Public Methods
        public void SetIsChecked(bool isChecked)
        {
            IList<FrameworkElement> checkedMenus = GetLinkedElements(RibbonUICommandLinkedElementTypes.CheckedMenus);
            foreach (RadMenuItem menuItem in checkedMenus)
                menuItem.IsChecked = isChecked;
        }
        #endregion

        #region Protected Override Methods
        protected override void AfterExecute()
        {
            IList<FrameworkElement> checkedMenus = GetLinkedElements(RibbonUICommandLinkedElementTypes.CheckedMenus);
            if (checkedMenus.Count > 0)
            {
                RadMenuItem menuItem = ((RadMenuItem)checkedMenus[0]);
                SetIsChecked(!menuItem.IsChecked);
            }
        }
        #endregion
    }
}
