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

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

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// Class that manages global user settings for the ribbon.
    /// </summary>
    public class RibbonGlobalUserSettingsManager : RibbonCustomizationManagerBase, IStorageElement, IObjectListManager
    {
        #region Constructor
        public RibbonGlobalUserSettingsManager()
            : base(RibbonCommandCustomizationStorageElementIDs.GlobalUserSettings,
                  RibbonCommandCustomizationStorageElementNames.GlobalUserSettings,
                  RibbonCommandCustomizationStorageElementVersions.GlobalUserSettings,
                  StorageStrings.ElementLabel,
                  String.Empty,
                  INITIAL_CAPACITY)
        {
        }
        #endregion

        #region Public Override Methods
        public override object Add(object[] arguments)
        {
            int position = Add();
            RibbonGlobalUserSettingsElement element = (RibbonGlobalUserSettingsElement)ElementAt(position);
            if (arguments != null)
            {
                bool isRibbonMinimized = (bool)arguments[0];
                element.IsRibbonMinimized = isRibbonMinimized;

                bool isQuickAccessToolBarAboveRibbon = (bool)arguments[1];
                element.IsQuickAccessToolBarAboveRibbon = isQuickAccessToolBarAboveRibbon;
            }
            else
                throw new Exception("Something awry in RibbonGlobalUserSettingsManager::Add()"); // I18N. OK.

            return element;
        }

        public override void Read(IStorageReadStream stream, int version)
        {
            CacheDefaultElements();
            Elements().Clear();

            IStorageXmlReadStream readStream = GetStorageXmlReadStream(stream);

            readStream.NextContentNode();
            if (readStream.Name != Name)
                readStream.NextContentNode();

            Add();

            readStream.NextContentNode();

            if (readStream.Name != StorageStrings.ElementLabel)
                throw new StorageException("Expected node " + StorageStrings.ElementLabel + " in RibbonGlobalUserSettingsManager::Read()"); // I18N. OK.

            IStorageAtom storageAtom = ElementAt(0) as IStorageAtom;
            if (storageAtom == null)
                throw new StorageException("Element must implement IStorageAtom in RibbonGlobalUserSettingsManager::Read()"); // I18N. OK.

            storageAtom.Read(stream, version);
        }

        public override void Write(IStorageWriteStream stream)
        {
            IStorageXmlWriteStream writeStream = stream as IStorageXmlWriteStream;

            writeStream.WriteStartElement(Name);

            IStorageAtom storageAtom = ElementAt(0) as IStorageAtom;

            writeStream.WriteStartElement(StorageStrings.ElementLabel);
            storageAtom.Write(stream);
            writeStream.WriteEndElement();

            writeStream.WriteEndElement();
        }
        #endregion

        #region IObjectListManager Members
        IList IObjectListManager.Elements()
        {
            return Elements();
        }
        #endregion

        #region Public Methods
        public void AddRibbonDefaultSettings(bool intiallyMinimized, bool quickAccessToolBarInitiallyAboveRibbon)
        {
            Add(new object[] { intiallyMinimized, quickAccessToolBarInitiallyAboveRibbon });
        }
        #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 IElementListManagerElement NewElement()
        {
            return new RibbonGlobalUserSettingsElement(Count, this);
        }

        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 void BeforeResetToDefaults()
        {
            // Note: <whar> No need to update any information on the defaults since unlike the QAT and the DropDownToggle managers, 
            // we know all we need to know up front.
        }
        #endregion

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

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

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