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;
using C1.Silverlight;

namespace C1_Demo
{
    public partial class NestedScrollers : UserControl
    {
        public NestedScrollers()
        {
            InitializeComponent();

            C1DragDropManager ddm = new C1DragDropManager();
            ddm.AutoScrollEdge = 12;
            ddm.DragDrop += _dd_DragDrop;

            // Initialize data
            for (int i = 0; i < 10; i++)
            {
                AddScrollViewer(_sp, ddm);
            }
        }


        // perform drop
        void _dd_DragDrop(object source, DragDropEventArgs e)
        {
            // get object being dragged
            var sourceElement = (FrameworkElement)e.DragSource;

            // get source parent, target listboxes
            var sourceParent = sourceElement.FindParent<ItemsControl>();
            var target = (ItemsControl)((ScrollViewer)e.DropTarget).Content;

            // move it
            if (sourceParent != target)
            {
                // remove from original position, insert into new position
                sourceParent.Items.Remove(sourceElement);
                target.Items.Add(sourceElement);
            }
        }


        #region ** data

        void AddScrollViewer(StackPanel parent, C1DragDropManager ddm)
        {
            ScrollViewer sv = new ScrollViewer();
            sv.Height = 200;
            sv.Margin = new Thickness(20, 10, 20, 10);
            sv.Background = new SolidColorBrush(Colors.Transparent);
            ddm.RegisterDropTarget(sv, true);
            parent.Children.Add(sv);

            ItemsControl container = new ItemsControl();
            sv.Content = container;
            container.Margin = new Thickness(10);

            foreach (Person p in Person.Generate(10))
            {
                ContentControl studentControl = new ContentControl();
                studentControl.ContentTemplate = (DataTemplate)Resources["StudentTemplate"];
                studentControl.Content = p;
                container.Items.Add(studentControl);
                ddm.RegisterDragSource(studentControl, DragDropEffect.Copy, ModifierKeys.None);
            }
        }

        #endregion
    }
}
