using C1.Xaml.Chart;
using System;
using System.Collections.Generic;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Shapes;

namespace ControlExplorer.Controls.Chart
{
    public partial class Animation
        : UserControl, IExposeApplicationBarItems
    {
        Dictionary<string, PlotElement> _dict = new Dictionary<string, PlotElement>();

        // Constructor
        public Animation()
        {
            InitializeComponent();

            Loaded += Animation_Loaded;
        }

        void Animation_Loaded(object sender, RoutedEventArgs e)
        {
            chart.View.AxisY.Min = 0;
            chart.View.AxisY.Max = 100;
            BarColumnOptions.SetRadiusX(chart, 2);
            BarColumnOptions.SetRadiusY(chart, 2);
            chart.ChartType = ChartType.Column;

            NewData();
        }

        Random rnd = new Random();

        void NewData()
        {
            // create new random data values
            int cnt = 12;
            double[] vals = new double[cnt];
            for (int i = 0; i < cnt; i++)
                vals[i] = rnd.Next(10, 100);

            DataSeries ds;
            if (chart.Data.Children.Count == 0)
            {
              ds = new DataSeries();
                ds.PlotElementLoaded += ds_PlotElementLoaded;
                chart.Data.Children.Add(ds);
            }
            chart.Data.Children[0].ValuesSource = vals;
        }

        void ds_PlotElementLoaded(object sender, EventArgs e)
        {
            var pe = (PlotElement)sender;
            string key = GetKey(pe);
            if (_dict.ContainsKey(key))
            {
                var pe0 = _dict[key];
                Rect rc0 = ((Path)pe0.Shape).Data.Bounds;
                Rect rc = ((Path)pe.Shape).Data.Bounds;

                if (rc0 != rc) // different values
                {
                    // transform to fit into the previous value
                    ScaleTransform st = new ScaleTransform()
                    {
                        ScaleX = rc0.Width / rc.Width,
                        ScaleY = rc0.Height / rc.Height
                    };
                    pe.RenderTransform = st;
                    pe.RenderTransformOrigin = new Point(0, 1);

                    // animate to the current value
                    Storyboard sb = new Storyboard() { Duration = new Duration(TimeSpan.FromSeconds(3)) };
                    DoubleAnimation da1 = new DoubleAnimation() { To = 1.0 };
                    Storyboard.SetTarget(da1, st);
                    Storyboard.SetTargetProperty(da1, "ScaleX");
                    sb.Children.Add(da1);
                    DoubleAnimation da2 = new DoubleAnimation() { To = 1.0 };
                    Storyboard.SetTarget(da2, st);
                    Storyboard.SetTargetProperty(da2, "ScaleY");
                    sb.Children.Add(da2);
                    sb.Begin();
                }
            }
            // store the previous element into the dictionary
            _dict[key] = pe;
            pe.Opacity = 1.0;
        }

        static string GetKey(PlotElement pe)
        {
            return string.Format("s{0}p{1}", pe.DataPoint.SeriesIndex, pe.DataPoint.PointIndex);
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            NewData();
        }

    
        #region IExposeApplicationBarItems Members

        public IEnumerable<ICommandBarElement> ApplicationBarItems
        {
            get 
            {
                var btn = new AppBarButton()
                {
                    Icon = new SymbolIcon(Windows.UI.Xaml.Controls.Symbol.Add),
                    Label = "New Data",
                    IsEnabled = true
                };
                btn.Click += btnNew_Click;

                yield return btn;
            }
        }

        #endregion

    }
}
