using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
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;

using C1.Silverlight.Chart;

namespace ChartSamples
{
  public struct VisualEffectsState
  {
    public BorderStyle BorderStyle { get; set; }
    public double BorderThickness { get; set; }

    public double LightAngle { get; set; }

    public double ShadowDepth { get; set; }
    public Color ShadowColor { get; set; }
    public double ShadowOpacity { get; set; }
    public bool IsSoftShadow { get; set; }
  }

  public partial class VfxOptions : UserControl
  {
    VisualEffectsState _state;

    public VisualEffectsState State
    {
      get { return _state; }
    }

    public event EventHandler StateChanged;

    void OnStateChanged()
    {
      if (StateChanged != null)
        StateChanged(this, EventArgs.Empty);
    }

    public VfxOptions()
    {
      InitializeComponent();

      // defaults
      cbBorder.ItemsSource = Tools.GetEnumValues(typeof(BorderStyle));
      cbBorder.SelectedIndex = 0;

      nbThickness.Value = 10;
      cbLight.IsChecked = true;
      angleCtl.Angle = 45;

      nbShadowOpacity.Value = 0.4;
      nbShadowDepth.Value = 4;
      cpShadowColor.SelectedColor = Colors.Black;
      cbSoftShadow.IsChecked = true;
    }

    void TextBlock_Loaded(object sender, RoutedEventArgs e)
    {
      var tb = (TextBlock)sender;
      tb.Foreground = Foreground;
    }

    void CheckBox_Loaded(object sender, RoutedEventArgs e)
    {
      var cb = (CheckBox)sender;
      cb.Foreground = Foreground;
    }

    private void cbBorder_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      _state.BorderStyle = (BorderStyle)Enum.Parse(typeof(BorderStyle), cbBorder.SelectedItem.ToString(), false);
      OnStateChanged();
    }

    private void cbLight_Checked(object sender, RoutedEventArgs e)
    {
      if (cbLight.IsChecked == true)
      {
        _state.LightAngle = angleCtl.Angle;
        angleCtl.Opacity = 1.0; angleCtl.IsHitTestVisible = true;
      }
      if (cbLight.IsChecked == false)
      {
        _state.LightAngle = double.NaN;
        angleCtl.Opacity = 0.0; angleCtl.IsHitTestVisible = false;
      }
      OnStateChanged();
    }

    private void angleCtl_AngleChanged(object sender, EventArgs e)
    {
      _state.LightAngle = angleCtl.Angle;
      OnStateChanged();
    }

    private void nbThickness_ValueChanged(object sender, C1.Silverlight.PropertyChangedEventArgs<double> e)
    {
      _state.BorderThickness = nbThickness.Value;
      OnStateChanged();
    }

    private void nbShadowDepth_ValueChanged(object sender, C1.Silverlight.PropertyChangedEventArgs<double> e)
    {
      _state.ShadowDepth = nbShadowDepth.Value;
      OnStateChanged();
    }

    private void nbShadowOpacity_ValueChanged(object sender, C1.Silverlight.PropertyChangedEventArgs<double> e)
    {
      _state.ShadowOpacity = nbShadowOpacity.Value;
      OnStateChanged();
    }

    private void cpShadowColor_SelectedColorChanged(object sender, C1.Silverlight.PropertyChangedEventArgs<Color> e)
    {
      if (cpShadowColor != null)
      {
        _state.ShadowColor = cpShadowColor.SelectedColor;
        OnStateChanged();
      }
    }

    private void cbSoftShadow_Checked(object sender, RoutedEventArgs e)
    {
      _state.IsSoftShadow = (bool)cbSoftShadow.IsChecked;
      OnStateChanged();
    }
  }

  static class Tools
  {
    public static IEnumerable GetEnumValues(Type type)
    {
      return from s in type.GetFields() where s.IsSpecialName == false select s.Name;
    }
  }
}
