using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using C1.Win.C1Input;
using C1.Util.Localization;

namespace C1.Win.Chart.RangeSelector
{
    /// <summary>
    /// Represents a FlexChart range slider control supporting additional visual styles.
    /// </summary>
    [C1Description("RangeSelector", "A FlexChart range slider control supporting additional visual styles.")]
    [DefaultEvent("ValueChanged")]
    public partial class RangeSelector : C1RangeSlider
    {
        private FlexChart _parentChart;

        public RangeSelector(FlexChart chart)
            : base()
        {
            if (chart == null) throw new ArgumentNullException();
            if (chart.AxisX == null) throw new ArgumentNullException();

            //Set range selector styles
            SetRangeSelectorDefaultStyles();
            SetThumbShapes();

            //Geting chart paremeters
            _parentChart = chart;
            UpperValue = Maximum = _parentChart.AxisX.ActualMax;
            LowerValue = Minimum = _parentChart.AxisX.ActualMin;
            _parentChart.Paint += ParentChart_Paint;
            SetRangeSelectorSize();

            //Add RangeSelector to the chart
            _parentChart.Controls.Add(this);
        }

        private void SetRangeSelectorDefaultStyles()
        {
            BackColor = Color.Transparent;
            VisualStyle = C1Input.VisualStyle.Office2007Black;
            IsRangeBarVisible = false;
            Styles.BarStyle.SelectedAreaColor = Color.FromArgb(50, Color.DimGray);
            Styles.ThumbStyle.PressedBackColor = Color.FromArgb(75, 0, 50, 200);
            Styles.ThumbStyle.CornerRadius = 3;
            Padding = new Padding(0);
            LargeChange = UpperValue - LowerValue;
            ScrollBehavior = RangeSliderScrollBehavior.Range;
        }

        void ParentChart_Paint(object sender, EventArgs e)
        {
            if (Width != (int)_parentChart.PlotRect.Width + 20 || 
                Height != (int)_parentChart.PlotRect.Height)
                SetRangeSelectorSize();
        }

        private void SetThumbShapes()
        {
            LowerThumbPath = new System.Drawing.Drawing2D.GraphicsPath();
            LowerThumbPath.AddArc(0, 0, 20, 20, 0, 360);
            LowerThumbPath.AddLine(9, 5, 9, 15);
            LowerThumbPath.CloseAllFigures();
            LowerThumbPath.AddLine(11, 5, 11, 15);
            LowerThumbPath.CloseAllFigures();
            UpperThumbPath = LowerThumbPath;
        }

        private void SetRangeSelectorSize()
        {
            this.Left = (int)_parentChart.PlotRect.Left - 10;
            this.Top = (int)_parentChart.PlotRect.Top;
            this.Width = (int)_parentChart.PlotRect.Width + 20;
            this.Height = (int)_parentChart.PlotRect.Height;
        }

        protected override void OnValueChanged(EventArgs e)
        {
            //do not change values
            if (LowerValue != UpperValue)
            {
                base.OnValueChanged(e);
                LargeChange = UpperValue - LowerValue;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var sliderSize = Maximum - Minimum;
            var _lowerThumbRect = LowerThumbPath.GetBounds();
            var _upperThumbRect = UpperThumbPath.GetBounds();
            var _workingArea = new Rectangle(ClientRectangle.X, ClientRectangle.Y + 2, ClientRectangle.Width, ClientRectangle.Height - 4);

            double onePartSize = sliderSize / (_workingArea.Width - (_lowerThumbRect.Width + _upperThumbRect.Width));

            double _lowerThumbRectX = LowerValue / onePartSize + _lowerThumbRect.Width / 2;
        }

        /// <summary>
        /// Removes itself from parent control
        /// </summary>
        public virtual void Remove()
        {
            _parentChart.AxisX.Min = _parentChart.AxisX.Max = Double.NaN;
            _parentChart.Paint -= ParentChart_Paint;
            if (this.Parent != null)
                this.Parent.Controls.Remove(this);
        }

    }
}
