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 ChartSamples
{
    public sealed partial class PieSliceExplode : UserControl
    {
        public PieSliceExplode()
        {
            this.InitializeComponent();
            this.Loaded += PieSliceExplode_Loaded;
        }

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

            //disable interaction on the parent FlipView
            c1Chart1.ManipulationMode = ManipulationModes.All;

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

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

            c1Chart1.EndUpdate();
        }

        private void DataSeries_PlotElementLoaded(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.Text = (dp.PercentageSeries * 100).ToString("0.00") + "%";
                tb.Foreground = new SolidColorBrush(Colors.Black);
                tb.FontSize = 26;
                tb.IsHitTestVisible = false;
                border.Child = tb;
                border.Background = new SolidColorBrush(Colors.LightGray);
                border.CornerRadius = new CornerRadius(5);
                border.Opacity = 0.8;
                border.IsHitTestVisible = false;
                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; }
    }
}
