using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using DevComponents.DotNetBar.Charts.Style;
namespace DevComponents.DotNetBar.Charts
{
    /// 
    /// Represents the collection of Axis Stripes.
    /// 
    [Editor("DevComponents.Charts.Design.AxisStripeCollectionEditor, DevComponents.Charts.Design, " +
        "Version=14.1.0.37, Culture=neutral,  PublicKeyToken=90f470f34c89ccaf", typeof(UITypeEditor))]
    public class AxisStripeCollection : CustomNamedCollection
    {
        public AxisStripeCollection()
        {
        }
        #region GetUniqueName
        public string GetUniqueName()
        {
            return (GetUniqueName("Stripe"));
        }
        #endregion
    }
    /// 
    /// Element describing a color stripe on the chart.  Each AxisStripe
    /// is associated with a primary or Ancillary Axis.
    /// 
    public class AxisStripe : ChartVisualElement, ILegendItem
    {
        #region Private variables
        private States _States;
        private object _MinValue;
        private object _MaxValue;
        private AxisStripeVisualStyle _AxisStripeVisualStyle;
        private string _LegendText;
        private ChartLegendItem _LegendItem;
        private ChartLegendItemVisualStyles _ChartLegendItemVisualStyles;
        #endregion
        #region Constructors
        /// 
        /// Create a new AxisStripe.
        /// 
        /// 
        /// 
        /// 
        public AxisStripe(string name, object minValue, object maxValue)
            : this()
        {
            Name = name;
            MinValue = minValue;
            MaxValue = maxValue;
        }
        /// 
        /// Create a new AxisStripe.
        /// 
        /// 
        public AxisStripe(string name)
            : this()
        {
            Name = name;
        }
        /// 
        /// Create a new AxisStripe.
        /// 
        public AxisStripe()
        {
            InitDefaultStates();
        }
        #endregion
        #region InitDefaultStates
        private void InitDefaultStates()
        {
            SetState(States.CheckedInLegend, true);
            SetState(States.ShowInLegend, true);
            SetState(States.ShowInParentLegend, true);
            SetState(States.ShowCheckBoxInLegend, true);
            SetState(States.ShowMarkerInLegend, true);
        }
        #endregion
        #region Public properties
        #region AxisStripeVisualStyle
        /// 
        /// Gets or sets the visual style for the AxisStripe.
        /// 
        [Category("Style")]
        [Description("Indicates the visual style for the AxisStripe.")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public AxisStripeVisualStyle AxisStripeVisualStyle
        {
            get
            {
                if (_AxisStripeVisualStyle == null)
                {
                    _AxisStripeVisualStyle = new AxisStripeVisualStyle();
                    StyleVisualChangeHandler(null, _AxisStripeVisualStyle);
                }
                return (_AxisStripeVisualStyle);
            }
            set
            {
                if (_AxisStripeVisualStyle != value)
                {
                    AxisStripeVisualStyle oldValue = _AxisStripeVisualStyle;
                    _AxisStripeVisualStyle = value;
                    OnVisualStyleChanged("AxisStripeVisualStyle", oldValue, value);
                    if (oldValue != null)
                        oldValue.Dispose();
                }
            }
        }
        #endregion
        #region IsDisplayed
        ///
        /// Gets whether the Stripe is displayed (must be Visible
        /// and appropriately 'checked' if presented in the Legend).
        ///
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool IsDisplayed
        {
            get
            {
                return ((Visible == true) &&
                        (ShowCheckBoxInLegend == false || CheckedInLegend == true));
            }
        }
        #endregion
        #region MaxValue
        ///
        /// Gets or sets the maximum axis stripe value.
        ///
        [DefaultValue(null), Category("Data")]
        [Description("Indicates the maximum axis stripe value.")]
        [TypeConverter("DevComponents.Charts.Design.PointValueConverter, DevComponents.Charts.Design," +
            "Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf")]
        public object MaxValue
        {
            get { return (_MaxValue); }
            set
            {
                if (value != _MaxValue)
                {
                    _MaxValue = value;
                    OnPropertyChangedEx("MaxValue", Style.VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region MinValue
        ///
        /// Gets or sets the minimum axis stripe value.
        ///
        [DefaultValue(null), Category("Data")]
        [Description("Indicates the minimum axis stripe value.")]
        [TypeConverter("DevComponents.Charts.Design.PointValueConverter, DevComponents.Charts.Design," +
            "Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf")]
        public object MinValue
        {
            get { return (_MinValue); }
            set
            {
                if (value != _MinValue)
                {
                    _MinValue = value;
                    OnPropertyChangedEx("MinValue", Style.VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #endregion
        #region MeasureOverride
        protected override void MeasureOverride(ChartLayoutInfo layoutInfo)
        {
        }
        #endregion
        #region ArrangeOverride
        protected override void ArrangeOverride(ChartLayoutInfo layoutInfo)
        {
        }
        #endregion
        #region RenderOverride
        protected override void RenderOverride(ChartRenderInfo renderInfo)
        {
        }
        #endregion
        #region Style support
        #region InvalidateStyle
        ///
        ///Invalidate the cached Styles
        ///
        public void InvalidateStyle()
        {
            ClearEffectiveStyles();
        }
        #endregion
        #region ClearEffectiveStyles
        protected override void ClearEffectiveStyles()
        {
            base.ClearEffectiveStyles();
            if (LegendItem != null)
                LegendItem.EffectiveStyles.InvalidateStyles();
        }
        #endregion
        #region StyleChanged
        protected override void StyleChanged(object sender, PropertyChangedEventArgs e)
        {
            base.StyleChanged(sender, e);
            if (sender is ChartLegendItemVisualStyles && LegendItem != null)
                InvalidateRender(LegendItem.Bounds);
        }
        #endregion
        #endregion
        #region ILegendItem
        #region ChartLegendItemVisualStyles
        /// 
        /// Gets or sets the visual styles for the Legend item.
        /// 
        [Category("Style")]
        [Description("Indicates the visual styles for the Legend item.")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ChartLegendItemVisualStyles ChartLegendItemVisualStyles
        {
            get
            {
                if (_ChartLegendItemVisualStyles == null)
                {
                    _ChartLegendItemVisualStyles = new ChartLegendItemVisualStyles();
                    StyleVisualChangeHandler(null, _ChartLegendItemVisualStyles);
                }
                return (_ChartLegendItemVisualStyles);
            }
            set
            {
                if (_ChartLegendItemVisualStyles != value)
                {
                    ChartLegendItemVisualStyles oldValue = _ChartLegendItemVisualStyles;
                    _ChartLegendItemVisualStyles = value;
                    OnStyleChanged("ChartLegendItemVisualStyles", oldValue, value);
                    if (oldValue != null)
                        oldValue.Dispose();
                }
            }
        }
        #endregion
        #region CheckedInLegend
        /// 
        /// Gets or sets whether the ReferenceLine is checked in the Legend.
        /// 
        [DefaultValue(true), Category("Legend")]
        [Description("Indicates whether the ReferenceLine is checked in the Legend.")]
        public bool CheckedInLegend
        {
            get { return (TestState(States.CheckedInLegend)); }
            set
            {
                if (value != CheckedInLegend)
                {
                    SetState(States.CheckedInLegend, value);
                    if (LegendItem != null)
                        LegendItem.UpdateCheckState();
                    
                    OnPropertyChangedEx("CheckedInLegend", VisualChangeType.Render);
                }
            }
        }
        #endregion
        #region ShowCheckBoxInLegend
        /// 
        /// Gets or sets whether a checkbox for the ReferenceLine is shown in the Legend.
        /// 
        [DefaultValue(true), Category("Legend")]
        [Description("Indicates whether a checkbox for the ReferenceLine is shown in the Legend.")]
        public bool ShowCheckBoxInLegend
        {
            get { return (TestState(States.ShowCheckBoxInLegend)); }
            set
            {
                if (value != ShowCheckBoxInLegend)
                {
                    SetState(States.ShowCheckBoxInLegend, value);
                    OnPropertyChangedEx("ShowCheckBoxInLegend", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ShowInLegend
        /// 
        /// Gets or sets whether the ReferenceLine is shown in the Legend.
        /// 
        [DefaultValue(true), Category("Legend")]
        [Description("Indicates whether the ReferenceLine is shown in the Legend.")]
        public bool ShowInLegend
        {
            get { return (TestState(States.ShowInLegend)); }
            set
            {
                if (value != ShowInLegend)
                {
                    SetState(States.ShowInLegend, value);
                    OnPropertyChangedEx("ShowInLegend", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ShowInParentLegend
        /// 
        /// Gets or sets whether the Reference Line is shown in parent Legend(s).
        /// 
        [DefaultValue(true), Category("Legend")]
        [Description("Indicates whether the Reference Line is shown in parent Legend(s).")]
        public bool ShowInParentLegend
        {
            get { return (TestState(States.ShowInParentLegend)); }
            set
            {
                if (value != ShowInParentLegend)
                {
                    SetState(States.ShowInParentLegend, value);
                    OnPropertyChangedEx("ShowInParentLegend", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ShowMarkerInLegend
        /// 
        /// Gets or sets whether the ReferenceLine Marker is shown in the Legend.
        /// 
        [DefaultValue(true), Category("Legend")]
        [Description("Indicates whether the ReferenceLine Marker is shown in the Legend.")]
        public bool ShowMarkerInLegend
        {
            get { return (TestState(States.ShowMarkerInLegend)); }
            set
            {
                if (value != ShowMarkerInLegend)
                {
                    SetState(States.ShowMarkerInLegend, value);
                    OnPropertyChangedEx("ShowMarkerInLegend", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region LegendItem
        ///
        /// Gets the item's parent LegendItem.
        ///
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Description("Indicates the item's parent LegendItem.")]
        public ChartLegendItem LegendItem
        {
            get { return (_LegendItem); }
            internal set { _LegendItem = value; }
        }
        #endregion
        #region LegendText
        ///
        /// Gets or sets the text to display in the legend.
        ///
        [DefaultValue(null), Category("Legend")]
        [Description("Indicates the text to display in the legend.")]
        [NotifyParentProperty(true)]
        public string LegendText
        {
            get { return (_LegendText); }
            set
            {
                if (value != _LegendText)
                {
                    _LegendText = value;
                    OnPropertyChangedEx("LegendText", Style.VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region GetLegendItem
        /// 
        /// Creates the LegendItem for the AxisStripe.
        /// 
        /// 
        public ChartLegendItem GetLegendItem()
        {
            _LegendItem = null;
            if (ShowInLegend == true)
            {
                _LegendItem = new ChartLegendItem();
                if (CheckedInLegend == true)
                    _LegendItem.CheckState = CheckState.Checked;
                _LegendItem.Name = Name;
                _LegendItem.ItemText = LegendText;
                if (string.IsNullOrEmpty(_LegendItem.Name) == true)
                    _LegendItem.Name = "(RefLine)";
                _LegendItem.ChartItems.Add(this);
            }
            return (_LegendItem);
        }
        #endregion
        #region GetLegendItems
        /// 
        /// Creates a list of legend items associated with
        /// the AxisStripe.
        /// 
        /// 
        public List GetLegendItems()
        {
            List list = null;
            ChartLegendItem item = GetLegendItem();
            if (item != null)
            {
                list = new List(1);
                list.Add(item);
            }
            return (list);
        }
        #endregion
        #region GetLegendItemColor
        /// 
        /// Gets the default color associated with the
        /// AxisStripe legend item.
        /// 
        /// 
        public Color GetLegendItemColor()
        {
            AxisStripeVisualStyle astyle = AxisStripeVisualStyle;
            Color color = astyle.Background.Color1;
            if (color.IsEmpty == false)
                return (color);
            return (Color.DimGray);
        }
        #endregion
        #region RenderLegendItemMarker
        /// 
        /// Renders the Legend item Marker.
        /// 
        /// 
        /// 
        /// 
        public void RenderLegendItemMarker(Graphics g,
            ChartLegendItem litem, ChartLegendItemVisualStyle style)
        {
            AxisStripeVisualStyle astyle = AxisStripeVisualStyle;
            Rectangle bounds = litem.MarkerBounds;
            if (astyle.Background.IsEmpty == true)
            {
                g.FillRectangle(Brushes.LightPink, bounds);
            }
            else
            {
                using (Brush br = astyle.Background.GetBrush(bounds))
                    g.FillRectangle(br, bounds);
            }
        }
        #endregion
        #endregion
        #region Copy/CopyTo
        /// 
        /// Creates a copy of the element.
        /// 
        /// 
        public override ChartVisualElement Copy()
        {
            AxisStripe copy = new AxisStripe();
            CopyTo(copy);
            return (copy);
        }
        /// 
        /// Copies the current element properties and styles 
        /// to the provided "copy".
        /// 
        /// 
        public override void CopyTo(ChartVisualElement copy)
        {
            AxisStripe c = copy as AxisStripe;
            if (c != null)
            {
                base.CopyTo(c);
                c.AxisStripeVisualStyle =  (_AxisStripeVisualStyle != null) ? _AxisStripeVisualStyle.Copy() : null;
                c.ChartLegendItemVisualStyles = (_ChartLegendItemVisualStyles != null) ? ChartLegendItemVisualStyles.Copy() : null;
                c.MaxValue = MaxValue;
                c.MinValue = MinValue;
                c.CheckedInLegend = CheckedInLegend;
                c.ShowCheckBoxInLegend = ShowCheckBoxInLegend;
                c.ShowInLegend = ShowInLegend;
                c.ShowInParentLegend = ShowInParentLegend;
                c.LegendText = LegendText;
                c.ShowMarkerInLegend = ShowMarkerInLegend;
            }
        }
        #endregion
        #region GetSerialData
        internal override SerialElementCollection GetSerialData(string serialName)
        {
            SerialElementCollection sec = new SerialElementCollection();
            if (serialName != null)
            {
                if (serialName.Equals("") == true)
                    serialName = "ChartAxis";
                sec.AddStartElement(serialName);
            }
            if (_AxisStripeVisualStyle != null && _AxisStripeVisualStyle.IsEmpty == false)
                sec.AddElement(_AxisStripeVisualStyle.GetSerialData("AxisStripeVisualStyle"));
            if (_ChartLegendItemVisualStyles != null)
                sec.AddElement(_ChartLegendItemVisualStyles.GetSerialData("ChartLegendItemVisualStyles"));
            sec.AddDataValue("MaxValue", MaxValue, null);
            sec.AddDataValue("MinValue", MinValue, null);
            sec.AddValue("CheckedInLegend", CheckedInLegend, true);
            sec.AddValue("LegendText", LegendText, null);
            sec.AddValue("ShowCheckBoxInLegend", ShowCheckBoxInLegend, true);
            sec.AddValue("ShowInLegend", ShowInLegend, true);
            sec.AddValue("ShowInParentLegend", ShowInParentLegend, true);
            sec.AddValue("ShowMarkerInLegend", ShowMarkerInLegend, true);
            sec.AddElement(base.GetSerialData(null));
            if (serialName != null)
                sec.AddEndElement(serialName);
            return (sec);
        }
        #endregion
        #region PutSerialData
        #region ProcessValue
        internal override void ProcessValue(SerialElement se)
        {
            switch (se.Name)
            {
                case "CheckedInLegend":
                    CheckedInLegend = bool.Parse(se.StringValue);
                    break;
                case "LegendText":
                    LegendText = se.StringValue;
                    break;
                case "MaxValue":
                    MaxValue = se.DataValue;
                    break;
                case "MinValue":
                    MinValue = se.DataValue;
                    break;
                case "ShowCheckBoxInLegend":
                    ShowCheckBoxInLegend = bool.Parse(se.StringValue);
                    break;
                case "ShowInLegend":
                    ShowInLegend = bool.Parse(se.StringValue);
                    break;
                case "ShowInParentLegend":
                    ShowInParentLegend = bool.Parse(se.StringValue);
                    break;
                case "ShowMarkerInLegend":
                    ShowMarkerInLegend = bool.Parse(se.StringValue);
                    break;
                default:
                    base.ProcessValue(se);
                    break;
            }
        }
        #endregion
        #region ProcessCollection
        internal override void ProcessCollection(SerialElement se)
        {
            SerialElementCollection sec = se.Sec;
            switch (se.Name)
            {
                case "AxisStripeVisualStyle":
                    sec.PutSerialData(AxisStripeVisualStyle);
                    break;
                case "ChartLegendItemVisualStyles":
                    sec.PutSerialData(ChartLegendItemVisualStyles);
                    break;
                default:
                    base.ProcessCollection(se);
                    break;
            }
        }
        #region GetAncillaryAxis
        private ChartAxis GetAncillaryAxis(ChartAxesCollection axes, string name)
        {
            foreach (ChartAxis axis in axes)
            {
                if (name.Equals(axis.Name) == true)
                    return (axis);
            }
            return (null);
        }
        #endregion
        #endregion
        #endregion
        #region States
        [Flags]
        private enum States : uint
        {
            CheckedInLegend = (1U << 1),
            ShowCheckBoxInLegend = (1U << 2),
            ShowInLegend = (1U << 3),
            ShowInParentLegend = (1U << 4),
            ShowMarkerInLegend = (1U << 5),
        }
        #region TestState
        private bool TestState(States state)
        {
            return ((_States & state) == state);
        }
        #endregion
        #region SetState
        private void SetState(States state, bool value)
        {
            if (value == true)
                _States |= state;
            else
                _States &= ~state;
        }
        #endregion
        #endregion
        #region IDisposable
        public override void Dispose()
        {
            AxisStripeVisualStyle = null;
            base.Dispose();
        }
        #endregion
    }
}