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

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

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// Abstract base class that defines RibbonUIElements that can be placed into the Ribbon using the TelerikWPFRibbonBuilder API.
    /// Elements know how to persist themselves to disk and read themselves from disk.
    /// </summary>
    public abstract class RibbonUIElementBase : ElementListManagerElementBase, IRibbonUIElement, IStorageAtom, ICloneableElement
    {
        #region Constructor
        public RibbonUIElementBase(int id, RibbonUIElementManagerBase manager) 
            : base(id, manager)
        {
        }
        #endregion

        #region IRibbonUIElement Members
        public string AssociatedCommandName
        {
            get;
            set;
        }

        public IRibbonUICommand AssociatedCommand
        {
            get { return RibbonUICommandFactory.GetUICommand(AssociatedCommandName); }
        }

        public IRibbonUICommandFactory RibbonUICommandFactory
        {
            private get;
            set;
        }

        public UIElement AssociatedUIElement
        {
            get;
            set;
        }

        public bool IsVisible
        {
            get;
            set;
        }

        public bool SeparatorAfter
        {
            get;
            set;
        }
        #endregion

        #region IStorageAtom Members
        public string Name
        {
            get { return AssociatedCommandName; }
        }

        public virtual void Read(IStorageReadStream stream, int version)
        {
            if (version >= 1)
            {
                AssociatedCommandName = stream.ReadStringAttribute(RibbonUIElementBaseNames.AssociatedCommandName);
                IsVisible = stream.ReadBooleanAttribute(RibbonUIElementBaseNames.IsVisible);
                SeparatorAfter = stream.ReadBooleanAttribute(RibbonUIElementBaseNames.SeparatorAfter);
            }
        }

        public virtual void Write(IStorageWriteStream stream)
        {
            stream.WriteAttribute(RibbonUIElementBaseNames.AssociatedCommandName, Name);
            stream.WriteAttribute(RibbonUIElementBaseNames.IsVisible, IsVisible);
            stream.WriteAttribute(RibbonUIElementBaseNames.SeparatorAfter, SeparatorAfter);
        }
        #endregion

        #region IClonableElement Members
        public void AddClone()
        {
            int position = Manager.Add();
            RibbonUIElementBase newElement = Manager.ElementAt(position) as RibbonUIElementBase;
            CloneProperties(newElement);
        }
        #endregion

        #region Protected Virtual Methods
        protected virtual void CloneProperties(RibbonUIElementBase clone)
        {
            clone.AssociatedCommandName = AssociatedCommandName;
            clone.AssociatedUIElement = AssociatedUIElement;
            clone.IsVisible = IsVisible;
            clone.SeparatorAfter = SeparatorAfter;
        }
        #endregion

        #region Public Override Properties
        public override string Label
        {
            get { return string.Empty; }
            set { }
        }
        #endregion

        #region Private Structs
        private struct RibbonUIElementBaseNames
        {
            public const string AssociatedCommandName = "AssociatedCommandName";
            public const string IsVisible = "IsVisible";
            public const string SeparatorAfter = "SeparatorAfter";
        }
        #endregion
    }
}
