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

#if (SILVERLIGHT)
using C1.Silverlight.Chart;
using System.Diagnostics;
#else
using C1.WPF.C1Chart;
#endif

namespace ChartSamples
{
  [System.ComponentModel.Description("Gallery of chart types, themes and palettes")]
  public partial class Gallery : UserControl
  {
    public Gallery()
    {
      InitializeComponent();

      cbTheme.ItemsSource = new string[]
      {
        "None",
        "DuskBlue", "DuskGreen", "MediaPlayer",
        "Office2003Blue", "Office2003Classic", "Office2003Olive",
        "Office2003Royale", "Office2003Silver", "Office2007Black",
        "Office2007Blue", "Office2007Silver", "Vista"
      };
      cbChartType.ItemsSource = Utils.GetEnumValues(typeof(ChartType));
      cbPalette.ItemsSource = Utils.GetEnumValues(typeof(Palette));

      cbTheme.SelectedItem = "None";
      cbChartType.SelectedItem = chart.ChartType.ToString();
      cbPalette.SelectedItem = chart.Palette.ToString();
    }

    

    private void cbTheme_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      if (cbTheme.SelectedItem is string)
          chart.Theme = (ChartTheme)Enum.Parse(typeof(ChartTheme),
            (string)cbTheme.SelectedItem, false);
    }

    private void cbPalette_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      if (cbPalette.SelectedItem is string)
        chart.Palette = (Palette)Enum.Parse(typeof(Palette),
          (string)cbPalette.SelectedItem, false);
    }

    private void cbChartType_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      if (cbChartType.SelectedItem is string)
      {
        ChartType chartType = (ChartType)Enum.Parse(typeof(ChartType),
          (string)cbChartType.SelectedItem, false);
        chart.ChartType = chartType;
      }
    }
  }

  class Utils
  {
    public static T[] GetEnumValues<T>()
    {
      var type = typeof(T);
      if (!type.IsEnum)
        throw new ArgumentException("Type '" + type.Name + "' is not an enum");

      return (
        from field in type.GetFields(BindingFlags.Public | BindingFlags.Static)
        where field.IsLiteral
        select (T)field.GetValue(null)
      ).ToArray();
    }

    public static object[] GetEnumValues(Type type)
    {
      List<object> list = new List<object>();
      for(int i=0;;i++)
      {
          string val = Enum.GetName(type, i);
          if( !string.IsNullOrEmpty(val))
            list.Add( val);
          else
            break;
      }

      return list.ToArray();
    }
  }
}
