using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using Haestad.Framework.Application;
using Haestad.Framework.UI.Integration;
using Haestad.Framework.Windows.Forms.Support;
using Haestad.Framework.Windows.Forms.Resources;
using Haestad.Support.Library;
using Haestad.Support.Storage;
using Haestad.Support.Support;
using Telerik.Windows;
using Telerik.Windows.Controls;

namespace Haestad.WPFRibbonPrototype
{
    public partial class ParentForm : Form, IRibbonCallbacks
    {
        public ParentForm()
        {
            InitializeComponent();

            // Note: <whar> To force loading of H.F.W.F. so that icon resources can be loaded.
            HtmlHelp htmlHelp = new HtmlHelp("");

            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            InitializeRibbon();
            InitializeParentFormExtender();
            InitializeEvents();
        }

        #region Event Handlers
        private void ParentForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if (OnParentFormClosing != null && !e.Cancel)
            //    OnParentFormClosing();
        }

        private void RibbonServices_RibbonTabChanged(object sender, TabChangeEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Tab change. Previous tab: " + e.PreviousTabLabel + ", new tab: " + e.NewTabLabel);
        }
        #endregion

        #region IRibbonCallbacks Members
        //public void SetParentFormClosingDelegate(OnParentFormClosingDelegate onParentFormClosing)
        //{
        //    OnParentFormClosing = onParentFormClosing;
        //}

        public IntPtr ParentFormHandle { get { return Handle; } }

        public bool IsParentFormInLockDown { get { return false; } }

        public System.Windows.Input.ICommand GetProductHelpCommand()
        {
            return CommandFactory.GetUICommand(StandardTestCommandNames.HelpCommand);
        }

        public Icon GetIcon(string standardGraphicResourceName)
        {
            return (Icon)GraphicResourceManager.Current[standardGraphicResourceName];
        }

        public IList<IStorageElement> GetRibbonStorageElements()
        {
            IList<IStorageElement> ribbonStorageElements = new List<IStorageElement>();
            ribbonStorageElements.Add(QATButtonManager);

            //
            // TODO 1: <whar> Add other managers as we support additional ribbon customization features.
            //

            return ribbonStorageElements;
        }

        public IStorageWriteContext GetStorageWriteContext()
        {
            return new StorageWriteContext((int)HmiProductRelease.WaterGEMS, new Version(1, 0, 0, 0), String.Empty);
        }

        public string LocalUserSettingsDirectory
        {
            get { return @"C:\Users\Public\"; }
        }

        public IRibbonUICommand GetUICommand(string commandName)
        {
            return CommandFactory.GetUICommand(commandName);
        }

        public ApplicationEventChannel ApplicationEventChannel
        {
            get
            {
                return null;
            }
        }

        public void LayoutRibbon()
        {
            LayoutQuickAccess();

            LayoutBackstage();

            RibbonBuilder.AddTabToRibbonBar(StandardTabNames.RegularButtons, StandardTabLabels.RegularButtons);
            RibbonBuilder.AddTabToRibbonBar(StandardTabNames.ToggleButtons, StandardTabLabels.ToggleButtons);

            LayoutTabRegularButtons();
            LayoutTabToggleButtons();

            RibbonBuilder.AddTabToRibbonBar(StandardTabNames.DropDownButtons, StandardTabLabels.DropDownButtons);
            RibbonBuilder.AddTabToRibbonBar(StandardTabNames.DropDownOnlyButtons, StandardTabLabels.DropDownOnlyButtons);
            RibbonBuilder.AddTabToRibbonBar(StandardTabNames.DropDownToggleButtons, StandardTabLabels.DropDownToggleButtons);

            LayoutTabDropDownButtons();
            LayoutTabDropDownOnlyButtons();
            LayoutTabDropDownToggleButtons();

            LayoutDropDownMenus(RibbonUICommandLinkedElementTypes.DropDownButtons);
            LayoutDropDownMenus(RibbonUICommandLinkedElementTypes.DropDownOnlyButtons);
            LayoutDropDownMenus(RibbonUICommandLinkedElementTypes.DropDownToggleButtons);

            SetADefaultToggleMenuItem();

            RibbonBuilder.AddTabToRibbonBar(StandardTabNames.HorizontalButtonLayout, StandardTabLabels.HorizontalButtonLayout);

            LayoutHorizontalButtons();

            RibbonBuilder.AddTabToRibbonBar(StandardTabNames.CheckedMenuSync, StandardTabLabels.CheckedMenuSync);

            LayoutCheckedMenus();

            RibbonBuilder.AddTabToRibbonBar(StandardTabNames.FlyOutMenus, StandardTabLabels.FlyOutMenus);

            LayoutFlyOutMenus();

            RibbonBuilder.AddTabToRibbonBar(StandardTabNames.ToggleButtonSync, StandardTabLabels.ToggleButtonSync);

            LayoutToggleButtons();

            LayoutTopTabStripControls();
        }

        #endregion

        private void InitializeParentFormExtender()
        {
            m_ribbonParentFormExtender = new RibbonParentFormExtender(this,
                RibbonHost,
                GetIcon(StandardGraphicResourceNames.Bentley32));
        }

        private void InitializeRibbon()
        {
            SuspendLayout();

            StatusStrip statusStrip = new StatusStrip();
            statusStrip.Items.Add(new ToolStripStatusLabel { Spring = true });

            RibbonHost.Name = "ribbonHost";
            Controls.Add(statusStrip);
            Controls.Add(RibbonHost);

            ResumeLayout(true);
        }

        private void InitializeEvents()
        {
            FormClosing += ParentForm_FormClosing;

            RibbonServices.RibbonTabChanged += RibbonServices_RibbonTabChanged;
        }

        #region Ribbon Layout
        private void LayoutTopTabStripControls()
        {
            AddTopTabStripSignInButton();
            AddTopTabStripSignInButtonTextVersion();
            //AddTopTabStripCombo(); // The real earch combo now added automatically.
            AddConnectAdvisorButton();
        }

        private void AddTopTabStripSignInButton()
        {
            IRibbonUICommand signInCommand = CommandFactory.GetUICommand(StandardTestCommandNames.SignInCommand);
            System.Windows.Controls.Control button = RibbonBuilder.AddButtonToTopTabStrip(signInCommand, ButtonSize.Small, ButtonStyle.Regular, StandardButtonNames.ImageSignInButton);
            button.Visibility = Visibility.Collapsed;
        }

        private void AddTopTabStripSignInButtonTextVersion()
        {
            IRibbonUICommand signInCommand = CommandFactory.GetUICommand(StandardTestCommandNames.SignInCommand);
            RibbonBuilder.AddButtonToTopTabStrip(signInCommand, ButtonSize.Medium, ButtonStyle.RegularTextOnly, StandardButtonNames.TextSignInButton);
        }

        private void AddConnectAdvisorButton()
        {
            IRibbonUICommand connectAdvisorCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ConnectAdvisorCommand);
            System.Windows.Controls.Control button = RibbonBuilder.AddButtonToTopTabStrip(connectAdvisorCommand, ButtonSize.Small, ButtonStyle.Regular, StandardButtonNames.ConnectAdvisorButton, true);
        }

        private void AddTopTabStripCombo()
        {
            double width = 180.0;
            double height = Double.NaN; // Automatic
            double dropdownWidth = 180.0;
            double maximumDropDownHeight = 500.0;
            double fontSize = 11.0;

            RibbonBuilder.AddComboBoxToTopTabStrip("searchCombo", "Ribbon Search", "Search for ribbon commands and your missing stash.",
                width, height, dropdownWidth, maximumDropDownHeight, fontSize);

            RadComboBox radComboBox = RibbonBuilder.GetComboBox("searchCombo");

            radComboBox.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            radComboBox.IsTextSearchEnabled = true;
            radComboBox.TextSearchMode = TextSearchMode.Contains;
            radComboBox.IsEditable = true;
        }

        private void LayoutQuickAccess()
        {
            foreach (IRibbonUIButton ribbonUIButton in QATButtonManager.Elements())
                RibbonBuilder.AddQuickAccessToolbarButton(ribbonUIButton);
        }

        private void LayoutBackstage()
        {
            // Tabs ---

            IRibbonBackstageUICommand userControlWPFCommand = CommandFactory.GetUICommand(StandardTestCommandNames.BackstageUserControlWPF) as IRibbonBackstageUICommand;
            RibbonBuilder.AddBackStageItemTabType(userControlWPFCommand, false);

            IRibbonBackstageUICommand backstageDisabledTabCommand = CommandFactory.GetUICommand(StandardTestCommandNames.BackstageDisabledTab) as IRibbonBackstageUICommand;
            RadRibbonBackstageItem disabledTab = RibbonBuilder.AddBackStageItemTabType(backstageDisabledTabCommand, false);
            disabledTab.IsEnabled = false;

            // Buttons ---

            IRibbonUICommand backstageButtonCommand = CommandFactory.GetUICommand(StandardTestCommandNames.BackstageButton);
            RibbonBuilder.AddBackStageItemButtonType(backstageButtonCommand, false);

            IRibbonUICommand backstageDisabledButtonCommand = CommandFactory.GetUICommand(StandardTestCommandNames.BackstageDisabledButton);
            RadRibbonBackstageItem disabledButton = RibbonBuilder.AddBackStageItemButtonType(backstageDisabledButtonCommand, false);
            disabledButton.IsEnabled = false;
        }

        private void OnButtonClick(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            System.Windows.MessageBox.Show("Button click: " + element.Name);
        }

        private void LayoutTabRegularButtons()
        {
            LayoutTestButtons(
                StandardTabNames.RegularButtons,
                StandardRibbonGroupNames.RegularButtons_RG1,
                StandardRibbonGroupNames.RegularButtons_RG2,
                StandardRibbonGroupNames.RegularButtons_RG3,
                StandardButtonGroupNames.RegularButtons_RG1_BG1,
                StandardButtonGroupNames.RegularButtons_RG1_BG2,
                StandardButtonGroupNames.RegularButtons_RG2_BG1,
                StandardButtonGroupNames.RegularButtons_RG3_BG1,
                ButtonStyle.Regular,
                false);
        }

        private void LayoutTabToggleButtons()
        {
            LayoutTestButtons(
                StandardTabNames.ToggleButtons,
                StandardRibbonGroupNames.ToggleButtons_RG1,
                StandardRibbonGroupNames.ToggleButtons_RG2,
                StandardRibbonGroupNames.ToggleButtons_RG3,
                StandardButtonGroupNames.ToggleButtons_RG1_BG1,
                StandardButtonGroupNames.ToggleButtons_RG1_BG2,
                StandardButtonGroupNames.ToggleButtons_RG2_BG1,
                StandardButtonGroupNames.ToggleButtons_RG3_BG1,
                ButtonStyle.Toggle,
                true);
        }

        private void LayoutTabDropDownButtons()
        {
            LayoutTestButtons(
                StandardTabNames.DropDownButtons,
                StandardRibbonGroupNames.DropDownButtons_RG1,
                StandardRibbonGroupNames.DropDownButtons_RG2,
                StandardRibbonGroupNames.DropDownButtons_RG3,
                StandardButtonGroupNames.DropDownButtons_RG1_BG1,
                StandardButtonGroupNames.DropDownButtons_RG1_BG2,
                StandardButtonGroupNames.DropDownButtons_RG2_BG1,
                StandardButtonGroupNames.DropDownButtons_RG3_BG1,
                ButtonStyle.DropDown,
                false);
        }

        private void LayoutTabDropDownOnlyButtons()
        {
            LayoutTestButtons(
                StandardTabNames.DropDownOnlyButtons,
                StandardRibbonGroupNames.DropDownOnlyButtons_RG1,
                StandardRibbonGroupNames.DropDownOnlyButtons_RG2,
                StandardRibbonGroupNames.DropDownOnlyButtons_RG3,
                StandardButtonGroupNames.DropDownOnlyButtons_RG1_BG1,
                StandardButtonGroupNames.DropDownOnlyButtons_RG1_BG2,
                StandardButtonGroupNames.DropDownOnlyButtons_RG2_BG1,
                StandardButtonGroupNames.DropDownOnlyButtons_RG3_BG1,
                ButtonStyle.DropDownOnly,
                false);
        }

        private void LayoutTabDropDownToggleButtons()
        {
            LayoutTestButtons(
                StandardTabNames.DropDownToggleButtons,
                StandardRibbonGroupNames.DropDownToggleButtons_RG1,
                StandardRibbonGroupNames.DropDownToggleButtons_RG2,
                StandardRibbonGroupNames.DropDownToggleButtons_RG3,
                StandardButtonGroupNames.DropDownToggleButtons_RG1_BG1,
                StandardButtonGroupNames.DropDownToggleButtons_RG1_BG2,
                StandardButtonGroupNames.DropDownToggleButtons_RG2_BG1,
                StandardButtonGroupNames.DropDownToggleButtons_RG3_BG1,
                ButtonStyle.DropDownToggle,
                false);
        }

        private void SetADefaultToggleMenuItem()
        {
            // TODO 0: Fix up.

            //RibbonUICommand ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.EnableDisableCommand) as RibbonUICommand;
            //IList<FrameworkElement> dropDownToggleButtons = ribbonUICommand.GetLinkedElements(RibbonUICommandLinkedElementTypes.DropDownToggleButtons);
            //RadRibbonToggleSplitButton button = dropDownToggleButtons[0] as RadRibbonToggleSplitButton;

            //ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.MenuThreeCommand) as RibbonUICommand;
            //IList<FrameworkElement> dropDownToggleMenus = ribbonUICommand.GetLinkedElements(RibbonUICommandLinkedElementTypes.ToggleMenus);
            //RadToggleMenuItem menuItem = dropDownToggleMenus[0] as RadToggleMenuItem;

            //RibbonBuilder.SetDefaultMenuItemForDropDownToggleButton(button, menuItem);
        }

        private void LayoutHorizontalButtons()
        {
            RibbonBuilder.AddRibbonGroupToTab(StandardTabNames.HorizontalButtonLayout, StandardRibbonGroupNames.HorizontalButtonLayout_RG1, StandardRibbonGroupNames.HorizontalButtonLayout_RG1, StandardGraphicResourceNames.ControlOverrides);
            RibbonBuilder.AddWrapPanelToRibbonGroup(StandardRibbonGroupNames.HorizontalButtonLayout_RG1, StandardWrapPanelNames.HorizontalButtonLayout_WP1);
            RibbonBuilder.AddHorizontalButtonGroupToWrapPanel(StandardWrapPanelNames.HorizontalButtonLayout_WP1, StandardHorizontalButtonGroupNames.HorizontalButtonLayout_RG1_HBG1);

            IRibbonUICommand ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.OpenModelessFormCommand);
            RibbonBuilder.AddButtonToHorizontalButtonGroup(StandardHorizontalButtonGroupNames.HorizontalButtonLayout_RG1_HBG1, ribbonUICommand, ButtonSize.Small, ButtonStyle.Regular);
            RibbonBuilder.AddButtonToHorizontalButtonGroup(StandardHorizontalButtonGroupNames.HorizontalButtonLayout_RG1_HBG1, ribbonUICommand, ButtonSize.Small, ButtonStyle.Toggle);
            RibbonBuilder.AddButtonToHorizontalButtonGroup(StandardHorizontalButtonGroupNames.HorizontalButtonLayout_RG1_HBG1, ribbonUICommand, ButtonSize.Small, ButtonStyle.DropDown);

            ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.OpenModalFormCommand);
            RibbonBuilder.AddButtonToHorizontalButtonGroup(StandardHorizontalButtonGroupNames.HorizontalButtonLayout_RG1_HBG1, ribbonUICommand, ButtonSize.Small, ButtonStyle.Regular);
            RibbonBuilder.AddButtonToHorizontalButtonGroup(StandardHorizontalButtonGroupNames.HorizontalButtonLayout_RG1_HBG1, ribbonUICommand, ButtonSize.Small, ButtonStyle.Toggle);
            RibbonBuilder.AddButtonToHorizontalButtonGroup(StandardHorizontalButtonGroupNames.HorizontalButtonLayout_RG1_HBG1, ribbonUICommand, ButtonSize.Small, ButtonStyle.DropDown);
        }

        private void LayoutCheckedMenus()
        {
            RibbonBuilder.AddRibbonGroupToTab(StandardTabNames.CheckedMenuSync, StandardRibbonGroupNames.CheckedMenuSync_RG1, StandardRibbonGroupNames.CheckedMenuSync_RG1, StandardGraphicResourceNames.ControlOverrides);
            RibbonBuilder.AddButtonGroupToRibbonGroup(StandardRibbonGroupNames.CheckedMenuSync_RG1, StandardButtonGroupNames.CheckedMenuSync_RG1_BG1);

            IRibbonUICommand checkedMenuCommand = CommandFactory.GetUICommand(StandardTestCommandNames.CheckedMenuCommand);
            IRibbonUICommand nullCommand = CommandFactory.GetUICommand(StandardTestCommandNames.NullCommand);

            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.CheckedMenuSync_RG1_BG1, checkedMenuCommand, ButtonSize.Large, ButtonStyle.DropDown, StandardButtonNames.CheckedMenuButtonOne);
            RibbonBuilder.AddCheckedMenuItemToDropDownButton(StandardButtonNames.CheckedMenuButtonOne, checkedMenuCommand, false);

            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.CheckedMenuSync_RG1_BG1, checkedMenuCommand, ButtonSize.Large, ButtonStyle.DropDown, StandardButtonNames.CheckedMenuButtonTwo);
            RibbonBuilder.AddCheckedMenuItemToDropDownButton(StandardButtonNames.CheckedMenuButtonTwo, checkedMenuCommand, false);

            RibbonBuilder.AddRibbonGroupToTab(StandardTabNames.CheckedMenuSync, StandardRibbonGroupNames.CheckedMenuSync_RG2, StandardRibbonGroupNames.CheckedMenuSync_RG2, StandardGraphicResourceNames.ControlOverrides);
            RibbonBuilder.AddButtonGroupToRibbonGroup(StandardRibbonGroupNames.CheckedMenuSync_RG2, StandardButtonGroupNames.CheckedMenuSync_RG2_BG1);

            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.CheckedMenuSync_RG2_BG1, nullCommand, ButtonSize.Large, ButtonStyle.DropDownOnly, StandardButtonNames.CheckedMenuButtonThree);
            RibbonBuilder.AddCheckedMenuItemToDropDownButton(StandardButtonNames.CheckedMenuButtonThree, checkedMenuCommand, false);

            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.CheckedMenuSync_RG2_BG1, nullCommand, ButtonSize.Large, ButtonStyle.DropDownOnly, StandardButtonNames.CheckedMenuButtonFour);
            RibbonBuilder.AddCheckedMenuItemToDropDownButton(StandardButtonNames.CheckedMenuButtonFour, checkedMenuCommand, false);
        }

        private void LayoutFlyOutMenus()
        {
            RibbonBuilder.AddRibbonGroupToTab(StandardTabNames.FlyOutMenus, StandardRibbonGroupNames.FlyOutMenus_RG1, StandardRibbonGroupNames.FlyOutMenus_RG1, StandardGraphicResourceNames.ControlOverrides);
            RibbonBuilder.AddButtonGroupToRibbonGroup(StandardRibbonGroupNames.FlyOutMenus_RG1, StandardButtonGroupNames.FlyOutMenus_RG1_BG1);

            IRibbonUICommand clickCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ClickCommand);
            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.FlyOutMenus_RG1_BG1, clickCommand, ButtonSize.Large, ButtonStyle.DropDownOnly, StandardButtonNames.FlyOutMenuButton);

            IRibbonUICommand nullCommand = CommandFactory.GetUICommand(StandardTestCommandNames.NullCommand);
            RibbonBuilder.AddMenuItemToDropDownButton(StandardButtonNames.FlyOutMenuButton, nullCommand, true, StandardButtonNames.FlyOutBaseMenuItemOne);

            IRibbonUICommand newCommand = CommandFactory.GetUICommand(StandardTestCommandNames.NewCommand);
            RibbonBuilder.AddMenuItemToDropDownButton(StandardButtonNames.FlyOutMenuButton, newCommand, false, StandardButtonNames.FlyOutBaseMenuItemTwo);

            IRibbonUICommand helloWorldCommand = CommandFactory.GetUICommand(StandardTestCommandNames.HelloWorldCommand);
            RibbonBuilder.AddMenuItemToMenuItem(StandardButtonNames.FlyOutBaseMenuItemOne, helloWorldCommand, false, StandardButtonNames.FlyOutMenuItemOne);

            // ---

            RibbonBuilder.AddRibbonGroupToTab(StandardTabNames.FlyOutMenus, StandardRibbonGroupNames.FlyOutMenus_RG2, StandardRibbonGroupNames.FlyOutMenus_RG2, StandardGraphicResourceNames.FlushingHydrant);
            RibbonBuilder.AddButtonGroupToRibbonGroup(StandardRibbonGroupNames.FlyOutMenus_RG2, StandardButtonGroupNames.FlyOutMenus_RG2_BG1);

            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.FlyOutMenus_RG2_BG1, nullCommand, ButtonSize.Large, ButtonStyle.DropDownOnly, StandardButtonNames.FlyOutMenuButton2);
            RibbonBuilder.AddMenuItemToDropDownButton(StandardButtonNames.FlyOutMenuButton2, nullCommand, true, StandardButtonNames.FlyOutBaseMenuItemOne2);
            RibbonBuilder.AddMenuItemToMenuItem(StandardButtonNames.FlyOutBaseMenuItemOne2, helloWorldCommand, false, StandardButtonNames.FlyOutMenuItemOne2);
            RibbonBuilder.AddMenuItemToMenuItem(StandardButtonNames.FlyOutBaseMenuItemOne2, clickCommand, false, StandardButtonNames.FlyOutMenuItemTwo2);
            RibbonBuilder.AddMenuItemToMenuItem(StandardButtonNames.FlyOutBaseMenuItemOne2, newCommand, false, StandardButtonNames.FlyOutMenuItemThree2);

            RibbonBuilder.RemoveMenuItemFromMenuItem(StandardButtonNames.FlyOutBaseMenuItemOne2, StandardButtonNames.FlyOutMenuItemTwo2, clickCommand);
        }

        private void LayoutToggleButtons()
        {
            RibbonBuilder.AddRibbonGroupToTab(StandardTabNames.ToggleButtonSync, StandardRibbonGroupNames.ToggleButtonSync_RG1, StandardRibbonGroupNames.ToggleButtonSync_RG1, StandardGraphicResourceNames.ControlOverrides);
            RibbonBuilder.AddButtonGroupToRibbonGroup(StandardRibbonGroupNames.ToggleButtonSync_RG1, StandardButtonGroupNames.ToggleButtonSync_RG1_BG1);

            IRibbonUICommand toggleButtonOneCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleButtonSyncTestCommandOne);
            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.ToggleButtonSync_RG1_BG1, toggleButtonOneCommand, ButtonSize.Medium, ButtonStyle.Toggle);
            IRibbonUICommand toggleButtonTwoCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleButtonSyncTestCommandTwo);
            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.ToggleButtonSync_RG1_BG1, toggleButtonTwoCommand, ButtonSize.Medium, ButtonStyle.Toggle);
            IRibbonUICommand toggleButtonThreeCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleButtonSyncTestCommandThree);
            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.ToggleButtonSync_RG1_BG1, toggleButtonThreeCommand, ButtonSize.Medium, ButtonStyle.Toggle);

            RibbonBuilder.AddRibbonGroupToTab(StandardTabNames.ToggleButtonSync, StandardRibbonGroupNames.ToggleButtonSync_RG2, StandardRibbonGroupNames.ToggleButtonSync_RG2, StandardGraphicResourceNames.ControlOverrides);
            RibbonBuilder.AddButtonGroupToRibbonGroup(StandardRibbonGroupNames.ToggleButtonSync_RG2, StandardButtonGroupNames.ToggleButtonSync_RG2_BG1);

            toggleButtonOneCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleButtonSyncTestCommandOne);
            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.ToggleButtonSync_RG2_BG1, toggleButtonOneCommand, ButtonSize.Medium, ButtonStyle.Toggle);
            toggleButtonTwoCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleButtonSyncTestCommandTwo);
            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.ToggleButtonSync_RG2_BG1, toggleButtonTwoCommand, ButtonSize.Medium, ButtonStyle.Toggle);
            toggleButtonThreeCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleButtonSyncTestCommandThree);
            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.ToggleButtonSync_RG2_BG1, toggleButtonThreeCommand, ButtonSize.Medium, ButtonStyle.Toggle);

            IRibbonUICommand toggleButtonSync = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleButtonSyncDropDownCommand);
            RibbonBuilder.AddButtonToButtonGroup(StandardButtonGroupNames.ToggleButtonSync_RG2_BG1, toggleButtonSync, ButtonSize.Medium, ButtonStyle.DropDownOnly, StandardButtonNames.ToggleButtonSyncDropDown);

            IRibbonUICommand toggleButtonFourCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleButtonSyncTestCommandFour);
            RibbonBuilder.AddMenuItemToDropDownButton(StandardButtonNames.ToggleButtonSyncDropDown, toggleButtonFourCommand, false);

            IRibbonUICommand toggleButtonFiveCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleButtonSyncTestCommandFive);
            RibbonBuilder.AddMenuItemToDropDownButton(StandardButtonNames.ToggleButtonSyncDropDown, toggleButtonFiveCommand, false);
        }
        private void LayoutDropDownMenus(RibbonUICommandLinkedElementTypes linkedElementType)
        {
            bool toggleMenus = linkedElementType == RibbonUICommandLinkedElementTypes.DropDownToggleButtons;

            RibbonUICommand ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.ClickCommand) as RibbonUICommand;
            IList<FrameworkElement> elements = ribbonUICommand.GetLinkedElements(linkedElementType);
            AddStandardMenusToElements(elements, toggleMenus);

            ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.OpenModelessFormCommand) as RibbonUICommand;
            elements = ribbonUICommand.GetLinkedElements(linkedElementType);
            AddStandardMenusToElements(elements, toggleMenus);

            ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.OpenModalFormCommand) as RibbonUICommand;
            elements = ribbonUICommand.GetLinkedElements(linkedElementType);
            AddStandardMenusToElements(elements, toggleMenus);

            ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.HelloWorldCommand) as RibbonUICommand;
            elements = ribbonUICommand.GetLinkedElements(linkedElementType);
            AddStandardMenusToElements(elements, toggleMenus);
        }

        private void AddStandardMenusToElements(IList<FrameworkElement> elements, bool toggleMenus)
        {
            IRibbonUICommand menuOneCommand = CommandFactory.GetUICommand(StandardTestCommandNames.MenuOneCommand);
            IRibbonUICommand menuTwoCommand = CommandFactory.GetUICommand(StandardTestCommandNames.MenuTwoCommand);
            IRibbonUICommand menuThreeCommand = CommandFactory.GetUICommand(StandardTestCommandNames.MenuThreeCommand);

            if (toggleMenus)
            {
                menuOneCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleMenuOneCommand);
                menuTwoCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleMenuTwoCommand);
                menuThreeCommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleMenuThreeCommand);
            }

            foreach (System.Windows.Controls.Control existingDropDownButton in elements)
            {
                RibbonBuilder.AddMenuItemToDropDownButton(existingDropDownButton, menuOneCommand, false);
                RibbonBuilder.AddMenuItemToDropDownButton(existingDropDownButton, menuTwoCommand, true);
                RibbonBuilder.AddMenuItemToDropDownButton(existingDropDownButton, menuThreeCommand, false);
            }
        }

        private void LayoutTestButtons(string tabName, string RG1, string RG2, string RG3, string RG1_BG1, string RG1_BG2, string RG2_BG1, string RG3_BG1, ButtonStyle buttonStyle, bool toggle)
        {
            RibbonBuilder.AddRibbonGroupToTab(tabName, RG1, RG1, StandardGraphicResourceNames.ControlOverrides);

            RibbonBuilder.AddButtonGroupToRibbonGroup(RG1, RG1_BG1);
            IRibbonUICommand ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.ClickCommand);
            RibbonBuilder.AddButtonToButtonGroup(RG1_BG1, ribbonUICommand, ButtonSize.Large, buttonStyle);

            RibbonBuilder.AddButtonGroupToRibbonGroup(RG1, RG1_BG2);
            ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.OpenModelessFormCommand);
            RibbonBuilder.AddButtonToButtonGroup(RG1_BG2, ribbonUICommand, ButtonSize.Large, buttonStyle);
            ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.DockingWindowCommand);
            RibbonBuilder.AddDialogLauncherToRibbonGroup(RG1, ribbonUICommand);

            // ---

            RibbonBuilder.AddRibbonGroupToTab(tabName, RG2, RG2, StandardGraphicResourceNames.ControlOverrides);

            RibbonBuilder.AddButtonGroupToRibbonGroup(RG2, RG2_BG1);
            ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.OpenModalFormCommand);
            RibbonBuilder.AddButtonToButtonGroup(RG2_BG1, ribbonUICommand, ButtonSize.Medium, buttonStyle);
            RibbonBuilder.AddButtonToButtonGroup(RG2_BG1, ribbonUICommand, ButtonSize.Medium, buttonStyle);
            RibbonBuilder.AddButtonToButtonGroup(RG2_BG1, ribbonUICommand, "LabelOverride", ButtonSize.Medium, buttonStyle);

            // ---

            RibbonBuilder.AddRibbonGroupToTab(tabName, RG3, RG3, StandardGraphicResourceNames.ControlOverrides);

            RibbonBuilder.AddButtonGroupToRibbonGroup(RG3, RG3_BG1);
            ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.HelloWorldCommand);

            if (toggle)
                ribbonUICommand = CommandFactory.GetUICommand(StandardTestCommandNames.ToggleButtonOnlyCommand);

            RibbonBuilder.AddButtonToButtonGroup(RG3_BG1, ribbonUICommand, ButtonSize.Small, buttonStyle);
            RibbonBuilder.AddButtonToButtonGroup(RG3_BG1, ribbonUICommand, ButtonSize.Small, buttonStyle);
            RibbonBuilder.AddButtonToButtonGroup(RG3_BG1, ribbonUICommand, ButtonSize.Small, buttonStyle);
            RibbonBuilder.AddButtonToButtonGroup(RG3_BG1, ribbonUICommand, ButtonSize.Small, buttonStyle);
            RibbonBuilder.AddButtonToButtonGroup(RG3_BG1, ribbonUICommand, ButtonSize.Small, buttonStyle);
            RibbonBuilder.AddButtonToButtonGroup(RG3_BG1, ribbonUICommand, ButtonSize.Small, buttonStyle);
            RibbonBuilder.AddButtonToButtonGroup(RG3_BG1, ribbonUICommand, ButtonSize.Small, buttonStyle);
            RibbonBuilder.AddButtonToButtonGroup(RG3_BG1, ribbonUICommand, ButtonSize.Small, buttonStyle);
            RibbonBuilder.AddButtonToButtonGroup(RG3_BG1, ribbonUICommand, ButtonSize.Small, buttonStyle);
        }
        #endregion

        private QATButtonManager InitializeQATButtonManager()
        {
            QATButtonManager qatButtonManager = new QATButtonManager(CommandFactory);

            // Defaults
            qatButtonManager.AddRibbonUIButton(StandardTestCommandNames.NewCommand, true, true, ButtonSize.Small, ButtonStyle.Regular);
            qatButtonManager.AddRibbonUIButton(StandardTestCommandNames.OpenCommand, false, false, ButtonSize.Small, ButtonStyle.Regular);
            qatButtonManager.AddRibbonUIButton(StandardTestCommandNames.SaveCommand, true, false, ButtonSize.Small, ButtonStyle.Regular);

            return qatButtonManager;
        }

        private RibbonGlobalUserSettingsManager InitializeRibbonGlobalUserSettingsManager()
        {
            RibbonGlobalUserSettingsManager ribbonGlobalUserSettingsManager = new RibbonGlobalUserSettingsManager();
            ribbonGlobalUserSettingsManager.AddRibbonDefaultSettings(false, true);
            return ribbonGlobalUserSettingsManager;
        }

        /// <summary>
        /// ParentForm UI is "designed" in a UserControl rather than directly on the form.
        /// </summary>
        private UserControl ApplicationUI
        {
            get
            {
                if (m_applicationUI == null)
                    m_applicationUI = new UserControlApplicationUI();
                return m_applicationUI;
            }
        }

        private TelerikWPFRibbonHost RibbonHost
        {
            get
            {
                if (m_ribbonHost == null)
                {
                    m_ribbonHost = new TelerikWPFRibbonHost(
                        this,
                        "Bentley WaterGEMS",
                        this,
                        ApplicationUI,
                        GetKeyNavSupportFlags());
                }
                return m_ribbonHost;
            }
        }

        private IRibbonServices RibbonServices
        {
            get { return RibbonHost as IRibbonServices; }
        }


        private KeyNavSupportFlags GetKeyNavSupportFlags()
        {
            return KeyNavSupportFlags.All;
        }

        private TelerikWPFRibbonBuilder RibbonBuilder
        {
            get { return RibbonHost.RibbonBuilder; }
        }

        private RibbonUICommandFactory CommandFactory
        {
            get
            {
                if (m_ribbonUICommandFactory == null)
                    m_ribbonUICommandFactory = new RibbonUICommandFactory(this);
                return m_ribbonUICommandFactory;
            }
        }

        private QATButtonManager QATButtonManager
        {
            get
            {
                if (m_qatButtonManager == null)
                    m_qatButtonManager = InitializeQATButtonManager();
                return m_qatButtonManager;
            }
        }

        //private OnParentFormClosingDelegate OnParentFormClosing
        //{
        //    get;
        //    set;
        //}

        private TelerikWPFRibbonHost m_ribbonHost;
        private RibbonParentFormExtender m_ribbonParentFormExtender;
        private UserControl m_applicationUI;
        private RibbonUICommandFactory m_ribbonUICommandFactory;
        private QATButtonManager m_qatButtonManager;
    }
}
