using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using C1.Win.Chart;
using System.Linq;

namespace DrillDown.Samples
{
    public partial class ColumnPie : UserControl
    {
        DrillDownManager manager;
        bool isFlexChartShown = true;
        
        public ColumnPie()
        {
            InitializeComponent();
            InitializeControls();
            SetupChart();
        }

        void SetupChart()
        {
            Queue<string> drilldownPath = new Queue<string>();
            drilldownPath.Enqueue("Year");
            drilldownPath.Enqueue("Country");

            List<Item> items = DataService.Instance.GetData(500);
            manager = new DrillDownManager(flexChart1, items, drilldownPath);
            cbHistory.DataSource = manager.History.ToList();
            cbHistory.DisplayMember = "Name";
            manager.OnDrillDownChanged += (o, e) =>
            {
                cbHistory.DataSource = manager.History.ToList();
                cbHistory.DisplayMember = "Name";
            };

            flexChart1.BeginUpdate();
            flexChart1.Name = "flexChart1";
            flexChart1.ToolTip.Content = "{value}";
            flexChart1.Stacking = C1.Chart.Stacking.Stacked;
            // Set x-binding and add data to the chart
            flexChart1.BindingX = "Name";
            flexChart1.Binding = "Value";
            flexChart1.Series.Clear();
            flexChart1.Series.Add(new Series() { Binding = "Value" });
            flexChart1.DataSource = manager.CurrentItems.Views;
            flexChart1.DataLabel.Style.Font = new System.Drawing.Font(FontFamily.GenericSansSerif, 10);
            flexChart1.DataLabel.Position = C1.Chart.LabelPosition.Top;
            flexChart1.DataLabel.Content = "{y}";
            flexChart1.MouseClick += ChartMouseClick;
            flexChart1.EndUpdate();
            
            flexPie1.BeginUpdate();
            flexPie1.Name = "flexPie1";
            flexPie1.ToolTip.Content = "{value}";
            // Set x-binding and add data to the chart
            flexPie1.Binding = "Value";
            flexPie1.BindingName = "Name";
            flexPie1.DataLabel.Position = C1.Chart.PieLabelPosition.Center;
            flexPie1.DataLabel.Border = true;
            flexPie1.DataLabel.Content = "{name} | {y}";
            flexPie1.DataSource = manager.CurrentItems.Views;
            flexPie1.MouseClick += ChartMouseClick;
            flexPie1.EndUpdate();
        }

        private void InitializeControls()
        {
            //Set localized content
            baseSample1.lblTitle.Text = Localizer.GetItem("columnpie", "title");
            baseSample1.tbDescription.Rtf = Localizer.GetItem("columnpie", "description").MakeRtf();
            
            cbChartType = new ComboBox()
            {
                Size = new Size(200, 21),
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.DimGray
            };
            cbAggregateType = new ComboBox()
            {
                Size = new Size(200, 21),
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.DimGray
            };
            cbGroupBy = new ComboBox()
            {
                Size = new Size(200, 21),
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.DimGray
            };
            btnGo = new Button()
            {
                Text = "Go",
                Width = 100,
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.DimGray,
                AutoSize = true
            };
            cbHistory = new ComboBox()
            {
                Size = new Size(200, 21),
                FlatStyle = FlatStyle.Flat,
                ForeColor = Color.DimGray
            };

            flexChart1 = baseSample1.flexChart1;

            flexPie1 = new FlexPie()
            {
                Dock = DockStyle.Fill,
                Visible = true,
                BackColor = Color.White,
            };

            btnGo.Click += (o, e) =>
            {
                DrillDownHistory history = cbHistory.SelectedItem as DrillDownHistory;
                manager.GoBackTo(history);
                UpdateChart();
            };

            foreach (var item in Enum.GetValues(typeof(AggregateType)))
                cbAggregateType.Items.Add(new ComboBoxItem("Aggregate Type") { Text = ((AggregateType)item).ToString(), Value = (int)item });
            cbAggregateType.SelectedIndex = 0;

            cbAggregateType.SelectedIndexChanged += (o, e) =>
            {
                manager.AggregateType = (AggregateType)Enum.Parse(typeof(AggregateType), (cbAggregateType.SelectedItem as ComboBoxItem).Text);
                UpdateChart();
            };

            cbChartType.Items.Add(new ComboBoxItem("Chart Type") { Text = "Column" });
            cbChartType.Items.Add(new ComboBoxItem("Chart Type") { Text = "Pie" });
            cbChartType.SelectedIndex = 0;
            cbChartType.SelectedIndexChanged += (o, e) =>
            {
                if ((cbChartType.SelectedItem as ComboBoxItem).Text == "Column")
                {
                    isFlexChartShown = true;
                    baseSample1.pChart.Controls.Clear();
                    baseSample1.pChart.Controls.Add(flexChart1);
                }
                else
                {
                    isFlexChartShown = false;
                    baseSample1.pChart.Controls.Clear();
                    baseSample1.pChart.Controls.Add(flexPie1);
                }
                UpdateChart();
            };

            cbGroupBy.Items.Add(new ComboBoxItem("Group By") { Text = "Country, City", Value = 0 });
            cbGroupBy.Items.Add(new ComboBoxItem("Group By") { Text = "Country, Year", Value = 1 });
            cbGroupBy.Items.Add(new ComboBoxItem("Group By") { Text = "Year, Country", Value = 2 });
            cbGroupBy.Items.Add(new ComboBoxItem("Group By") { Text = "Country, City, Year", Value = 3 });
            cbGroupBy.Items.Add(new ComboBoxItem("Group By") { Text = "Year, Country, City", Value = 4 });
            cbGroupBy.SelectedIndex = 2;
            cbGroupBy.SelectedIndexChanged += (o, e) =>
            {
                Queue<string> drilldownPath = new Queue<string>();
                var selectedText = (cbGroupBy.SelectedItem as ComboBoxItem).Text;
                var groups = selectedText.Split(',');
                foreach (var group in groups)
                {
                    drilldownPath.Enqueue(group.Trim());
                }
                manager.DrillDownPath = drilldownPath;
                UpdateChart();
            };

            baseSample1.pControls.Controls.Add(cbHistory);
            baseSample1.pControls.Controls.Add(btnGo);
            baseSample1.pControls.Controls.Add(cbAggregateType);
            baseSample1.pControls.Controls.Add(cbChartType);
            baseSample1.pControls.Controls.Add(cbGroupBy);
        }

        private void ChartMouseClick(object sender, MouseEventArgs e)
        {
            var flexChart = sender as FlexChartBase;
            var hitInfo = flexChart.HitTest(new Point(e.X, e.Y));
            if (hitInfo != null && hitInfo.Distance < 2)
            {
                if (manager != null)
                {
                    manager.DrillDown(hitInfo.PointIndex);
                    UpdateChart();
                }
            }
        }

        private void UpdateChart()
        {
            if (isFlexChartShown)
            {
                flexChart1.DataSource = manager.CurrentItems.Views;
            }
            else
            {
                flexPie1.Enabled = false;
                flexPie1.DataSource = manager.CurrentItems.Views;
                flexPie1.Enabled = true;
            }
        }
    }
}
