using C1.Xaml.Chart;
using System;
using Windows.Devices.Input;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;

namespace ControlExplorer.Controls.Chart
{
    public partial class StyledChart : UserControl
    {
        bool loaded = false;
        TransformGroup tg;
        SampleData sampleData;

        public StyledChart()
        {
            InitializeComponent();

            sampleData = new SampleData(DataSeries_Loaded);

            tg = new TransformGroup();
            tg.Children.Add(new ScaleTransform() { ScaleX = 0, ScaleY = 0.3 });
            c1chart.Data = sampleData.DefaultData;
        }
        public ChartType ChartType
        {
            get { return c1chart.ChartType; }
            set
            {
                if (value != c1chart.ChartType)
                {
                    // several chart requires special data
                    if (value == ChartType.HighLowOpenClose || value == ChartType.Candle)
                        c1chart.Data = sampleData.FinancialData;
                    else if (value == ChartType.Bubble)
                        c1chart.Data = sampleData.BubbleData;
                    else if (value == ChartType.Gantt)
                        c1chart.Data = sampleData.GanttData;
                    else
                        c1chart.Data = sampleData.DefaultData;

                    StartAnimation();
                    c1chart.ChartType = value;
                }
            }
        }

        public Palette Palette
        {
            get { return c1chart.Palette; }
            set
            {
                StartAnimation();
                c1chart.Palette = value;
            }
        }

        public ChartTheme Theme
        {
            get { return c1chart.Theme; }
            set
            {
                StartAnimation();
                c1chart.Theme = value;
            }
        }

        private void StartAnimation()
        {
            if (loaded)
            {
                ((ScaleTransform)tg.Children[0]).ScaleX = 0;
                ((ScaleTransform)tg.Children[0]).ScaleY = 0.3;
                Storyboard sb = (Storyboard)Resources["sb"];
                sb.Stop();
                Storyboard.SetTarget(sb, tg);
                sb.Begin();
            }
        }

        private void c1chart_Loaded(object sender, RoutedEventArgs e)
        {
            loaded = true;
            StartAnimation();
        }

        private void DataSeries_Loaded(object sender, EventArgs e)
        {
            // DataSeries.Loaded fires whenever plot element related
            // to the series is loaded
            PlotElement pe = sender as PlotElement;
            if (pe != null)
            {
                pe.RenderTransform = tg;
                pe.RenderTransformOrigin = new Point(0.5, 0.5);

                pe.PointerEntered += OnPointerEntered;
                pe.PointerExited += OnPointerExited;
            }
        }

        void OnPointerExited(object sender, PointerRoutedEventArgs e)
        {
            PlotElement pe = sender as PlotElement;
            if (pe != null)
            {
                Canvas.SetZIndex(pe, 0);
                Storyboard sb = (Storyboard)Resources["sbEnter"];
                sb.Stop();

                sb = (Storyboard)Resources["sbLeave"];
                sb.Stop();
                Storyboard.SetTarget(sb, pe.Shape);
                sb.Begin();

                //pe.Shape.OpacityMask = null;
            }
        }

        void OnPointerEntered(object sender, PointerRoutedEventArgs e)
        {
            PlotElement pe = sender as PlotElement;
            if (pe != null)
            {
                //pe.Shape.OpacityMask = (Brush)Resources["mask"];

                if (!(pe is Area))
                    Canvas.SetZIndex(pe, 1);

                Storyboard sb = (Storyboard)Resources["sbEnter"];
                sb.Stop();
                Storyboard.SetTarget(sb, pe.Shape);
                sb.Begin();
            }
        }
    }
}
