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;
using StockPortfolio.Common;
using StockPortfolio.Model;
using C1.Xaml.Chart;

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

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();
            LoadTransition.Completed += LoadTransition_Completed;
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
           
        }
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }
        void LoadTransition_Completed(object sender, object e)
        {
            
        }
        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)
            {
                progressBar.Visibility = Visibility.Visible;
                await item.UpdateHistoricalStockData(DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0)));
                progressBar.Visibility = Visibility.Collapsed;
                btnFiveDay.IsChecked = true;
                this.DataContext=item;
                InitializeMarkers();
              
            }

       
        }

        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 btnDate_Click(object sender, RoutedEventArgs e)
        {
            StockDataItem item = this.DataContext as StockDataItem;
            if (item != null)
            {
               
                DateTime startDate = DateTime.Now;
                var btn = sender as ToggleButton;

                if (btn.Name == "btnOneDay")
                {
                    startDate = DateTime.Now.Subtract(new TimeSpan(2, 0, 0, 0));
                    btnFiveDay.IsChecked = false;
                    btnOneMonth.IsChecked = false;
                    btnSixMonth.IsChecked = false;
                    btnOneYear.IsChecked = false;
                   
                }
                else if (btn.Name == "btnFiveDay")
                {
                    startDate = DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0));
                    btnOneDay.IsChecked = false;
                    btnOneMonth.IsChecked = false;
                    btnSixMonth.IsChecked = false;
                    btnOneYear.IsChecked = false;
                }
                else if (btn.Name == "btnOneMonth")
                {
                    startDate = DateTime.Now.Subtract(new TimeSpan(30, 0, 0, 0));
                    btnFiveDay.IsChecked = false;
                    btnOneDay.IsChecked = false;
                    btnSixMonth.IsChecked = false;
                    btnOneYear.IsChecked = false;
                }
                else if (btn.Name == "btnSixMonth")
                {
                    startDate = DateTime.Now.Subtract(new TimeSpan(182, 0, 0, 0));
                    btnFiveDay.IsChecked = false;
                    btnOneMonth.IsChecked = false;
                    btnOneDay.IsChecked = false;
                    btnOneYear.IsChecked = false;
                }
                else if (btn.Name == "btnOneYear")
                {
                    startDate = DateTime.Now.Subtract(new TimeSpan(365, 0, 0, 0));
                    btnFiveDay.IsChecked = false;
                    btnOneMonth.IsChecked = false;
                    btnSixMonth.IsChecked = false;
                    btnOneDay.IsChecked = false;
                }
                progressBar.Visibility = Visibility.Visible;
                await item.UpdateHistoricalStockData(startDate);
                progressBar.Visibility = Visibility.Collapsed;
            }

        }

    }
}
