using C1.Xaml.Chart;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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 User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace ChartSamples
{
    public sealed partial class RangeBar : UserControl
    {
        public RangeBar()
        {
            this.InitializeComponent();
            // remove grid strokes, tick marks and axis lines
            chart.View.AxisY.MajorTickThickness = 0;
            chart.View.AxisY.MinorTickThickness = 0;
            chart.View.AxisY.AxisLine = new Windows.UI.Xaml.Shapes.Line() { StrokeThickness = 0 };
            chart.View.AxisX.MajorGridStrokeThickness = 0;
            chart.View.AxisX.MinorGridStrokeThickness = 0;
            chart.View.AxisX.MajorTickThickness = 0;
            chart.View.AxisX.MinorTickThickness = 0;
            chart.View.AxisX.AxisLine = new Windows.UI.Xaml.Shapes.Line() { StrokeThickness = 0 };

            // enable selection
            chart.Data.SelectionAction = SelectionAction.Tap;
            this.DataContext = new WeatherViewModel();

            SetTemperatureChart();

        }

        private void SetTemperatureChart()
        {
            // create axis title
            chart.View.AxisY.Title = new TextBlock() { Text = "Temperature", HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center };

            // add data series
            chart.Data.Children.Clear();
            HighLowSeries ds = new HighLowSeries();
            ds.ChartType = ChartType.Gantt;
            ds.HighValueBinding = new Binding() { Path = new PropertyPath("TempHigh") };
            ds.LowValueBinding = new Binding() { Path = new PropertyPath("TempLow") };
            ds.Style = this.Resources["highlowseries"] as Style;
            ds.SymbolSize = new Size(40, 40);
            chart.Data.Children.Add(ds);

            ToggleRainButton.IsChecked = false;
            ToggleSnowButton.IsChecked = false;
            ToggleTemperatureButton.IsChecked = true;
        }

        private void SetRainChart()
        {
            chart.View.AxisY.Title = new TextBlock() { Text = "Inches", HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center };
            chart.Data.Children.Clear();
            DataSeries ds = new DataSeries();
            ds.ChartType = ChartType.Column;
            ds.ValueBinding = new Binding() { Path = new PropertyPath("Rainfall") };
            ds.Style = this.Resources["dataseries"] as Style;
            ds.SymbolSize = new Size(40, 40);
            chart.Data.Children.Add(ds);

            ToggleRainButton.IsChecked = true;
            ToggleSnowButton.IsChecked = false;
            ToggleTemperatureButton.IsChecked = false;
        }

        private void SetSnowChart()
        {
            chart.View.AxisY.Title = new TextBlock() { Text = "Days", HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center };
            chart.Data.Children.Clear();
            DataSeries ds = new DataSeries();
            ds.ChartType = ChartType.Column;
            ds.ValueBinding = new Binding() { Path = new PropertyPath("SnowDays") };
            ds.Style = this.Resources["dataseries"] as Style;
            ds.SymbolSize = new Size(40, 40);
            chart.Data.Children.Add(ds);

            ToggleRainButton.IsChecked = false;
            ToggleSnowButton.IsChecked = true;
            ToggleTemperatureButton.IsChecked = false;
        }

        private void ToggleTemperatureButton_Click(object sender, RoutedEventArgs e)
        {
            SetTemperatureChart();
        }

        private void ToggleRainButton_Click(object sender, RoutedEventArgs e)
        {
            SetRainChart();
        }

        private void ToggleSnowButton_Click(object sender, RoutedEventArgs e)
        {
            SetSnowChart();
        }
    }

    public class WeatherViewModel
    {
        // create sample data
        private ObservableCollection<WeatherData> items;
        public C1.Xaml.C1CollectionView View { get; private set; }
        public WeatherViewModel()
        {
            items = new ObservableCollection<WeatherData>();
            // create sample data
            items.Add(new WeatherData() { Month = "Jan", MonthFull = "January", TempHigh = 37, TempLow = 21, RecordTempHigh = 73, RecordTempLow = -22, Rainfall = 3.12, SnowDays = 17, RainDays = 10 });
            items.Add(new WeatherData() { Month = "Feb", MonthFull = "February", TempHigh = 40, TempLow = 22, RecordTempHigh = 76, RecordTempLow = -12, Rainfall = 2.47, SnowDays = 14, RainDays = 8 });
            items.Add(new WeatherData() { Month = "Mar", MonthFull = "March", TempHigh = 50, TempLow = 30, RecordTempHigh = 82, RecordTempLow = -3, Rainfall = 3.35, SnowDays = 10, RainDays = 12 });
            items.Add(new WeatherData() { Month = "Apr", MonthFull = "April", TempHigh = 63, TempLow = 40, RecordTempHigh = 89, RecordTempLow = 14, Rainfall = 3.73, SnowDays = 4, RainDays = 14 });
            items.Add(new WeatherData() { Month = "May", MonthFull = "May", TempHigh = 72, TempLow = 49, RecordTempHigh = 91, RecordTempLow = 27, Rainfall = 4.14, SnowDays = 0, RainDays = 15 });
            items.Add(new WeatherData() { Month = "Jun", MonthFull = "June", TempHigh = 80, TempLow = 58, RecordTempHigh = 98, RecordTempLow = 34, Rainfall = 3.99, SnowDays = 0, RainDays = 12 });
            items.Add(new WeatherData() { Month = "Jul", MonthFull = "July", TempHigh = 83, TempLow = 62, RecordTempHigh = 103, RecordTempLow = 43, Rainfall = 4.13, SnowDays = 0, RainDays = 12 });
            items.Add(new WeatherData() { Month = "Aug", MonthFull = "August", TempHigh = 82, TempLow = 61, RecordTempHigh = 100, RecordTempLow = 39, Rainfall = 4.02, SnowDays = 0, RainDays = 11 });
            items.Add(new WeatherData() { Month = "Sep", MonthFull = "September", TempHigh = 75, TempLow = 54, RecordTempHigh = 97, RecordTempLow = 33, Rainfall = 3.24, SnowDays = 0, RainDays = 11 });
            items.Add(new WeatherData() { Month = "Oct", MonthFull = "October", TempHigh = 64, TempLow = 43, RecordTempHigh = 88, RecordTempLow = 17, Rainfall = 2.81, SnowDays = 1, RainDays = 12 });
            items.Add(new WeatherData() { Month = "Nov", MonthFull = "November", TempHigh = 52, TempLow = 35, RecordTempHigh = 82, RecordTempLow = 0, Rainfall = 3.03, SnowDays = 7, RainDays = 12 });
            items.Add(new WeatherData() { Month = "Dec", MonthFull = "December", TempHigh = 40, TempLow = 25, RecordTempHigh = 74, RecordTempLow = -12, Rainfall = 2.96, SnowDays = 14, RainDays = 11 });
            this.View = new C1.Xaml.C1CollectionView(items);
            this.View.MoveCurrentToFirst();
        }
    }
    public class WeatherData
    {
        public string Month { get; set; }
        public string MonthFull { get; set; }
        public double TempHigh { get; set; }
        public double TempLow { get; set; }
        public double RecordTempHigh { get; set; }
        public double RecordTempLow { get; set; }
        public double Rainfall { get; set; }
        public double SnowDays { get; set; }
        public double RainDays { get; set; }
    }
}
