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

namespace FlexChartCustomization.Samples
{
    public partial class LegendItems : UserControl
    {
        static List<SmartPhoneVendor> vendors = SmartPhoneVendors();
        Series defaultSeries, customSeries;
        public LegendItems()
        {
            InitializeComponent();
            InitializeControls();
            SetupChart();
        }

        void SetupChart()
        {
            flexChart1.BeginUpdate();

            flexChart1.Series.Clear();
            flexChart1.DataSource = vendors;
            flexChart1.BindingX = "Name";
            flexChart1.Binding = "Shipments";
            flexChart1.Header.Style.Font = new Font(FontFamily.GenericSansSerif, 15);
            flexChart1.Header.Content = "2016 Top Smartphone Vendors";
            customSeries = new SeriesWithPointLegendItems();
            customSeries.Name = "Shipments";
            customSeries.SymbolRendering += VendorSeries_SymbolRendering;
            flexChart1.Series.Add(customSeries);
            flexChart1.ToolTip.Content = "{seriesName}\r\n{value}";
            flexChart1.EndUpdate();
        }

        private void VendorSeries_SymbolRendering(object sender, RenderSymbolEventArgs e)
        {
            // Customize the color of the columns.
            e.Engine.SetFill(new SolidBrush(vendors.ElementAt(e.Index).Color));
            e.Engine.SetStroke(new SolidBrush(Color.FromArgb(200, vendors.ElementAt(e.Index).Color)));
        }

        private void InitializeControls()
        {
            //Set localized content
            baseSample1.lblTitle.Text = Localizer.GetItem("legenditems", "title");
            baseSample1.tbDescription.Rtf = Localizer.GetItem("legenditems", "description").MakeRtf();
            flexChart1 = baseSample1.flexChart1;
            chbPointsLegends = new CheckBox()
            {
                Text = "Display Series Points in Legend",
                FlatStyle = FlatStyle.Flat,
                Checked = true,
                AutoSize = true
            };
            chbPointsLegends.CheckedChanged += ChbPointsLegends_CheckedChanged;

            baseSample1.pControls.Controls.Add(chbPointsLegends);
        }

        private void ChbPointsLegends_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chbPointsLegends.Checked)
            {
                flexChart1.Series.Clear();
                flexChart1.Series.Add(customSeries);
            }
            else
            {
                if (defaultSeries == null)
                {
                    defaultSeries = new Series();
                    defaultSeries.Name = "Shipments";
                }
                flexChart1.Series.Clear();
                flexChart1.Series.Add(defaultSeries);
            }

        }

        public class SeriesWithPointLegendItems : Series, ISeries
        {
            /// <summary>
            /// Gets the name of legend.
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            string ISeries.GetLegendItemName(int index) { return vendors.ElementAt(index).Name; }

            /// <summary>
            /// Gets the style of legend.
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            _Style ISeries.GetLegendItemStyle(int index)
            {
                return new _Style { Fill = new SolidBrush(vendors.ElementAt(index).Color) };
            }

            /// <summary>
            /// Get the number of series items in the legend.
            /// </summary>
            int ISeries.GetLegendItemLength() { return vendors.Count; }
        }

        private static List<SmartPhoneVendor> SmartPhoneVendors()
        {
            List<SmartPhoneVendor> vendors = new List<SmartPhoneVendor>();

            vendors.Add(new SmartPhoneVendor()
            {
                Name = "Apple",
                Color = Color.FromArgb(136, 189, 230),
                Shipments = 215.2
            });
            vendors.Add(new SmartPhoneVendor()
            {
                Name = "Huawei",
                Color = Color.FromArgb(251, 178, 88),
                Shipments = 139
            });
            vendors.Add(new SmartPhoneVendor()
            {
                Name = "Lenovo",
                Color = Color.FromArgb(144, 205, 151),
                Shipments = 50.7
            });
            vendors.Add(new SmartPhoneVendor()
            {
                Name = "LG",
                Color = Color.FromArgb(246, 170, 201),
                Shipments = 55.1
            });
            vendors.Add(new SmartPhoneVendor()
            {
                Name = "Oppo",
                Color = Color.FromArgb(191, 165, 84),
                Shipments = 92.5
            });
            vendors.Add(new SmartPhoneVendor()
            {
                Name = "Samsung",
                Color = Color.FromArgb(188, 153, 199),
                Shipments = 310.3
            });
            vendors.Add(new SmartPhoneVendor()
            {
                Name = "Vivo",
                Color = Color.FromArgb(237, 221, 70),
                Shipments = 71.7
            });
            vendors.Add(new SmartPhoneVendor()
            {
                Name = "Xiaomi",
                Color = Color.FromArgb(240, 126, 110),
                Shipments = 61
            });
            vendors.Add(new SmartPhoneVendor()
            {
                Name = "ZTE",
                Color = Color.FromArgb(140, 140, 140),
                Shipments = 61.9
            });

            return vendors;
        }

        public class SmartPhoneVendor
        {
            public string Name { get; set; }
            public double Shipments { get; set; }
            public Color Color { get; set; }
        }
    }
}
