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

using System;
using System.Collections;
using System.Collections.Generic;
using Haestad.Framework.Manager;
using Haestad.Framework.UI.Integration;
using Haestad.Support.Storage;
using Haestad.Support.Support;

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// A class for managing drop down toggle button elements for persisting default commands for these special buttons.
    /// </summary>
    public class DropDownToggleSettingsManager : RibbonCustomizationManagerBase, IStorageElement
    {
        #region Constructor
        public DropDownToggleSettingsManager()
            : base(RibbonCommandCustomizationStorageElementIDs.DropDownToggleSettings,
                  RibbonCommandCustomizationStorageElementNames.DropDownToggleSettings,
                  RibbonCommandCustomizationStorageElementVersions.DropDownToggleSettings,
                  StorageStrings.ElementLabel, 
                  StorageStrings.ElementCountLabel, 
                  INITIAL_CAPACITY)
        {
        }
        #endregion

        #region Public Override Methods
        public override object Add(object[] arguments)
        {
            int position = Add();
            DropDownToggleSettingsElement element = (DropDownToggleSettingsElement)ElementAt(position);
            if (arguments != null)
            {
                string buttonName = (string)arguments[0];
                element.ButtonName = buttonName;

                string defaultCommandName = (string)arguments[1];
                element.DefaultCommandName = defaultCommandName;
            }
            else
                throw new Exception("Something awry in DropDownToggleSettingsManager::Add()"); // I18N. OK.

            return element;
        }
        #endregion

        #region Public Methods
        public void AddDropDownToggleButton(string buttonName, string defaultCommandName)
        {
            Add(new object[] { buttonName, defaultCommandName });
        }
        #endregion

        #region Internal Methods
        internal void CacheElements()
        {
            ElementsCache.Clear();
            foreach (IElementListManagerElement element in Elements())
                ElementsCache.Add(element);
        }

        internal void StoreDefaultsAndRestoreCache()
        {
            CacheDefaultElements();

            Elements().Clear();
            foreach (IElementListManagerElement element in ElementsCache)
                Elements().Add(element);
        }
        #endregion

        #region Protected Override Methods
        protected override void InitializeNewElement(int position)
        {
            // Note: <whar> Initialize state here that cannot be read from disk / user modified, 
            // but which is needed for the re-constituted object to be complete.
        }

        protected override void InitializeSupportedFields()
        {
            // Note: <whar> This manager has no user facing UI that requires fields,
            // so as far as I know I don't need to do anything here (unless we wish to expose a UI).
        }

        protected override IElementListManagerElement NewElement()
        {
            return new DropDownToggleSettingsElement(Count, this);
        }

        protected override void BeforeResetToDefaults()
        {
            // Note: <whar> Nothing to do here. The defaults in this case are implicitly arrived at through the order menus are added.
            // So we need to update the DefaultElements before the user has a chance to monkey with the settings (by using the buttons).
            // To that end, we leverage the AfterRibbonLayoutComplete hook on the RibbonBuilder to call internal methods on this class
            // that manage the building up of defaults based on the implicit default state, all the while preserving the state that was
            // loaded from disk.
        }
        #endregion

        #region Public Override Properties
        public override Type ElementType
        {
            get { return typeof(DropDownToggleSettingsElement); }
        }
        #endregion

        #region Private Properties
        private ElementListManagerElementCollection ElementsCache
        {
            get
            {
                if (m_elementsCache == null)
                    m_elementsCache = new ElementListManagerElementCollection(Elements().Count);
                return m_elementsCache;
            }
        }
        #endregion

        #region Private Fields
        private ElementListManagerElementCollection m_elementsCache;
        #endregion

        #region Private Structs
        private struct StorageStrings
        {
            public const string ElementLabel = "DownToggleButton"; // DO NOT LOCALIZE <whar>.
            public const string ElementCountLabel = "CountOfDropDownToggleButtons"; // DO NOT LOCALIZE <whar>.
        }
        #endregion

        #region Private Constants
        private const int INITIAL_CAPACITY = 16;
        #endregion
    }
}
