using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

namespace WeifenLuo.WinFormsUI.Docking
{
    /// <summary>
    /// 
    /// </summary>
    [ProvideProperty("EnableVSStyle", typeof(ToolStrip))]
    public partial class VisualStudioToolStripExtender : Component, IExtenderProvider
    {
        private class ToolStripProperties
        {
            private VsVersion version = VsVersion.Unknown;
            private readonly ToolStrip strip;
            private readonly Dictionary<ToolStripItem, string> menuText = new Dictionary<ToolStripItem, string>();
            

            public ToolStripProperties(ToolStrip toolstrip)
            {
                if (toolstrip == null) throw new ArgumentNullException("toolstrip");
                strip = toolstrip;

                if (strip is MenuStrip)
                    SaveMenuStripText();
            }

            public VsVersion VsVersion 
            {
                get { return this.version; }
                set
                {
                    this.version = value;
                    UpdateMenuText(this.version == VsVersion.Vs2012 || this.version == VsVersion.Vs2013);
                }
            }

            private void SaveMenuStripText()
            {
                foreach (ToolStripItem item in strip.Items)
                    menuText.Add(item, item.Text);
            }

            public void UpdateMenuText(bool caps)
            {
                foreach (ToolStripItem item in menuText.Keys)
                {
                    var text = menuText[item];
                    item.Text = caps ? text.ToUpper() : text;
                }
            }
        }

        private readonly Dictionary<ToolStrip, ToolStripProperties> strips = new Dictionary<ToolStrip, ToolStripProperties>();
        /// <summary>
        /// 
        /// </summary>
        public VisualStudioToolStripExtender()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="container"></param>
        public VisualStudioToolStripExtender(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        #region IExtenderProvider Members
        /// <summary>
        /// 
        /// </summary>
        /// <param name="extendee"></param>
        /// <returns></returns>
        public bool CanExtend(object extendee)
        {
            return extendee is ToolStrip;
        }

        #endregion
        /// <summary>
        /// 
        /// </summary>
        public ToolStripRenderer DefaultRenderer { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strip"></param>
        /// <returns></returns>
        [DefaultValue(false)]
        public VsVersion GetStyle(ToolStrip strip)
        {
            if (strips.ContainsKey(strip))
                return strips[strip].VsVersion;

            return VsVersion.Unknown;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strip"></param>
        /// <param name="version"></param>
        /// <param name="theme"></param>
        public void SetStyle(ToolStrip strip, VsVersion version, ThemeBase theme)
        {
            ToolStripProperties properties = null;

            if (!strips.ContainsKey(strip))
            {
                properties = new ToolStripProperties(strip) { VsVersion = version };
                strips.Add(strip, properties);
            }
            else
            {
                properties = strips[strip];
            }

            if (theme == null)
            {
                if (DefaultRenderer != null)
                    strip.Renderer = DefaultRenderer;
            }
            else
            {
                theme.ApplyTo(strip);
            }
            properties.VsVersion = version;
        }
        /// <summary>
        /// 
        /// </summary>
        public enum VsVersion
        {
            /// <summary>
            /// 
            /// </summary>
            Unknown,
            /// <summary>
            /// 
            /// </summary>
            Vs2003,
            /// <summary>
            /// 
            /// </summary>
            Vs2005,
            /// <summary>
            /// 
            /// </summary>
            Vs2008,
            /// <summary>
            /// 
            /// </summary>
            Vs2010,
            /// <summary>
            /// 
            /// </summary>
            Vs2012,
            /// <summary>
            /// 
            /// </summary>
            Vs2013,
            /// <summary>
            /// 
            /// </summary>
            Vs2015
        }
    }
}
