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 AxisGrouping : UserControl
    {
        string toFlat = "Switch to flat data";
        string toHierarchical = "Switch to hierarchical data";
        public AxisGrouping()
        {
            InitializeComponent();
            InitilizeControls();

            SetupChart();
        }

        void SetupChart()
        {
            flexChart1.BeginUpdate();
            flexChart1.Series.Clear();
            
            flexChart1.ChartType = ChartType.Column;
            flexChart1.BindingX = "Country";
            flexChart1.ToolTip.Content = "{x}\r{seriesName}\r{y}";
            flexChart1.Header.Content = "World GDP Ranking";
            flexChart1.Header.Style.Font = new System.Drawing.Font(FontFamily.GenericSansSerif, 15);

            SetToFlatData();
            flexChart1.AxisX.GroupSeparator = AxisGroupSeparator.Vertical;
            flexChart1.AxisY.Title = "GDP (billion USD)";

            var serCP = new Series();
            serCP.Binding = "CurrentPrices";
            serCP.Name = "GDP, current prices";
            flexChart1.Series.Add(serCP);

            var serPPP = new Series();
            serPPP.Binding = "PPPValuation";
            serPPP.Name = "GDP based on PPP valuation";
            flexChart1.Series.Add(serPPP);

            flexChart1.EndUpdate();
        }

        void SetToFlatData()
        {
            flexChart1.DataSource = CountryGDP.Countries();
            flexChart1.AxisX.GroupNames = "Continent";
            flexChart1.AxisX.GroupItemsPath = null;
        }

        void SetToHierarchicalData()
        {
            flexChart1.DataSource = HierarchicalGDP.Data();
            flexChart1.AxisX.GroupNames = "Continent";
            flexChart1.AxisX.GroupItemsPath = "Items";
        }

        private void InitilizeControls()
        {
            flexChart1 = baseSample1.flexChart1;
            baseSample1.lblTitle.Text = Localizer.GetItem("axisGrouping", "title");
            baseSample1.tbDescription.Rtf = Localizer.GetItem("axisGrouping", "description").MakeRtf();

            baseSample1.pControls.Controls.Clear();

            cbXGroupSeparator = new ComboBox()
            {
                Size = new Size(240, 21),
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.DimGray
            };
            cbXGroupSeparator.SelectedIndexChanged += CbXGroupSeparator_SelectedIndexChanged;
            foreach (var item in Enum.GetValues(typeof(AxisGroupSeparator)))
            {
                cbXGroupSeparator.Items.Add(new ComboBoxItem("AxisX Group Separator") { Text = ((AxisGroupSeparator)item).ToString(), Value = (int)item });
            }

            cbXGroupSeparator.SelectedIndex = 1;
            
            baseSample1.pControls.Controls.Add(cbXGroupSeparator);
        }

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

    }

    public class CountryGDP
    {
        public string Country { get; set; }
        public string Continent { get; set; }

        public double CurrentPrices { get; set; }
        public double PPPValuation { get; set; }

        public CountryGDP(string country, string continent, double currentPrices, double pppValuation)
        {
            Country = country;
            Continent = continent;
            CurrentPrices = currentPrices;
            PPPValuation = pppValuation;
        }

        public static List<CountryGDP> Countries()
        {
            List<CountryGDP> countries = new List<CountryGDP>();
            countries.Add(new CountryGDP("US", "America", 19362, 19362));
            countries.Add(new CountryGDP("China", "Asia", 11938, 23122));
            countries.Add(new CountryGDP("Japan", "Asia", 4884, 5405));
            countries.Add(new CountryGDP("Germany", "Europe", 3652, 4150));
            countries.Add(new CountryGDP("France", "Europe", 2575, 2826));
            countries.Add(new CountryGDP("UK", "Europe", 2565, 2880));
            countries.Add(new CountryGDP("India", "Asia", 2439, 9447));
            countries.Add(new CountryGDP("Brazil", "America", 2081, 3219));
            countries.Add(new CountryGDP("Italy", "Europe", 1921, 2307));
            countries.Add(new CountryGDP("Canada", "America", 1640, 1764));
            return countries;
        }
    }

    public class HierarchicalGDP
    {
        public string Country { get; set; }
        public string Continent { get; set; }

        public double CurrentPrices { get; set; }
        public double PPPValuation { get; set; }

        public HierarchicalGDP[] Items { get; set; }

        public static HierarchicalGDP[] Data()
        {
            return new HierarchicalGDP[] {
                new HierarchicalGDP { Continent = "Asia",
                    Items = new HierarchicalGDP[] {
                        new HierarchicalGDP{Country= "China", CurrentPrices=11938, PPPValuation=23122},
                        new HierarchicalGDP{Country= "Japan", CurrentPrices=4884, PPPValuation=5405},
                        new HierarchicalGDP{Country= "India", CurrentPrices=2439, PPPValuation=9447}
                    }
                },
                new HierarchicalGDP { Continent = "America",
                    Items = new HierarchicalGDP[] {
                        new HierarchicalGDP {Country= "US", CurrentPrices=19362, PPPValuation=19362},
                        new HierarchicalGDP{Country= "Brazil", CurrentPrices=2081, PPPValuation=3219},
                        new HierarchicalGDP{Country= "Canada", CurrentPrices=1640, PPPValuation=1764}
                    }
                },
                new HierarchicalGDP { Continent = "Europe",
                    Items = new HierarchicalGDP[] {
                        new HierarchicalGDP{Country= "Germany", CurrentPrices=3652, PPPValuation=4150},
                        new HierarchicalGDP{Country= "France", CurrentPrices=2575, PPPValuation=2826},
                        new HierarchicalGDP{Country= "UK", CurrentPrices=2565, PPPValuation=2880},
                        new HierarchicalGDP{Country= "Italy", CurrentPrices=1921, PPPValuation=2307}
                    }
                }
            };
        }
    }
}
