using C1.Xaml.Chart;
using System;
using System.Collections.Generic;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

namespace ControlExplorer.Controls.Chart
{
    public partial class InteractiveChart
        : UserControl, IExposeApplicationBarItems
    {
        Color[] _clrs = new Color[] { Colors.Red, Colors.Green, Colors.Blue };
        AppBarButton btnZoomOut = new AppBarButton();
        Random rnd = new Random();

        public InteractiveChart()
        {
            InitializeComponent();

            c1chart.View.PlotBackground = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255));
            c1chart.View.AxisY.MajorGridFill = new SolidColorBrush(Color.FromArgb(50, 50, 50, 50));
            btnZoomOut.Label = "zoom out";
            btnZoomOut.Icon = new BitmapIcon { UriSource = new Uri("ms-appx:///Resources/AppBar/zoom_1-1.png") };
            btnZoomOut.Click += OnZoomOut;

            CreateSampleData(3, 200);

            c1chart.View.AxisX.Scale = 0.5;
            UpdateScrollbars();

            c1chart.ActionUpdateDelay = 0;
            c1chart.ManipulationMode = ManipulationModes.Scale |
              ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.TranslateInertia;

            c1chart.GestureDoubleTap = GestureDoubleTapAction.Scale;
            c1chart.GesturePinch = GesturePinchAction.Scale;
            c1chart.GestureSlide = GestureSlideAction.Translate;
        }

        #region object model

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

        #endregion

        void CreateSampleData(int nser, int npts)
        {
            c1chart.BeginUpdate();
            DataSeriesCollection sc = c1chart.Data.Children;
            sc.Clear();

            for (int iser = 0; iser < nser; iser++)
            {
                double phase = 0.05 + 0.1 * rnd.NextDouble();
                double amp = 1 + 5 * rnd.NextDouble();

                DataSeries ds = new DataSeries();
                double[] vals = new double[npts];
                for (int i = 0; i < npts; i++)
                    vals[i] = amp * Math.Sin(i * phase);

                ds.ValuesSource = vals;
                ds.Label = "S " + iser.ToString();

                ds.ConnectionStroke = new SolidColorBrush(_clrs[iser % 3]);
                ds.ConnectionStrokeThickness = 2;

                sc.Add(ds);
            }
            c1chart.EndUpdate();
        }

        private void OnZoomOut(object sender, RoutedEventArgs e)
        {
            c1chart.BeginUpdate();
            c1chart.View.AxisX.Scale = 1;
            c1chart.View.AxisX.Value = 0.5;
            c1chart.View.AxisY.Scale = 1;
            c1chart.View.AxisY.Value = 0.5;

            c1chart.EndUpdate();
        }
        private void UpdateScrollbars()
        {
            double sx = c1chart.View.AxisX.Scale;
            AxisScrollBar sbx = (AxisScrollBar)c1chart.View.AxisX.ScrollBar;
            if (sx >= 1.0)
                sbx.Visibility = Visibility.Collapsed;
            else
                sbx.Visibility = Visibility.Visible;

            double sy = c1chart.View.AxisY.Scale;
            AxisScrollBar sby = (AxisScrollBar)c1chart.View.AxisY.ScrollBar;
            if (sy >= 1.0)
                sby.Visibility = Visibility.Collapsed;
            else
                sby.Visibility = Visibility.Visible;
        }
    }
}