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.Input;

namespace ControlExplorer.Controls.Chart
{
    public partial class FinancialChart : UserControl, IExposeApplicationBarItems
    {
        AppBarButton btnNewData = new AppBarButton { Icon = new BitmapIcon { UriSource = new Uri("ms-appx:///Resources/AppBar/plus.png") } };
        public FinancialChart()
        {
            InitializeComponent();

            //set up scaling
            chart.View.AxisX.Scale = 0.20;
            //chart.ActionUpdateDelay = 0;
            //chart.ManipulationMode = ManipulationModes.TranslateX;
            //chart.GestureSlide = GestureSlideAction.Translate;

            chart.Data.Children[0].ChartType = ChartType.Column;

            ChartPanel panel = chart.View.Layers[0] as ChartPanel;
            if(panel.Children.Count > 1)
            {
                panel.Children[0].DataPoint = new Point(-1, double.NaN);
                panel.Children[1].DataPoint = new Point(-1, double.NaN);
            }

            btnNewData.Label = "New Data";
            btnNewData.Click += OnNewDataClick;

            Loaded += FinancialChart_Loaded;
            UpdateScrollButtons();
        }

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

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

        void NewData()
        {
            long ticks = DateTime.Now.Ticks;
            int len = 40;
            List<Quotation> data = SampleFinancialData.Create(len);
            chart.BeginUpdate();
            chart.Data.ItemsSource = data;
            //chart.View.AxisX.Min = data[0].Time.ToOADate() - 0.5;
            //chart.View.AxisX.Max = data[len - 1].Time.ToOADate() + 0.5;
            chart.EndUpdate();
        }

        private void btnScrollRight_Click(object sender, RoutedEventArgs e)
        {
            if (chart.View.AxisX.Value < 1.0)
            {
                chart.View.AxisX.Value += 0.1;
            }
            UpdateScrollButtons();
        }

        private void btnScrollLeft_Click(object sender, RoutedEventArgs e)
        {
            if (chart.View.AxisX.Value > 0)
            {
                chart.View.AxisX.Value -= 0.1;
            }
            UpdateScrollButtons();
        }

        private void UpdateScrollButtons()
        {
            if (chart.View.AxisX.Value == 1.0)
            {
                btnScrollRight.IsEnabled = false;
            }
            else
            {
                btnScrollRight.IsEnabled = true;
            }
            if (chart.View.AxisX.Value == 0)
            {
                btnScrollLeft.IsEnabled = false;
            }
            else
            {
                btnScrollLeft.IsEnabled = true;
            }

        }

        #region IExposeApplicationBarItems Members

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

        #endregion

        private void obj_DataPointChanged(object sender, EventArgs e)
        {
            // update label in code from marker
            var obj = (ChartPanelObject)sender;
            if(obj != null && chart != null)
            {
                Point pt = chart.View.PointFromData(obj.DataPoint);
                double distance = 0; //distance calculated, but not used

                // get point for the series
                int indexCandle = chart.View.DataIndexFromPoint(pt, 1, MeasureOption.X, out distance);
                if(indexCandle != -1)
                { 
                    StackPanel panel = new StackPanel();
                    var quote = ((List<Quotation>)chart.Data.ItemsSource)[indexCandle];
                    var tb = new TextBlock();
                    tb.Text = string.Format("High: {0}\nLow: {1}\nOpen: {2}\nClose: {3}\nVolume: {4}", 
                        quote.High, quote.Low, quote.Open, quote.Close, quote.Volume);
                    panel.Children.Add(tb);

                    obj.Content = panel;
                }
                
            }
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if(e.NewSize.Width > e.NewSize.Height)
            {
                chart.View.AxisX.Scale = 0.30;
            }
            else
            {
                chart.View.AxisX.Scale = 0.20;
            }
        }

    }

    public class Quotation
    {
        public DateTime Time
        {
            get;
            set;
        }

        public double Open
        {
            get;
            set;
        }

        public double Close
        {
            get;
            set;
        }

        public double High
        {
            get;
            set;
        }

        public double Low
        {
            get;
            set;
        }

        public double Volume
        {
            get;
            set;
        }
    }

    public class SampleFinancialData
    {
        static Random rnd = new Random();
        public static List<Quotation> Create(int npts)
        {
            List<Quotation> data = new List<Quotation>();
            DateTime dt = DateTime.Today.AddDays(0);

            for (int i = 0; i < npts; i++)
            {
                Quotation q = new Quotation();

                q.Time = dt.AddDays(i);

                if (i > 0)
                    q.Open = data[i - 1].Close;
                else
                    q.Open = 1000;

                q.High = q.Open + rnd.Next(50);
                q.Low = q.Open - rnd.Next(50);

                q.Close = rnd.Next((int)q.Low, (int)q.High);

                q.Volume = rnd.Next(0, 100);

                data.Add(q);
            }

            return data;
        }
    }
}