using C1.Xaml.Chart;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;

namespace ControlExplorer.Controls.Chart
{
    public partial class Dynamic
        : UserControl, IExposeApplicationBarItems
    {
        ObservableCollection<Point> _pts = new ObservableCollection<Point>();
        int counter = 0;
        Random rnd = new Random();
        DispatcherTimer dt;
        int nMaxPoints = 40;
        int nAddPoints = 1;

        public Dynamic()
        {
            InitializeComponent();

            chart.DataContext = _pts;
            chart.ChartType = ChartType.Line;

            XYDataSeries ds = new XYDataSeries()
            {
                XValueBinding = new Binding { Path = new PropertyPath("X") },
                ValueBinding = new Binding { Path = new PropertyPath("Y") },
                ConnectionStrokeThickness = 1,
                Label = "raw",
                ConnectionStroke = new SolidColorBrush(Colors.DarkBlue)
            };

            chart.Data.Children.Add(ds);

            chart.View.AxisY.Min = -1000;
            chart.View.AxisY.Max = 1000;

            dt = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(0.2) };
            dt.Tick += (s, e) => Update();
            dt.Start();


            btnTimer.Click += Button_Click;
            UpdateButton();
        }

        void Update()
        {
            chart.BeginUpdate();

            int cnt = nAddPoints;
            for (int i = 0; i < cnt; i++)
            {
                double r = rnd.NextDouble();
                double y = (10 * r * Math.Sin(0.1 * counter) * Math.Sin(0.6 * rnd.NextDouble() * counter));
                _pts.Add(new Point(counter++, y * 100));
            }

            int ndel = _pts.Count - nMaxPoints;
            if (ndel > 0)
                for (int i = 0; i < ndel; i++)
                    _pts.RemoveAt(0);

            chart.EndUpdate();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            UpdateButton();
        }

        private void UpdateButton()
        {
            if (dt.IsEnabled)
            {
                dt.Stop();
                btnTimer.Label = "Start";
                btnTimer.Icon = new SymbolIcon(Windows.UI.Xaml.Controls.Symbol.Play);
            }
            else
            {
                dt.Start();
                btnTimer.Label = "Pause";
                btnTimer.Icon = new SymbolIcon(Windows.UI.Xaml.Controls.Symbol.Pause);
            }
        }

        internal void StopTimer()
        {
            if (dt.IsEnabled)
            {
                dt.Stop();
                UpdateButton();
            }
        }

        AppBarButton btnTimer = new AppBarButton();

        #region IExposeApplicationBarItems Members

        public IEnumerable<ICommandBarElement> ApplicationBarItems
        {
            get
            {
                yield return btnTimer;
            }
        }

        #endregion
    }
}