using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using C1.Xaml.Chart;
using Windows.UI;
using Windows.UI.Xaml.Shapes;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace SalesDashboard
{
    public sealed partial class QuarterlySales : UserControl
    {
        SolidColorBrush brush1 = new SolidColorBrush(Color.FromArgb(255, 3, 154, 205));
        SolidColorBrush brush2 = new SolidColorBrush(Color.FromArgb(255, 2, 86, 205));
        SolidColorBrush[] customPalette = new SolidColorBrush[2];

        public QuarterlySales()
        {
            this.InitializeComponent();

            // set custom palette
            customPalette[0] = brush2;
            customPalette[1] = brush1;
            c1Chart1.CustomPalette = customPalette;

            // attach legend
            c1ChartLegend1.Chart = c1Chart1;
        } 

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            c1Chart1.BeginUpdate();
            c1Chart1.View.AxisY.MajorTickThickness = 0;
            c1Chart1.View.AxisY.MinorTickThickness = 0;
            c1Chart1.View.AxisY.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            c1Chart1.View.AxisY.AxisLine = new Windows.UI.Xaml.Shapes.Line() { StrokeThickness = 0 };
            c1Chart1.View.AxisY.Foreground = new SolidColorBrush(Colors.Transparent);
            c1Chart1.View.AxisX.MajorGridStrokeThickness = 0;
            c1Chart1.View.AxisX.MinorGridStrokeThickness = 0;
            c1Chart1.View.AxisX.MajorTickThickness = 0;
            c1Chart1.View.AxisX.MinorTickThickness = 0;
            c1Chart1.View.AxisX.FontSize = 16;

            // set binding in code because issue with XAML binding
            Data.SampleDataSource dataSource = this.DataContext as Data.SampleDataSource;
            if (dataSource != null)
            {
                foreach (DataSeries ds in dataSource.SalesByQuarter)
                {
                    ds.PointLabelTemplate = this.Resources["lbl"] as DataTemplate;
                    ds.PlotElementLoaded += ds_PlotElementLoaded;
                    c1Chart1.Data.Children.Add(ds);
                }
                c1Chart1.Data.ItemNames = dataSource.Quarters;
            }

            c1Chart1.EndUpdate();
        }

        void ds_PlotElementLoaded(object sender, EventArgs e)
        {
            Area area = sender as Area;
            if (area != null)
            {
                area.Opacity = 0.6;
                DataPoint point = area.DataPoint;
                int numberOfPoints = point.Series.Values.Count;

                PathGeometry geometry = area.Data as PathGeometry;
                PathFigure figure = geometry.Figures[0];
                PolyLineSegment segment = figure.Segments[0] as PolyLineSegment;
       
                Polyline line = new Polyline();
                line.Stroke = customPalette[point.SeriesIndex];
                line.StrokeThickness = 5;
                // get first number of points in series
                for (int i = 0; i < numberOfPoints; i++)
                {
                    line.Points.Add(new Point(segment.Points[i].X, segment.Points[i].Y - 2.5));
                }     
                //c1Chart1.View.Children.Add(line);
                // alternate way to add line
                ((Canvas)area.Parent).Children.Add(line);
            }
        }

        private void c1Chart1_Loaded(object sender, RoutedEventArgs e)
        {
            
        }
    }
}
