using C1.Xaml.Chart;
using System;
using System.Collections.Generic;
using Windows.UI.Xaml.Controls;

namespace ControlExplorer.Controls.Chart.Interaction
{
    public partial class DragDrop : Page
    {
        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 += new DragDropEventHandler(_ddManager_DragDrop);

            _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;
        }
    }
}