using System;
using System.Collections.Generic;
using System.IO;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using C1.Silverlight.Chart;

namespace ChartSamples
{
  public partial class StackedPie : UserControl
  {
    public StackedPie()
    {
      InitializeComponent();

      // load data from resource
      var data = new CSVData();
      using (Stream stream = Assembly.GetExecutingAssembly().
          GetManifestResourceStream("ChartSamples.Resources.browsers.csv"))
      {
        data.Read(stream, false);
      }

      int len = data.Length;
      var vdata = new VersionInfo[len];

      for (int i = 0; i < len; i++)
      {
        vdata[i] = new VersionInfo()
        {
          Name = data[i, 0],
          Version = data[i, 1],
          Value = double.Parse(data[i, 2], CultureInfo.InvariantCulture)
        };
      }

      chart.BeginUpdate();
      chart.Data.ItemsSource = vdata;
      chart.Data.ItemNameBinding = new Binding("Name");

      chart.Aggregate = Aggregate.Sum;

      // first series - total by browser
      var ds1 = new DataSeries()
      {
        ValueBinding = new Binding("Value"),
        PointLabelTemplate = (DataTemplate)Resources["lbl"],
      };
      ds1.PlotElementLoaded += (PlotElementLoaded);
      Canvas.SetZIndex(ds1, 1);
      chart.Data.Children.Add(ds1);

      // second series - browser versions
      var ds2 = new DataSeries()
      {
        ItemsSource = vdata, // own data source(no aggregates)
        ValueBinding = new Binding("Value"),
        PointLabelTemplate = (DataTemplate)Resources["lbl1"],
      };
      ds2.PlotElementLoaded += (PlotElementLoaded);
      chart.Data.Children.Add(ds2);

      // chart type and direction
      chart.ChartType = ChartType.PieStacked;
      PieOptions.SetDirection(chart, SweepDirection.Counterclockwise);

      // set palette
      //_palette.Add("Internet Explorer", Color.FromArgb(255, 214, 239, 255));
      //_palette.Add("Firefox", Color.FromArgb(255, 123, 211, 56));
      //_palette.Add("Chrome", Color.FromArgb(255, 239, 21, 123));
      //_palette.Add("Safari", Color.FromArgb(255, 255, 186, 0));
      //_palette.Add("Opera", Color.FromArgb(255, 0, 174, 222));

      // find max version usage by browser
      //foreach (var key in _palette.Keys)
      //  _maxs[key] = (from item in vdata where item.Name == key select item.Value).Max();

      chart.EndUpdate();
    }

    // palette colors
    //Dictionary<string, Color> _palette = new Dictionary<string, Color>();
    // max values(by browser)
    Dictionary<string, double> _maxs = new Dictionary<string, double>();

    void PlotElementLoaded(object sender, EventArgs e)
    {
      var pe = (PlotElement)sender;
      var dp = pe.DataPoint;

      pe.Stroke = new SolidColorBrush(Colors.White);
      pe.StrokeThickness = 1;

      // set slice color
      //if (dp.SeriesIndex == 0)
      //  pe.Fill = new SolidColorBrush(_palette[dp.Name]);
      //else
      //{
      //  var name = ((VersionInfo)dp.DataObject).Name;
      //  pe.Fill = new SolidColorBrush(_palette[name])
      //  {
      //    // opacity depends on value(less usage -> more transparent)
      //    Opacity = 0.3 + 0.7 * dp.Value / _maxs[name]
      //  };
      //}

    }
  }

  public class VersionInfo
  {
    public string Name { get; set; }
    public string Version { get; set; }
    public double Value { get; set; }
  }

}
