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;

namespace ChartSamples
{
  public sealed partial class AggregateChart : UserControl
  {
    Dictionary<string, Brush> _dict = new Dictionary<string, Brush>();
    Random rnd = new Random();

    public AggregateChart()
    {
      this.InitializeComponent();

      _dict["red"] = CreateBrush(Colors.Red);
      _dict["blue"] = CreateBrush(Color.FromArgb(255, 2, 150, 252));
      _dict["yellow"] = CreateBrush(Colors.Yellow);
      //_dict["white"] = CreateBrush(Colors.White);


      cb.ItemsSource = Utils.GetEnumValues(typeof(Aggregate));
      cb.SelectedIndex = 0;
      cb.SelectionChanged += (s, e) =>
        chart.Aggregate = (Aggregate)cb.SelectedIndex;

      int cnt = 30;
      chart.Data.ItemNames = CreateRandomStrings(cnt,
        new string[] { "blue", "red", "yellow" }); ;

      var vals = CreateRandomValues(cnt);

      var ds = new DataSeries() { ValuesSource = vals };

      ds.PlotElementLoaded += (s, e) =>
      {
        PlotElement pe = (PlotElement)s;
        if (_dict.ContainsKey(pe.DataPoint.Name))
          pe.Fill = _dict[pe.DataPoint.Name];
        pe.StrokeThickness = 0;
      };

      chart.Data.Children.Add(ds);
      chart.View.AxisX.AnnoTemplate = Resources["label"];
      chart.View.AxisX.AnnoVisibility = AnnoVisibility.ShowAll;
      BarColumnOptions.SetRadiusX(chart, 4);
      BarColumnOptions.SetRadiusY(chart, 4);
    }

    double[] CreateRandomValues(int cnt)
    {
      double[] vals = new double[cnt];
      for (int i = 0; i < cnt; i++)
        vals[i] = rnd.NextDouble() * 100;
      return vals;
    }

    string[] CreateRandomStrings(int cnt, string[] list)
    {
      string[] vals = new string[cnt];
      for (int i = 0; i < cnt; i++)
        vals[i] = list[rnd.Next(0, list.Length)];
      return vals;
    }

    Brush CreateBrush(Color clr)
    {
      return new LinearGradientBrush()
      {
        GradientStops = 
        { 
          //new GradientStop() { Color= Colors.Black,  Offset = 0},
          new GradientStop() { Color= Color.FromArgb(128, clr.R,clr.G,clr.B) , Offset = 0},
          new GradientStop() { Color= clr, Offset = 0.2},
          new GradientStop() { Color= clr, Offset = 0.7},
          new GradientStop() { Color= Color.FromArgb(128, clr.R,clr.G,clr.B) , Offset = 1},
        },
        StartPoint = new Point(0, 0),
        EndPoint = new Point(1, 1),
        //Opacity = 0.8
      };
    }
  }

  public class LabelConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, string language)
    {
      if (value != null)
#if SampleLib
        return string.Format(@"ms-appx:///ChartSamplesLib2013/Assets/flower_{0}.png", value.ToString());
#else
        return string.Format("/Assets/flower_{0}.png", value.ToString());
#endif

      return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
      throw new NotImplementedException();
    }
  }

}
