using System;
using System.ComponentModel;
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;

using C1.Silverlight;

namespace ChartSamples
{
  public partial class Page : UserControl
  {
    SolidColorBrush _whiteBrush = new SolidColorBrush(Colors.White);

    public Page()
    {
      InitializeComponent();

      iGallery.Tag = new Gallery();
      iBasic.Tag = iLabels.Tag = new Labels();

      //
      iChart.Tag = new InteractiveChart();
      iZoom.Tag = new ZoomChart();
      iDragDrop.Tag = new DragDrop();
      iMarkers.Tag = new Markers();
      iMouseMarker.Tag = new MouseMarker();
      iAddRemoveMarkers.Tag = new AddRemove();

      iBubble.Tag = new Bubble();
      iRadar.Tag = new Radar();
      iFinance.Tag = new FinancialChart();
      iPlotAreasHor.Tag = new PlotAreasHorizontal();
      iPlotAreasVert.Tag = new PlotAreasVertical();
      
      iAxesLog.Tag = new LogAxes();
      iAxesOrigin.Tag = new AxisOrigin();
      iAxesTicks.Tag = new AxisTicks();
      iAxesDep.Tag = new DependentAxes();
      iAxesCustom.Tag = new CustomLabels();
      //iThemes.Tag = new Themes();
      iComplex.Tag = new ComplexChart();
      iLive.Tag = new Dynamic();
      iHuge.Tag = new LargeData();

      // appearance
      iEffects.Tag = new Effects();
      iAnimation.Tag = new Animation();
      iPieAnimation.Tag = new PieAnimation();
      iLegendCheckBox.Tag = new LegendCheckBox();
      iStackedPie.Tag = new StackedPie();

      // advanced
      iAdvAggregate.Tag = new AggregateChart();
      iAdvFunctions.Tag = new Functions();
      iAdvImage.Tag = new ImageExport();
      iAdvMovAvg.Tag = new MovAverage();
      iAdvParam.Tag = new Parametric();
      iAdvTrend.Tag = new TrendLines();
      iAdvPolygon.Tag = new PolygonPlot();

      foreach (C1TreeViewItem tvi in tv.Items)
        tvi.IsExpanded = true;
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
      SetSample( iGallery.Tag as UserControl);
    }

    private void tv_ItemClick(object sender, SourcedEventArgs e)
    {
      UserControl child = null;
      C1TreeView tv = sender as C1TreeView;
      if (tv != null)
      {
        C1TreeViewItem tvi = e.Source as C1TreeViewItem;
        if (tvi != null && tvi.Tag is UserControl)
          child = (UserControl)tvi.Tag;
        else
        {
          tvi.IsSelected = false;
          if (tvi.Items.Count > 0)
        {
            tvi = tvi.Items[0] as C1TreeViewItem;
            tvi.IsSelected = true;
            child = tvi.Tag as UserControl;
        }
        }
        if (child != null)
          child.Foreground = _whiteBrush;

        SetSample(child);
      }
    }

    void SetSample(UserControl sample)
    {
      bdr.Child = sample;
      DescriptionAttribute da = null;
      if (sample != null)
      {
        da = sample.GetType().GetCustomAttributes(typeof(DescriptionAttribute), false).
          FirstOrDefault(o => o is DescriptionAttribute) as DescriptionAttribute;
      }

      if (da != null)
        ControlDescription.Text = da.Description;
      else
        ControlDescription.Text = "";
    }
  }

  public class ControlNode : C1TreeViewItem
  {
    public ControlNode()
    {
      DefaultStyleKey = typeof(ControlNode);
      Foreground = new SolidColorBrush(Color.FromArgb(0xff, 0x92, 0xc8, 0xd8)); 
    }
  }
}
