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;

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

namespace SalesDashboard
{
    public sealed partial class SalesByCategory : UserControl
    {
        SolidColorBrush brush1 = new SolidColorBrush(Color.FromArgb(255, 2, 186, 2));
        SolidColorBrush brush2 = new SolidColorBrush(Color.FromArgb(255, 2, 140, 186));
        SolidColorBrush brush3 = new SolidColorBrush(Color.FromArgb(255, 2, 78, 186));
        SolidColorBrush[] customPalette = new SolidColorBrush[3];

        public SalesByCategory()
        {
            this.InitializeComponent();

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

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            c1Chart1.BeginUpdate();

            // set pie direction to counter clockwise
            PieOptions.SetDirection(c1Chart1, SweepDirection.Counterclockwise);

            // set pie inner radius
            c1Chart1.Data.Renderer = new PieRenderer() { InnerRadius = 0.1 };

            // set binding in code
            Data.SampleDataSource dataSource = this.DataContext as Data.SampleDataSource;
            if (dataSource != null)
            {
                c1Chart1.Data.ItemNames = dataSource.Categories;
            }
            c1Chart1.EndUpdate();
        }

        private void DataSeries_PlotElementLoaded_1(object sender, EventArgs e)
        {
            PlotElement pe = (PlotElement)sender;
            //pe.StrokeThickness = 0;
            pe.Tapped += pe_Tapped;
            pe.MouseLeftButtonDown += pe_MouseLeftButtonDown;
        }

        void pe_MouseLeftButtonDown(object sender, PointerRoutedEventArgs e)
        {
            PlotElement pe = (PlotElement)sender;
            if (pe.GetType() == typeof(PieSlice))
            {
                ExplodeSlice((PieSlice)pe, e.GetCurrentPoint(c1Chart1).Position);
            }
        }

        void pe_Tapped(object sender, TappedRoutedEventArgs e)
        {
            PlotElement pe = (PlotElement)sender;
            if (pe.GetType().ToString().EndsWith("Pie"))
            {
                ExplodeSlice((PieSlice)pe, e.GetPosition(c1Chart1));
            }
        }

        private void ExplodeSlice(PieSlice slice, Point location)
        {
            Canvas ChartCanvas = slice.Parent as Canvas;
            Border border = new Border();
            
            if (slice.Tag != null)
            {
                // implode slice
                slice.Offset = 0.0;
                SliceObject so = slice.Tag as SliceObject;
                ChartCanvas.Children.Remove(so.Label);
                slice.Tag = null;
            }
            else
            {
                // explode slice
                slice.Offset = 20;
                slice.Tag = new SliceObject() { IsExploded = true, Label = border };

                //create label
                DataPoint dp = slice.DataPoint;
                TextBlock tb = new TextBlock();
                tb.Foreground = new SolidColorBrush(Colors.Blue);
                tb.Text = (dp.PercentageSeries * 100).ToString("0.00") + "%";
                tb.Foreground = new SolidColorBrush(Colors.White);
                tb.FontSize = 26;
                border.Child = tb;
                //border.Padding = new Thickness(2);
                //border.Background = new SolidColorBrush(Colors.White);

                Canvas.SetLeft(border, location.X);
                Canvas.SetTop(border, location.Y);

                ChartCanvas.Children.Add(border);
            }
        }
    }

    /// <summary>
    /// SliceObject is used to store information about each slice and set to the pie slice's Tag property at runtime
    /// </summary>
    public class SliceObject
    {
        public bool IsExploded { get; set; }
        public Border Label { get; set; }
    }
}
