using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using DevComponents.DotNetBar.Rendering;
namespace DevComponents.DotNetBar.Controls
{
    /// 
    /// Represents control which visually indicates current step in a sequence.
    /// 
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(StepIndicator), "Controls.StepIndicator.ico")]
    public class StepIndicator : Control
    {
        #region Constructor
        /// 
        /// Initializes a new instance of the StepIndicator class.
        /// 
        public StepIndicator()
        {
            this.SetStyle(ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.Opaque |
                ControlStyles.ResizeRedraw |
                ControlStyles.DoubleBuffer |
                ControlStyles.StandardDoubleClick | ControlStyles.StandardClick, true);
            this.SetStyle(ControlStyles.Selectable, false);
        }
        #endregion
        #region Implementation
        protected override void OnPaint(PaintEventArgs e)
        {
            StepIndicatorColorTable colors = GetColors();
            Color backColor = colors.BackgroundColor;
            Color indicatorColor = colors.IndicatorColor;
            Graphics g = e.Graphics;
            Rectangle r = this.ClientRectangle;
            using (SolidBrush brush = new SolidBrush(backColor))
                g.FillRectangle(brush, r);
            int currentStep = GetCurrentStep() - 1;
            if (currentStep >= 0)
            {
                int stepCount = _StepCount;
                Rectangle indicatorBounds;
                if (_Orientation == eOrientation.Horizontal)
                {
                    int indicatorWidth = (int)Math.Ceiling((double)r.Width / stepCount);
                    indicatorBounds = new Rectangle(r.X + indicatorWidth * currentStep, r.Y, indicatorWidth, r.Height);
                }
                else
                {
                    int indicatorHeight = (int)Math.Ceiling((double)r.Height / stepCount);
                    indicatorBounds = new Rectangle(r.X, r.Y + indicatorHeight * currentStep, r.Width, indicatorHeight);
                }
                if (r.Width > 0 && r.Height > 0)
                {
                    using (SolidBrush brush = new SolidBrush(indicatorColor))
                        g.FillRectangle(brush, indicatorBounds);
                }
            }
            base.OnPaint(e);
        }
        private StepIndicatorColorTable GetColors()
        {
            if (!_IndicatorColor.IsEmpty && !_BackgroundColor.IsEmpty)
                return new StepIndicatorColorTable(_BackgroundColor, _IndicatorColor);
            if (GlobalManager.Renderer is Office2007Renderer)
            {
                return ((Office2007Renderer)GlobalManager.Renderer).ColorTable.StepIndicator;
            }
            else
                return new StepIndicatorColorTable(Color.White, Color.MediumSeaGreen);
        }
        private int GetCurrentStep()
        {
            return Math.Max(0, Math.Min(_StepCount, _CurrentStep));
        }
        private int _StepCount = 10;
        /// 
        /// Gets or sets the total number of steps that control will track. Default value is 10.
        /// 
        [DefaultValue(10), Category("Behavior"), Description("Indicates total number of steps that control will track.")]
        public int StepCount
        {
            get { return _StepCount; }
            set
            {
                value = Math.Max(1, value);
                if (value != _StepCount)
                {
                    int oldValue = _StepCount;
                    _StepCount = value;
                    OnStepCountChanged(oldValue, value);
                }
            }
        }
        /// 
        /// Called when StepCount property has changed.
        /// 
        /// Old property value
        /// New property value
        protected virtual void OnStepCountChanged(int oldValue, int newValue)
        {
            //OnPropertyChanged(new PropertyChangedEventArgs("StepCount"));
            this.Invalidate();
        }
        private int _CurrentStep = 1;
        /// 
        /// Gets or sets the current step in sequence. Current step should be less or equal than StepCount.
        /// 
        [DefaultValue(1), Category("Behavior"), Description("Indicates current step in sequence..")]
        public int CurrentStep
        {
            get { return _CurrentStep; }
            set
            {
                if (value != _CurrentStep)
                {
                    int oldValue = _CurrentStep;
                    _CurrentStep = value;
                    OnCurrentStepChanged(oldValue, value);
                }
            }
        }
        /// 
        /// Called when CurrentStep property has changed.
        /// 
        /// Old property value
        /// New property value
        protected virtual void OnCurrentStepChanged(int oldValue, int newValue)
        {
            //OnPropertyChanged(new PropertyChangedEventArgs("CurrentStep"));
            this.Invalidate();
        }
        private static readonly Color DefaultBackgroundColor = Color.Empty;
        private Color _BackgroundColor = DefaultBackgroundColor;
        /// 
        /// Gets or sets the background color of the control.
        /// 
        [Category("Appearance"), Description("Indicates background color of control.")]
        public  Color BackgroundColor
        {
        	get {	return _BackgroundColor; }
        	set { _BackgroundColor = value;	}
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeBackgroundColor()
        {
            return _BackgroundColor != DefaultBackgroundColor;
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        public void ResetBackgroundColor()
        {
            this.BackgroundColor =  DefaultBackgroundColor;
        }
        private static readonly Color DefaultIndicatorColor = Color.Empty;
        private Color _IndicatorColor = DefaultIndicatorColor;
        /// 
        /// Gets or sets the color of the current step indicator.
        /// 
        [Category("Appearance"), Description("Indicates color of current step indicator.")]
        public  Color IndicatorColor
        {
        	get {	return _IndicatorColor; }
        	set { _IndicatorColor = value;	}
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeIndicatorColor()
        {
            return _IndicatorColor != DefaultIndicatorColor;
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        public void ResetIndicatorColor()
        {
            this.IndicatorColor =  DefaultIndicatorColor;
        }
        private eOrientation _Orientation = eOrientation.Horizontal;
        /// 
        /// Indicates the control orientation.
        /// 
        [Category("Appearance"), DefaultValue(eOrientation.Horizontal), Description("Indicates the control orientation.")]
        public eOrientation Orientation
        {
            get { return _Orientation; }
            set
            {
                if (value != _Orientation)
                {
                    eOrientation oldValue = _Orientation;
                    _Orientation = value;
                    OnOrientationChanged(oldValue, value);
                }
            }
        }
        /// 
        /// Called when Orientation property has changed.
        /// 
        /// Old property value
        /// New property value
        protected virtual void OnOrientationChanged(eOrientation oldValue, eOrientation newValue)
        {
            //OnPropertyChanged(new PropertyChangedEventArgs("Orientation"));
            this.Invalidate();
        }
        protected override Size DefaultSize
        {
            get
            {
                return new Size(400,4);
            }
        }
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
        public override System.Drawing.Color BackColor
        {
            get
            {
                return base.BackColor;
            }
            set
            {
                base.BackColor = value;
            }
        }
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
        public override System.Drawing.Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
            }
        }
        #endregion
    }
}