using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using System.Collections;

namespace Wpf.AutoDrag
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Grid1.ItemsSource = new MainViewModel().Data;
            this.Grid2.ItemsSource = new MainViewModel().Data;
        }

        private void Grid_DropQuery(object sender, Telerik.Windows.Controls.DragDrop.DragDropQueryEventArgs e)
        {
            var row = e.Options.Source as GridViewRow;
            var destinationGrid = e.Options.Destination as RadGridView;

            e.QueryResult = row != null && row.GridViewDataControl != null && destinationGrid != null && row.GridViewDataControl != destinationGrid;
        }

        private void Grid_DropInfo(object sender, Telerik.Windows.Controls.DragDrop.DragDropEventArgs e)
        {
             var destinationGrid = e.Options.Destination as RadGridView;

            if (e.Options.Status == Telerik.Windows.Controls.DragDrop.DragStatus.DropComplete && destinationGrid!=null)
            {
                ((IList)destinationGrid.ItemsSource).Add(e.Options.Payload);
            }
        }

        private void Grid_DragInfo(object sender, Telerik.Windows.Controls.DragDrop.DragDropEventArgs e)
        {
            var sourceRow = e.Options.Source as GridViewRow;

            if (e.Options.Status == Telerik.Windows.Controls.DragDrop.DragStatus.DragComplete && sourceRow!=null)
            {
                ((IList)sourceRow.GridViewDataControl.ItemsSource).Remove(e.Options.Payload);
            }
        }
    }

        
    public class User : INotifyPropertyChanged
    {
        private int id;

        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
                this.OnPropertyChanged("Id");
            }
        }

        private string userName;

        public string UserName
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
                this.OnPropertyChanged("UserName");
            }
        }

        private DateTime lastLoginDate;

        public DateTime LastLoginDate
        {
            get
            {
                return lastLoginDate;
            }
            set
            {
                lastLoginDate = value;
                this.OnPropertyChanged("LastLoginDate");
            }
        }


        private bool active;

        public bool Active
        {
            get
            {
                return active;
            }
            set
            {
                active = value;
                this.OnPropertyChanged("Active");
            }
        }


        private bool approved;

        public bool Approved
        {
            get
            {
                return approved;
            }
            set
            {
                approved = value;
                this.OnPropertyChanged("Approved");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class MainViewModel
    {
        private ObservableCollection<User> data;

        public ObservableCollection<User> Data
        {
            get { return data; }
            set { data = value; }
        }

        public MainViewModel()
        {
            GenerateData(5);
        }

        private void GenerateData(int c)
        {
            this.data = new ObservableCollection<User>((
                from p in Enumerable.Range(0, c)
                select new User
                {
                    Id = p,
                    Active = p % 2 == 0,
                    Approved = p % 3 == 0,
                    LastLoginDate = DateTime.Now.AddDays(-p),
                    UserName = "User " + p
                }).ToList());
        }

    }
      
}
