using C1.Xaml.Chart;
using StockPortfolio.Common;
using StockPortfolio.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace StockPortfolio
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class StockDetailPage : Page
    {
        private readonly NavigationHelper navigationHelper;
        public StockDetailPage()
        {
            this.InitializeComponent();
           
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += this.NavigationHelper_LoadState;

        }
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }
       
        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            // TODO: Create an appropriate data model for your problem domain to replace the sample data
            //this.DataContext = DataSource.Instance;
            var item = e.NavigationParameter as StockDataItem;
            if (item != null)
            {
                //await item.UpdateHistoricalStockData(DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0)));
                this.DataContext = item;
                InitializeMarkers();
                timeComboBox.SelectedIndex = 1;
            }
        }

        private void InitializeMarkers()
        {
            ChartPanel panel = c1Chart1.View.Layers[0] as ChartPanel;
            var line = panel.Children[0] as ChartPanelObject;
            var dot = panel.Children[1] as ChartPanelObject;
            line.DataPoint = new Point(0.5, double.NaN);
            dot.DataPoint = new Point(0.5, 0.5);
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);

            // LoadTransition.Begin();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedFrom(e);
        }

        private void line_DataPointChanged(object sender, EventArgs e)
        {
            var cpobj = (ChartPanelObject)sender;
            if (!double.IsNaN(cpobj.DataPoint.Y))
            {
                //DateLabel.Text = cpobj.DataPoint.Y.ToString("F2");
            }
            if (!double.IsNaN(cpobj.DataPoint.X))
            {
                //DateLabel.Text = cpobj.DataPoint.X.FromOADate().ToString("g");
            }
        }

        private void Axis_AnnoCreated(object sender, AnnoCreatedEventArgs e)
        {
            //StockDataItem item = this.DefaultViewModel["Item"] as StockDataItem;
            //(e.Label as TextBlock).Text = item.GetDate(e.Index).ToString("d");
        }

        
        private async void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            if (cb!=null)
            {
                var cbitem = cb.SelectedItem as ComboBoxItem;
                if (cbitem!=null)
                {
                    DateTime startDate = DateTime.Now;
                    switch (cbitem.Content.ToString())
                    {
                        case "DAY":
                            startDate = DateTime.Now.Subtract(new TimeSpan(2, 0, 0, 0));
                            break;
                        case "WEEK":
                            startDate = DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0));
                            break;
                        case "MONTH":
                            startDate = DateTime.Now.Subtract(new TimeSpan(30, 0, 0, 0));
                            break;
                        case "6 MONTH":
                            startDate = DateTime.Now.Subtract(new TimeSpan(182, 0, 0, 0));
                            break;
                        case "1 YEAR":
                            startDate = DateTime.Now.Subtract(new TimeSpan(365, 0, 0, 0));
                            break;    
                        default:
                            return;
                            break;
                    }
                    StockDataItem item = this.DataContext as StockDataItem;
                    progressBar.Visibility = Visibility.Visible;
                    await item.UpdateHistoricalStockData(startDate);
                    progressBar.Visibility = Visibility.Collapsed;
                }

            }
        }
    }
}
