using C1.WPF.Chart;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Media;
using C1.Chart;

namespace FlexChartCustomization
{
    /// <summary>
    /// Interaction logic for LegendItems.xaml
    /// </summary>
    public partial class LegendItems : UserControl
    {
        Series defaultSeries, customSeries;
        public LegendItems()
        {
            InitializeComponent();
            customSeries = new SeriesWithPointLegendItems();
            customSeries.SeriesName = "Shipments";
            customSeries.SymbolRendering += VendorSeries_SymbolRendering;
            flexChart.Series.Add(customSeries);
        }

        private void VendorSeries_SymbolRendering(object sender, RenderSymbolEventArgs e)
        {
            e.Engine.SetFill(new SolidColorBrush(ViewModel.SmartPhoneVendors.ElementAt(e.Index).Color));
            e.Engine.SetStroke(new SolidColorBrush(ViewModel.SmartPhoneVendors.ElementAt(e.Index).Color));
        }

        public class SeriesWithPointLegendItems : Series, ISeries
        {
            /// <summary>
            /// Gets the name of legend.
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            string ISeries.GetLegendItemName(int index) { return ViewModel.SmartPhoneVendors.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 SolidColorBrush(ViewModel.SmartPhoneVendors.ElementAt(index).Color) };
            }

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

        private void ChbPointsLegends_Changed(object sender, System.Windows.RoutedEventArgs e)
        {
            if (flexChart == null) return;
            if (chbPointsLegends.IsChecked.HasValue && chbPointsLegends.IsChecked.Value)
            {
                flexChart.Series.Clear();
                flexChart.Series.Add(customSeries);
            }
            else
            {
                if (defaultSeries == null)
                {
                    defaultSeries = new Series();
                    defaultSeries.SeriesName = "Shipments";
                }
                flexChart.Series.Clear();
                flexChart.Series.Add(defaultSeries);
            }
        }
    }
}
