using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using C1.Silverlight.Chart;

namespace AxisScrollBar
{
  public static class Tools
  {
    /// <summary>
    /// Creates sample data.
    /// </summary>
    /// <param name="chart">Chart</param>
    /// <param name="inverted">Inverts x and y.</param>
    public static void CreateData(this C1Chart chart, bool inverted = false)
    {
      int cnt = 1000;
      double[] x = new double[cnt];
      double[] y = new double[cnt];
      for (int i = 0; i < cnt; i++)
      {
        x[i] = i; y[i] = 100 * Math.Sin(0.1 * i);
      }

      if (inverted)
        chart.Data.Children.Add(
          new XYDataSeries() { ConnectionStroke = new SolidColorBrush(Colors.Red), 
            XValuesSource = y, ValuesSource = x });
      else
        chart.Data.Children.Add(
          new XYDataSeries(){  ConnectionStroke = new SolidColorBrush(Colors.Blue),
            XValuesSource = x, ValuesSource = y });
    }

    /// <summary>
    /// Creates chart that is displayed as background on axis silder.
    /// </summary>
    /// <param name="chart">Main chart.</param>
    /// <returns>Axis chart.</returns>
    public static C1Chart CreateAxisChart(this C1Chart chart)
    {
      var axchart = new C1Chart()
      {
        ChartType = chart.ChartType,
        Margin = new Thickness(0),
        Padding = new Thickness(0)
      };
      // no margin, it should fill all slider area
      axchart.View.Margin = new Thickness(0.01);

      // since axis chart is not in visual tree
      // we need to apply template
      axchart.Template = chart.Template;
      axchart.ApplyTemplate();

      // create a copy of first series of main chart
      axchart.Data.Children.Add(new XYDataSeries()
      {
        ConnectionStrokeThickness = 1, ConnectionStroke = chart.Data.Children[0].ConnectionStroke,
        XValuesSource = ((XYDataSeries)chart.Data.Children[0]).XValuesSource,
        ValuesSource = chart.Data.Children[0].ValuesSource
      });

      axchart.View.AxisX.MajorTickHeight = axchart.View.AxisX.MinorTickHeight = 0;
      axchart.View.AxisY.MajorTickHeight = axchart.View.AxisY.MinorTickHeight = 0;

      return axchart;
    }
  }
}

namespace C1.Silverlight.Chart.Extended
{
  public static class VisualTreeTools
  {
    /// <summary>
    /// Finds child with specified name and type.
    /// </summary>
    /// <typeparam name="T">Type of child.</typeparam>
    /// <param name="parent">Parent element,</param>
    /// <param name="name">Name of child.</param>
    /// <returns>The child of specified type with specified name.</returns>
    public static T FindVisualChildByName<T>(this DependencyObject parent, string name) where T : DependencyObject
    {
      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
      {
        var child = VisualTreeHelper.GetChild(parent, i);
        string controlName = child.GetValue(Control.NameProperty) as string;
        if (controlName == name)
        {
          return child as T;
        }
        else
        {
          T result = FindVisualChildByName<T>(child, name);
          if (result != null)
            return result;
        }
      }
      return null;
    }
  }
}
