using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.Primitives;
using Telerik.WinControls.UI;

namespace Telerik.WinControls.UI
{
    public class SelectThemeButton: RadButton, IPropertiesProvider
    {
        //private RadButton innerButton;
        private readonly ColorBlendInfoList colorBlends;

        public SelectThemeButton()
        {
            this.ThemeClassName = typeof (RadButton).FullName;
            this.colorBlends = new ColorBlendInfoList();
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string ThemeClassName
        {
            get
            {
                return base.ThemeClassName;
            }
            set
            {
                base.ThemeClassName = value;
            }
        }

        [DefaultValue("")]
        public string InnerThemeName
        {
            get
            {
                //this.EnsureChildItems();
                //return InnerButton.ThemeName;

                return this.innerThemeName;
            }
            set
            {
                //this.EnsureChildItems();
                //InnerButton.ThemeName = value;                

                this.innerThemeName = value;
            }
        }

        
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ColorBlendInfoList ColorBlends
        {
            get
            {
                return this.colorBlends;
            }
        }

        protected override void CreateChildItems(Telerik.WinControls.RadElement parent)
        {
            base.CreateChildItems(parent);

            //StyleSheet btnStyle = new StyleSheet();
            //btnStyle.PropertySettingGroups.Add(
            //    new PropertySettingGroup("ButtonFill",
            //        new SimpleCondition(RadElement.IsMouseOverProperty, true),
            //        new PropertySetting(RadElement.FitToSizeModeProperty, RadFitToSizeMode.FitToParentPadding),
            //        new PropertySetting(FillPrimitive.GradientStyleProperty, GradientStyles.Solid),
            //                             new AnimatedPropertySetting(
            //                                 VisualElement.BackColorProperty,
            //                                 20,
            //                                 33,
            //                                 new ColorAnimationStep(0, 20, 20, 20))));

            //this.ButtonElement.ButtonFillElement.Style = btnStyle;

            //    innerButton = new RadButton();

            //    innerButton.BeginInit();

            //    innerButton.Dock = DockStyle.Fill;
            //    this.Padding = new Padding(3, 3, 3, 3);
            //    innerButton.Click += innerButton_Click;
            //    innerButton.RootElement.RadPropertyChanging += RootElement_RadPropertyChanged;

            //    innerButton.EndInit();

            //    this.Controls.Add(innerButton);
        }

        void innerButton_Click(object sender, EventArgs e)
        {
            this.PerformClick();
            //RadButton would not call OnClick in PerformClick
            this.OnClick(EventArgs.Empty);
        }

        private bool changingStyle;

        private StyleSheet styleClone = null;

        void RootElement_RadPropertyChanged(object sender, RadPropertyChangingEventArgs e)
        {
            if (changingStyle)
            {
                return;
            }

            if (this.InnerThemeName == string.Empty)
            {
                return;
            }

            if (e.Property == RadElement.StyleProperty)
            {
                StyleSheet style = (StyleSheet) e.NewValue;

                if (style == null)
                {
                    return;
                }

                changingStyle = true;

                //processing stylesheet only once
                if (styleClone == null)
                {
                    XmlStyleSheet xmlClone = new XmlStyleSheet(style);

                    styleClone = xmlClone.GetStyleSheet();

                    for (int i = 0; i < colorBlends.Count; i++)
                    {
                        ColorBlendInfo info = colorBlends[i];
                        Theme.ApplyColorBlendToStyleSheet(info.OriginalColor, i.ToString(), this, styleClone);
                    }
                }

                e.NewValue = styleClone;
                

                changingStyle = false;
            }
        }

        private Control controlToApplyTheme;
        private string innerThemeName;

        [TypeConverter(typeof(ReferenceConverter))]
        public Control ControlToApplyTheme
        {
            get
            {
                return this.controlToApplyTheme;
            }
            set
            {
                this.controlToApplyTheme = value;
            }
        }

        //public RadButton InnerButton
        //{
        //    get
        //    {
        //        this.EnsureChildItems();
        //        return innerButton;
        //    }
        //}

        //protected override void Dispose(bool disposing)
        //{
        //    base.Dispose(disposing);

        //    if (InnerButton != null)
        //    {
        //        InnerButton.Click -= this.innerButton_Click;
        //        InnerButton.RootElement.RadPropertyChanging -= RootElement_RadPropertyChanged;
        //    }
        //}

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);

            if (this.controlToApplyTheme != null && this.InnerThemeName != string.Empty)
            {
                Theme theme = ThemeResolutionService.GetTheme(this.InnerThemeName);
                if (theme != null)
                {
                    theme.RemoveAllColorBlends(false);

                    if (colorBlends.Count > 0)
                    {
                        for (int i = 0; i < colorBlends.Count; i++)
                        {
                            ColorBlendInfo info = colorBlends[i];
                            string propertyName = this.Name + "Parameter" + i;
                            theme.AddColorBlend(propertyName, info.OriginalColor, false);
                            theme.ThemeProperties[propertyName] = info.NewColor;
                        }
                    }

                    theme.NotifyThemeChanged();
                }

                ThemeResolutionService.ApplyThemeToControlTree(this.controlToApplyTheme, this.InnerThemeName);
            }
        }

        #region IPropertiesProvider Members

        object IPropertiesProvider.GetPropertyValue(string propertyName)
        {
            int propertyIndex;

            if (int.TryParse(propertyName, out propertyIndex))
            {
                if (propertyIndex < this.ColorBlends.Count)
                {
                    return this.ColorBlends[propertyIndex].NewColor;
                }
            }

            return HslColor.FromColor(Color.Red);
        }

        void IPropertiesProvider.SetPropertyValue(string propertyName, object value)
        {
            throw new InvalidOperationException("Color blend can be changed only through ColorBlends property");
        }

        #endregion
    }
}
