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

namespace FlexChartExplorer.Samples
{
    public partial class FlexPieEvents : UserControl
    {
        DataItem[] data;
        public FlexPieEvents()
        {
            InitializeComponent();
            InitializeControls();

            //Draw chart
            SetupChart();
        }

        void SetupChart()
        {
            flexPie1.BeginUpdate();

            var rnd = new Random();
            data = new DataItem[]{
                new DataItem { Name = "US", Value = rnd.Next(10, 100) },
                new DataItem { Name = "Germany", Value = rnd.Next(10, 100) },
                new DataItem { Name = "UK", Value = rnd.Next(10, 100) },
                new DataItem { Name = "Japan",Value = rnd.Next(10, 100) },
                new DataItem { Name = "Italy", Value = rnd.Next(10, 100) },
                new DataItem { Name = "Greece", Value = rnd.Next(10, 100) }
            }.OrderBy(x => x.Name).ToArray();


            // Set x-binding and add data to the chart
            flexPie1.Binding = "Value";
            flexPie1.BindingName = "Name";
            flexPie1.DataSource = data;

            //Set labels and legend
            flexPie1.DataLabel.Content = "{Name}";
            flexPie1.DataLabel.Position = PieLabelPosition.Center;
            flexPie1.Legend.Position = Position.None;

            flexPie1.EndUpdate();
        }

        private void InitializeControls()
        {
            #region Init controls

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

            baseSample1.pChart.Controls.Add(flexPie1);
            baseSample1.pControls.Visible = false;
            flexPie1.CustomPalette = new List<Brush>();
            flexPie1.SliceRendering += flexPie1_SliceRendering;
            flexPie1.DataLabel.Style.Font = new System.Drawing.Font(FontFamily.GenericSansSerif, 10);
            flexPie1.DataLabel.Border = true;

            //Set localized text
            baseSample1.lblTitle.Text = Localizer.GetItem("flexpieevents", "title");
            baseSample1.tbDescription.Rtf = Localizer.GetItem("flexpieevents", "description").MakeRtf();

            #endregion
        }

        void flexPie1_SliceRendering(object sender, RenderSliceEventArgs e)
        {
            //get color depending on value
            var dataOrderedByValue = data.OrderBy(x => x.Value).ToArray();
            var itemOrderedIndex = Array.IndexOf(dataOrderedByValue, data[e.Index]);
            // Use color #B1DCB6 (177, 250, 182) and color #F9F9F9 (249, 249, 249) which are defined in the suggested palette as the start color and end color.
            var colorStart = Color.FromArgb(177, 250, 182);
            var colorEnd = Color.FromArgb(249, 249, 249);
            var stepsCount = (data.Count() - 1);
            int paletteStepR = (int)Math.Round((double)((colorEnd.A - colorStart.R) / stepsCount));
            int paletteStepG = (int)Math.Round((double)((colorEnd.G - colorStart.G) / stepsCount));
            int paletteStepB = (int)Math.Round((double)((colorEnd.B - colorStart.B) / stepsCount));

            var color = colorStart;
            if (itemOrderedIndex < dataOrderedByValue.Length - 1)
                color = Color.FromArgb(colorEnd.A - itemOrderedIndex * paletteStepR, colorEnd.G - itemOrderedIndex * paletteStepG, colorEnd.B - itemOrderedIndex * paletteStepB);

            //use engine to paint the slice
            e.Engine.SetFill(color.ToArgb());
            e.Engine.SetStroke(color.ToArgb());
        }
        private class DataItem
        {
            public string Name { get; set; }
            public int Value { get; set; }
        }
    }
}
