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;
using System.IO;

namespace FlexChartExplorer.Samples
{
    public partial class NumericAxisGrouping : UserControl
    {
        public NumericAxisGrouping()
        {
            InitializeComponent();
            InitilizeControls();
            SetupChart();
        }

        void SetupChart()
        {
            flexChart1.BeginUpdate();
            
            flexChart1.ChartType = ChartType.SplineSymbols;
            flexChart1.DataSource = TemperatureRecord.Records();
            flexChart1.BindingX = "Month";
            flexChart1.Binding = "Temperature";
            flexChart1.AxisY.GroupProvider = new MyGroupProvider();
            flexChart1.AxisY.GroupStyle.StrokeWidth = 2;
            //flexChart1.AxisY.Position = Position.Right;
            flexChart1.AxisY.Title = "Temperature in Celsius";
            flexChart1.AxisY.Min = 0;
            flexChart1.AxisY.Max = 40;
            flexChart1.Series.Clear();
            var series = new Series();
            flexChart1.Series.Add(series);
            flexChart1.EndUpdate();
        }

        class MyGroupProvider : IAxisGroupProvider
        {
            public int GetLevels(IRange range)
            {
                return 1;
            }

            public IList<IRange> GetRanges(IRange range, int level)
            {
                var ranges = new List<IRange>();
                if (level == 1)
                {
                    ranges.Add(new DoubleRange("Low", 0, 10));
                    ranges.Add(new DoubleRange("Medium", 10, 25));
                    ranges.Add(new DoubleRange("High", 25, 40));
                }
                return ranges;
            }
        }

        class TemperatureRecord
        {
            public string Month { get; set; }
            public double Temperature { get; set; }
            public TemperatureRecord(string month, double temperature)
            {
                Month = month;
                Temperature = temperature;
            }
            public static List<TemperatureRecord> Records()
            {
                var records = new List<TemperatureRecord>();
                records.Add(new TemperatureRecord("Jan", 1));
                records.Add(new TemperatureRecord("Feb", 5));
                records.Add(new TemperatureRecord("Mar", 2.4));
                records.Add(new TemperatureRecord("Apr", 8));
                records.Add(new TemperatureRecord("May", 30));
                records.Add(new TemperatureRecord("Jun", 17.5));
                records.Add(new TemperatureRecord("Jul", 32.8));
                records.Add(new TemperatureRecord("Aug", 20));
                records.Add(new TemperatureRecord("Sep", 16));
                records.Add(new TemperatureRecord("Oct", 10));
                records.Add(new TemperatureRecord("Nov", 4.6));
                records.Add(new TemperatureRecord("Dec", 5));
                return records;
            }
        }

        private void InitilizeControls()
        {
            flexChart1 = baseSample1.flexChart1;
            baseSample1.lblTitle.Text = Localizer.GetItem("numericAxisGrouping", "title");
            baseSample1.tbDescription.Rtf = Localizer.GetItem("numericAxisGrouping", "description").MakeRtf();
            cbYGroupSeparator = new ComboBox()
            {
                Size = new Size(240, 21),
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.DimGray
            };
            foreach (var item in Enum.GetValues(typeof(AxisGroupSeparator)))
            {
                cbYGroupSeparator.Items.Add(new ComboBoxItem("AxisY Group Separator") { Text = ((AxisGroupSeparator)item).ToString(), Value = (int)item });
            }
            cbYGroupSeparator.SelectedIndexChanged += CbYGroupSeparator_SelectedIndexChanged;
            cbYGroupSeparator.SelectedIndex = 2;
            baseSample1.pControls.Controls.Clear();
            baseSample1.pControls.Controls.Add(cbYGroupSeparator);
        }

        private void CbYGroupSeparator_SelectedIndexChanged(object sender, EventArgs e)
        {
            int idx = cbYGroupSeparator.SelectedIndex;
            switch (idx)
            {
                case 0:
                    flexChart1.AxisY.GroupSeparator = AxisGroupSeparator.None;
                    break;
                case 1:
                    flexChart1.AxisY.GroupSeparator = AxisGroupSeparator.Vertical;
                    break;
                case 2:
                    flexChart1.AxisY.GroupSeparator = AxisGroupSeparator.Horizontal;
                    break;
                case 3:
                    flexChart1.AxisY.GroupSeparator = AxisGroupSeparator.Grid;
                    break;
            }
        }
    }
}
