using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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.Xaml.Shapes;
using Windows.UI;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace ChartSamples
{
    public sealed partial class LineMarker : UserControl
    {
        Random rnd = new Random();
        DateTime date;
        const int NUMPOINTS = 1000;

        public LineMarker()
        {
            this.InitializeComponent();
            this.DataContext = this;

            // chart settings
            c1Chart1.ManipulationMode = ManipulationModes.All;
            c1Chart1.View.AxisX.MajorGridStrokeThickness = 0;
            c1Chart1.View.AxisX.IsTime = true;
            c1Chart1.View.AxisX.Scale = 1.0;
            c1Chart1.View.AxisY.MajorUnit = 20;
            c1Chart1.View.AxisX.UseExactLimits = true;
            c1Chart1.View.PlotBackground = new SolidColorBrush(Colors.White);

            c1Chart1.View.AxisX.MajorTickThickness = 0;
            c1Chart1.View.AxisX.MinorTickThickness = 0;
            c1Chart1.View.AxisY.MajorTickThickness = 0;
            c1Chart1.View.AxisY.MinorTickThickness = 0;


            CreateDataItems();

            c1Chart1.Data.ItemsSource = this.Items;


            // create moveable markers
            var pnl = new ChartPanel();

            // vertical line marker
            var vmarker = new ChartPanelObject();
            vmarker.Action = ChartPanelAction.MouseMove;
            vmarker.Attach = ChartPanelAttach.DataX;
            vmarker.VerticalContentAlignment = VerticalAlignment.Stretch;
            vmarker.DataPoint = new Point(0.5, double.NaN);

            SolidColorBrush foregroundBrush = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100));

            var bdr = new Border()
            {
                BorderBrush = Background = foregroundBrush,
                Padding = new Thickness(2),
                BorderThickness = new Thickness(2, 0, 0, 0),
                Margin = new Thickness(-1, 0, 0, 0),
                IsHitTestVisible = false
            };

            vmarker.Content = bdr;
            vmarker.DataPointChanged += obj_DataPointChanged;

            // dot marker
            var dotmarker = new ChartPanelObject();
            dotmarker.Action = ChartPanelAction.MouseMove;
            dotmarker.Attach = ChartPanelAttach.DataX;
            dotmarker.DataPoint = new Point(0.5, 0.5);

            var ell = new Ellipse()
            {
                Fill = foregroundBrush,
                Stroke = foregroundBrush,
                Width = 6,
                Height = 6,
                Margin = new Thickness(-3, 0, 0, 0),
                IsHitTestVisible = false
            };

            dotmarker.Content = ell;
            dotmarker.DataPointChanged += obj_DataPointChanged;

            pnl.Children.Add(vmarker);
            pnl.Children.Add(dotmarker);
            c1Chart1.View.Layers.Add(pnl); 
        }

        void obj_DataPointChanged(object sender, EventArgs e)
        {
            var cpobj = (ChartPanelObject)sender;
            if (!double.IsNaN(cpobj.DataPoint.Y))
            {
                txtValue.Text = cpobj.DataPoint.Y.ToString("F2");
            }
            if (!double.IsNaN(cpobj.DataPoint.X))
            {
                txtDate.Text = cpobj.DataPoint.X.FromOADate().ToString("f");
            }
        }

        public ObservableCollection<DataItem> Items { get; private set; }

        void CreateDataItems()
        {
            this.Items = new ObservableCollection<DataItem>();
            int previousVolume = rnd.Next(50, 100);
            date = new DateTime(DateTime.Now.Subtract(new TimeSpan(NUMPOINTS / 7, NUMPOINTS % 7, 0, 0)).Ticks);
            for (int i = 0; i < NUMPOINTS; i++)
            {
                var value = rnd.Next(previousVolume - 5, previousVolume + 6);
                DataItem di = new DataItem();
                di.Volume = value;
                di.Date = date;
                UpdateDate();
                this.Items.Add(di);
                previousVolume = value;
            }
        }

        private void UpdateDate()
        {
            date = date.AddHours(1);
            if (date.Hour > 16)
            {
                date = date.AddDays(1);
                date = date.Subtract(new TimeSpan(7, 0, 0));
            }
        }
    }

    public class DataItem
    {
        public DateTime Date { get; set; }
        public double Volume { get; set; }
    }
}
