using System.Drawing;
using System.Windows.Forms;
using C1.Win.Chart;
using C1.Chart;
using System;

namespace FlexChartExplorer.Samples
{
    public partial class RangedHistogram : UserControl
    {
        C1.Win.Chart.RangedHistogram histogramSeries;

        public RangedHistogram()
        {
            InitializeComponent();
            SetupChart();
            InitializeControls();
        }

        void SetupChart()
        {
            chart = new FlexChart()
            {
                Dock = DockStyle.Fill,
                Visible = true,
                BackColor = Color.White,
            };
            chart.BeginUpdate();
            chart.Series.Clear();
            histogramSeries = new C1.Win.Chart.RangedHistogram();
            chart.ChartType = ChartType.RangedHistogram;
            chart.Series.Add(histogramSeries);
            chart.Rendered += Chart_Rendered;
            
            histogramSeries.Binding = "Value";
            histogramSeries.DataSource = new[]
            {
                new { Name = "A", Value = 20 },
                new { Name = "B", Value = 35 },
                new { Name = "C", Value = 40 },
                new { Name = "A", Value = 55 },
                new { Name = "B", Value = 80 },
                new { Name = "C", Value = 60 },
                new { Name = "A", Value = 61 },
                new { Name = "B", Value = 85 },
                new { Name = "C", Value = 80 },
                new { Name = "A", Value = 64 },
                new { Name = "B", Value = 80 },
                new { Name = "C", Value = 75 },
                new { Name = "A", Value = 1222 },
                new { Name = "B", Value = 133 },
            };
            chart.EndUpdate();
        }

        private void Chart_Rendered(object sender, RenderEventArgs e)
        {
            tbOverflow.Text = histogramSeries.OverflowBin.ToString("0");
            tbUnderflow.Text = histogramSeries.UnderflowBin.ToString("0");
            tbBinWidth.Text = histogramSeries.BinWidth.ToString("0");
            tbBinNum.Text = histogramSeries.NumberOfBins.ToString();
        }

        private void InitializeControls()
        {
            #region Init controls
            //Set localized text
            baseSample1.lblTitle.Text = Localizer.GetItem("rangedhistogram", "title");
            baseSample1.tbDescription.Rtf = Localizer.GetItem("rangedhistogram", "description").MakeRtf();

            baseSample1.pChart.Controls.Clear();
            baseSample1.pChart.Controls.Add(chart);


            chbByCategory = new CheckBox()
            {
                Text = "By Category",
                FlatStyle = FlatStyle.Flat,
                Checked = false
            };
            chbByCategory.CheckedChanged += ChbByCategory_CheckedChanged;

            chbOverflow = new CheckBox()
            {
                Text = "Overflow bin",
                FlatStyle = FlatStyle.Flat,
                Checked = false
            };
            chbOverflow.CheckedChanged += ChbOverflow_CheckedChanged;
            tbOverflow = new TextBox();
            tbOverflow.TextChanged += TbOverflow_TextChanged;
            tbOverflow.KeyPress += NumericInputCheck;
            tbOverflow.Enabled = false;

            chbUnderflow = new CheckBox()
            {
                Text = "Underflow bin",
                FlatStyle = FlatStyle.Flat,
                Checked = false
            };
            chbUnderflow.CheckedChanged += ChbUnderflow_CheckedChanged;
            tbUnderflow = new TextBox();
            tbUnderflow.TextChanged += TbUnderflow_TextChanged;
            tbUnderflow.KeyPress += NumericInputCheck;
            tbUnderflow.Enabled = false;

            lblBinWidth = new Label()
            {
                Text = "BinWidth:",
                Size = new Size(80, 21),
                TextAlign = ContentAlignment.BottomRight,
            };
            tbBinWidth = new TextBox();
            tbBinWidth.Enabled = false;
            tbBinWidth.KeyPress += NumericInputCheck;
            tbBinWidth.TextChanged += TbBinWidth_TextChanged;

            lblBinNum = new Label()
            {
                Text = "Number of Bins:",
                Size = new Size(150, 21),
                TextAlign = ContentAlignment.BottomRight,
            };
            tbBinNum = new TextBox();
            tbBinNum.Enabled = false;
            tbBinNum.KeyPress += NumericInputCheck;
            tbBinNum.TextChanged += TbBinNum_TextChanged;

            cbMode = new ComboBox()
            {
                Size = new Size(200, 21),
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.DimGray
            };
            foreach (var item in Enum.GetValues(typeof(C1.Chart.HistogramBinning)))
                cbMode.Items.Add(new ComboBoxItem("Mode") { Text = ((C1.Chart.HistogramBinning)item).ToString(), Value = (int)item });
            cbMode.SelectedIndexChanged += CbMode_SelectedIndexChanged;
            cbMode.SelectedIndex = 0;

            baseSample1.pControls.Controls.Add(chbByCategory);
            baseSample1.pControls.Controls.Add(cbMode);
            baseSample1.pControls.Controls.Add(chbOverflow);
            baseSample1.pControls.Controls.Add(tbOverflow);
            baseSample1.pControls.Controls.Add(chbUnderflow);
            baseSample1.pControls.Controls.Add(tbUnderflow);
            baseSample1.pControls.Controls.Add(lblBinWidth);
            baseSample1.pControls.Controls.Add(tbBinWidth);
            baseSample1.pControls.Controls.Add(lblBinNum);
            baseSample1.pControls.Controls.Add(tbBinNum);

            #endregion
        }

        private void ChbByCategory_CheckedChanged(object sender, EventArgs e)
        {
            if (chbByCategory.Checked)
            {
                // To set BindingX property to a valid value will switch the histogram to 'By Category' mode.
                // In this mode, the histogram will group the same categories and sum the values in the value axis.
                histogramSeries.BindingX = "Name";
                cbMode.Enabled = false;
                chbOverflow.Enabled = false;
                tbOverflow.Enabled = false;
                chbUnderflow.Enabled = false;
                tbUnderflow.Enabled = false;
                tbBinWidth.Enabled = false;
                tbBinNum.Enabled = false;
            }
            else
            {
                histogramSeries.BindingX = "";
                cbMode.Enabled = true;
                chbOverflow.Enabled = true;
                tbOverflow.Enabled = chbOverflow.Checked;
                chbUnderflow.Enabled = true;
                tbUnderflow.Enabled = chbUnderflow.Checked;
                tbBinWidth.Enabled = histogramSeries.BinMode == HistogramBinning.BinWidth;
                tbBinNum.Enabled = histogramSeries.BinMode == HistogramBinning.NumberOfBins;
            }
        }

        private void TbBinNum_TextChanged(object sender, EventArgs e)
        {
            int result;
            if (Int32.TryParse(tbBinNum.Text, out result))
            {
                histogramSeries.NumberOfBins = result;
            }
        }

        private void TbBinWidth_TextChanged(object sender, EventArgs e)
        {
            double result;
            if (Double.TryParse(tbBinWidth.Text, out result))
            {
                histogramSeries.BinWidth = result;
            }
        }

        private void UpdateOverflowBin()
        {
            double result;
            if (chbOverflow.Checked && Double.TryParse(tbOverflow.Text, out result))
            {
                histogramSeries.OverflowBin = result;
            }
        }

        private void UpdateUnderflowBin()
        {
            double result;
            if (chbUnderflow.Checked && Double.TryParse(tbUnderflow.Text, out result))
            {
                histogramSeries.UnderflowBin = result;
            }
        }

        private void TbUnderflow_TextChanged(object sender, EventArgs e)
        {
            UpdateUnderflowBin();
        }

        private void TbOverflow_TextChanged(object sender, EventArgs e)
        {
            UpdateOverflowBin();
        }

        private void ChbUnderflow_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chbUnderflow.Checked)
            {
                tbUnderflow.Enabled = true;
                histogramSeries.ShowUnderflowBin = true;
                UpdateUnderflowBin();
            }
            else
            {
                histogramSeries.ShowUnderflowBin = false;
                tbUnderflow.Enabled = false;
            }
        }

        private void ChbOverflow_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chbOverflow.Checked)
            {
                tbOverflow.Enabled = true;
                histogramSeries.ShowOverflowBin = true;
                UpdateOverflowBin();
            }
            else
            {
                histogramSeries.ShowOverflowBin = false;
                tbOverflow.Enabled = false;
            }
        }

        private void NumericInputCheck(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void CbMode_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            histogramSeries.BinMode =
                (C1.Chart.HistogramBinning)Enum.Parse(typeof(C1.Chart.HistogramBinning), (cbMode.SelectedItem as ComboBoxItem).Text);
            switch (histogramSeries.BinMode)
            {
                case HistogramBinning.Automatic:
                    tbBinWidth.Enabled = false;
                    tbBinNum.Enabled = false;
                    break;
                case HistogramBinning.BinWidth:
                    tbBinWidth.Enabled = true;
                    tbBinNum.Enabled = false;
                    break;
                case HistogramBinning.NumberOfBins:
                    tbBinNum.Enabled = true;
                    tbBinWidth.Enabled = false;
                    break;
            }
        }
    }
}
