using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
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;

namespace ChartSamples
{
  public sealed partial class DateAxis : UserControl
  {
      XYDataSeries ds;

      public DateAxis()
      {
          InitializeComponent();

          

          // initialize aggregation combobox
          spAggregation.Visibility = Visibility.Collapsed;
          cbAggregation.Items.Add(Aggregate.Sum);
          cbAggregation.Items.Add(Aggregate.Count);
          cbAggregation.Items.Add(Aggregate.Average);
          cbAggregation.Items.Add(Aggregate.Minimum);
          cbAggregation.Items.Add(Aggregate.Maximum);
          cbAggregation.Items.Add(Aggregate.Variance);
          cbAggregation.Items.Add(Aggregate.StandardDeviation);
          cbAggregation.SelectedIndex = 0;
          

          CreateSampleChart();

          cbAggregation.SelectionChanged += (s, e) =>
          {
              if (ds.AggregateGroupSelector != null)
                  ds.Aggregate = (Aggregate)cbAggregation.SelectedItem;
          };
          btnMonth.IsChecked = true;
      }

      void CreateSampleChart()
      {
          chart.BeginUpdate();

          // random time data
          var npts = 500;
          var xs = new DateTime[npts];
          var ys = new double[npts];

          var x0 = DateTime.Now.AddDays(-npts);
          var rnd = new Random();
          for (int i = 0; i < npts; i++)
          {
              xs[i] = x0.AddDays(i);
              ys[i] = rnd.NextDouble() * 100;
          }

          ds = new XYDataSeries() { XValuesSource = xs, ValuesSource = ys };

          chart.Data.Children.Add(ds);
          chart.ChartType = ChartType.Column;
          chart.Palette = Palette.Flow;
          chart.View.AxisX.IsTime = true;
          chart.View.AxisX.UseExactLimits = true;
          chart.View.AxisX.MajorGridStrokeThickness = 0;
          chart.View.AxisX.MajorTickThickness = 0;
          chart.View.AxisX.MinorTickThickness = 0;
          chart.View.AxisX.AxisLine = new Line() { StrokeThickness = 0 };
          chart.View.AxisY.MajorTickThickness = 0;
          chart.View.AxisY.MinorTickThickness = 0;
          chart.View.AxisY.AxisLine = new Line() { StrokeThickness = 0 };
          chart.View.AxisY.MajorGridFill = new SolidColorBrush(Color.FromArgb(100, 80, 80, 80));
          chart.EndUpdate();

          
      }

      private void RadioButton_Checked(object sender, RoutedEventArgs e)
      {
          if (ds == null || cbAggregation == null)
              return;

          var btn = (RadioButton)sender;
          var cont = (string)btn.Content;

          // setup aggregation parameters 
          if (cont == "None")
          {
              ds.AggregateGroupSelector = null;
              ds.Aggregate = Aggregate.None;
              spAggregation.Visibility = Visibility.Collapsed;
          }
          else
          {
              spAggregation.Visibility = Visibility.Visible;
              ds.Aggregate = (Aggregate)cbAggregation.SelectedItem;

              if (cont == "Year")
              {
                  ds.AggregateGroupSelector = (x, y, o) =>
                  {
                      var dt = x.FromOADate();
                      var dt1 = new DateTime(dt.Year, 1, 1);
                      //var dt2 = dt1.AddYears(1);
                      //return GetMiddleDate(dt1, dt2).ToOADate();
                      return dt1.ToOADate();
                  };
                  chart.View.AxisX.AnnoFormat = "yyyy";
              }
              else if (cont == "Month")
              {
                  ds.AggregateGroupSelector = (x, y, o) =>
                  {
                      var dt = x.FromOADate();
                      var dt1 = new DateTime(dt.Year, dt.Month, 1);
                      //var dt2 = dt1.AddMonths(1);
                      //return GetMiddleDate(dt1, dt2).ToOADate();
                      return dt1.ToOADate();
                  };
                  chart.View.AxisX.AnnoFormat = "MMM yy";
              }
              else if (cont == "Quarter")
              {
                  ds.AggregateGroupSelector = (x, y, o) =>
                  {
                      var dt = x.FromOADate();
                      var dt1 = new DateTime(dt.Year, 1 + ((dt.Month - 1) / 3) * 3, 1);
                      //var dt2 = dt1.AddMonths(3);
                      //return GetMiddleDate(dt1, dt2).ToOADate();
                      return dt1.ToOADate();
                  };
                  chart.View.AxisX.AnnoFormat = "yyyy";
              }
              else if (cont == "Week")
              {
                  ds.AggregateGroupSelector = (x, y, o) =>
                  {
                      var dt = x.FromOADate();
                      var dt1 = dt.AddDays(-(int)dt.DayOfWeek);
                      //var dt2 = dt1.AddDays(7);
                      //return GetMiddleDate(dt1, dt2).ToOADate();
                      return dt1.ToOADate();
                  };
                  chart.View.AxisX.AnnoFormat = "MMM yy";
              }
          }
      }

      // center of time range
      DateTime GetMiddleDate(DateTime dt1, DateTime dt2)
      {
          return dt1 + TimeSpan.FromTicks((dt2 - dt1).Ticks / 2);
      }
  }

}
