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

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

namespace ChartSamples
{
  [System.ComponentModel.Description("Drag'n'drop data series")]
  public partial class DragDrop : UserControl
  {
    Random rnd = new Random();
    C1DragDropManager _ddManager;

    public DragDrop()
    {
      InitializeComponent();

      // create and setup drag and drop manager
      _ddManager = new C1DragDropManager();
      _ddManager.RegisterDropTarget(chart1, true);
      _ddManager.RegisterDropTarget(chart2, true);

      _ddManager.DragDrop += (s, e) =>
      {
        DataSeries dser = ((PlotElement)e.DragSource).DataPoint.Series;

        if (e.DropTarget == chart1)
        {
          if (chart2.Data.Children.Contains(dser))
          {
            _ddManager.ClearSources(); // clean up old plot elements
            chart2.Data.Children.Remove(dser);
            chart1.Data.Children.Add(dser);
          }
        }
        else if (e.DropTarget == chart2)
        {
          if (chart1.Data.Children.Contains(dser))
          {
            _ddManager.ClearSources(); // clean up old plot elements
            chart1.Data.Children.Remove(dser);
            chart2.Data.Children.Add(dser);
          }
        }

        CheckNoData();
      };

      // create test data
      int nser = 8, npts = 8;
      List<DataSeries> list1 = CreateTestData(nser, npts);
      foreach (DataSeries ds in list1)
      {
        ds.PlotElementLoaded += new EventHandler(ds_Loaded);
        chart1.Data.Children.Add(ds);
      }

      CheckNoData();
    }

    /// <summary>
    /// Fires when plot element was loaded.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void ds_Loaded(object sender, EventArgs e)
    {
      PlotElement pe = (PlotElement)sender;
      DataSeries dss = pe.DataPoint.Series;

      // "fix" the series color to avoid automatic palette
      if (pe is Lines)
      {
        if(dss.ConnectionFill == null)
          dss.ConnectionFill = pe.Fill;
        if(dss.ConnectionStroke == null)
        dss.ConnectionStroke = pe.Stroke;
      }
      else
      {
        if(dss.SymbolFill == null)
          dss.SymbolFill = pe.Fill;
        if(dss.SymbolStroke == null)
          dss.SymbolStroke = pe.Stroke;
      }

      // register as data source
      _ddManager.RegisterDragSource(pe, DragDropEffect.Move, ModifierKeys.None);
    }

    /// <summary>
    /// show or hide "no data" message
    /// </summary>
    void CheckNoData()
    {
      if (chart1.Data.Children.Count == 0)
      {
        nodata.Visibility = Visibility.Visible;
        Grid.SetColumn(nodata, 0);
      }
      else if (chart2.Data.Children.Count == 0)
      {
        nodata.Visibility = Visibility.Visible;
        Grid.SetColumn(nodata, 1);
      }
      else
        nodata.Visibility = Visibility.Collapsed;
    }

    /// <summary>
    /// Creates specified number of data series with test data.
    /// </summary>
    /// <param name="ns">Number of data series.</param>
    /// <param name="npts">Number of points.</param>
    /// <returns>List of data series.</returns>
    List<DataSeries> CreateTestData( int ns, int npts)
    {
      List<DataSeries> list = new List<DataSeries>();
      for (int iser = 0; iser < ns; iser++)
      {
        double[] vals = new double[npts];
        for (int i = 0; i < npts; i++)
          vals[i] = rnd.Next(1, 100);
        list.Add( new DataSeries() { ValuesSource = vals, Label = "s" + iser.ToString()});
      }
      return list;
    }
  }
}
