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.Chart;
using System.Drawing.Drawing2D;
using C1.Chart;

namespace FlexChartExplorer.Samples
{
    public partial class BoxPlotChart : UserControl
    {
        ComboBox cbCalculation;

        public BoxPlotChart()
        {
            InitializeComponent();
            InitializeControls();

            SetupChart();
        }

        void SetupChart()
        {
            flexChart1.BeginUpdate();
            flexChart1.DataSource = DataCreator.CreateForBoxWhisker();
            flexChart1.BindingX = "ClassName";
            flexChart1.ToolTip.Content = "{seriesName} - {name}\nMin: {Min}\nFirst Quartile: {FirstQuartile}\nMean: {Mean}\nMedian: {Median}\nThird Quartile: {ThirdQuartile}\nMax: {Max}";
            flexChart1.Series.Clear();
            
            Series serise = new BoxWhisker()
            {
                Name = "SchoolA",
                Binding = "SchoolA",
            };
            flexChart1.Series.Add(serise);

            serise = new BoxWhisker()
            {
                Name = "SchoolB",
                Binding = "SchoolB"
            };
            flexChart1.Series.Add(serise);

            serise = new BoxWhisker()
            {
                Name = "SchoolC",
                Binding = "SchoolC",
            };
            flexChart1.Series.Add(serise);

            flexChart1.EndUpdate();
        }
        private void InitializeControls()
        {
            #region Init controls
            baseSample1.lblTitle.Text = Localizer.GetItem("boxWhisker", "title");
            baseSample1.tbDescription.Rtf = Localizer.GetItem("boxWhisker", "description").MakeRtf();

            flexChart1 = baseSample1.flexChart1;
            baseSample1.pDescription.Height = 220;

            flexChart1.AxisX.MajorGrid = false;
            flexChart1.ChartType = C1.Chart.ChartType.Column;
            baseSample1.pControls.Visible = true;
            flexChart1.Series.Clear();

            cbCalculation = new ComboBox()
            {
                Size = new Size(260, 21),
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.DimGray
            };

            cbCalculation.SelectedIndexChanged += CbCalculation_SelectedIndexChanged;

            chbShowMeanLine = new CheckBox()
            {
                Text = "Show mean line",
                Size = new Size(120, 21),
                FlatStyle = FlatStyle.Flat,
                Checked = false
            };
            chbShowMeanLine.CheckedChanged += ChbShowMeanLine_CheckedChanged;

            chbShowMeanMarks = new CheckBox()
            {
                Text = "Show mean marks",
                FlatStyle = FlatStyle.Flat,
                Size = new Size(120, 21),
                Checked = true
            };
            chbShowMeanMarks.CheckedChanged += ChbShowMeanMarks_CheckedChanged;

            chbShowOutliers = new CheckBox()
            {
                Text = "Show outlier points",
                FlatStyle = FlatStyle.Flat,
                Size = new Size(120, 21),
                Checked = true
            };

            chbShowOutliers.CheckedChanged += ChbShowOutliers_CheckedChanged;

            chbShowInnerPoints = new CheckBox()
            {
                Text = "Show inner points",
                FlatStyle = FlatStyle.Flat,
                Size = new Size(120, 21),
                Checked = false
            };
            chbShowInnerPoints.CheckedChanged += ChbShowInnerPoints_CheckedChanged;

            chbRotate = new CheckBox()
            {
                Text = "Rotate",
                FlatStyle = FlatStyle.Flat,
                Size = new Size(80, 21),
                Checked = false
            };
            chbRotate.CheckedChanged += ChbRotate_CheckedChanged;

            foreach (var item in Enum.GetValues(typeof(QuartileCalculation)))
            {
                cbCalculation.Items.Add(new ComboBoxItem("Quartile Calculation") { Text = ((QuartileCalculation)item).ToString(), Value = (int)item });
            }
            cbCalculation.SelectedIndex = 1;

            baseSample1.pControls.Controls.Add(cbCalculation);
            baseSample1.pControls.Controls.Add(chbShowMeanLine);
            baseSample1.pControls.Controls.Add(chbShowMeanMarks);
            baseSample1.pControls.Controls.Add(chbShowOutliers);
            baseSample1.pControls.Controls.Add(chbShowInnerPoints);
            baseSample1.pControls.Controls.Add(chbRotate);
            #endregion
        }

        private void ChbRotate_CheckedChanged
            (object sender, EventArgs e)
        {
            flexChart1.Rotated = chbRotate.Checked;
        }

        private void ChbShowInnerPoints_CheckedChanged(object sender, EventArgs e)
        {
            foreach (var series in flexChart1.Series)
            {
                ((BoxWhisker)series).ShowInnerPoints = chbShowInnerPoints.Checked;
            }
        }

        private void ChbShowOutliers_CheckedChanged(object sender, EventArgs e)
        {
            foreach (var series in flexChart1.Series)
            {
                ((BoxWhisker)series).ShowOutliers = chbShowOutliers.Checked;
            }
        }

        private void ChbShowMeanMarks_CheckedChanged(object sender, EventArgs e)
        {
            foreach (var series in flexChart1.Series)
            {
                ((BoxWhisker)series).ShowMeanMarks = chbShowMeanMarks.Checked;
            }
        }

        private void ChbShowMeanLine_CheckedChanged(object sender, EventArgs e)
        {
            foreach (var series in flexChart1.Series)
            {
                ((BoxWhisker)series).ShowMeanLine = chbShowMeanLine.Checked;
            }
        }

        private void CbCalculation_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (var series in flexChart1.Series)
            {
                ((BoxWhisker)series).QuartileCalculation = (QuartileCalculation)Enum.Parse(typeof(QuartileCalculation), (cbCalculation.SelectedItem as ComboBoxItem).Text);
            }
        }
    }
}
