# PlaylistManagement [C#]

     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 WpfApplication1.ServiceReference1;
     using System.Globalization;
     using System.Collections;
     using System.Collections.ObjectModel;

     namespace WpfApplication1
     {

         // Interaction logic for PlaylistManagement.xaml
         public partial class PlaylistManagement : UserControl
         {
             ServiceSample sample = new ServiceSample();

             public PlaylistManagement()
             {
                 InitializeComponent();
             }

             //Initialize the XAML object and retrieve group from server by login
             public PlaylistManagement(string login, string password)
             {
                 InitializeComponent();

                 //Login and retrive group
                 List<View_Group> listResult;
                 sample.GetGroup(out listResult, login, password);

                 //Bind group to tree
                 treeGroup.ItemsSource = TreeNode.GetTreeNode(listResult);
             }

             //Select group and retrive coresponding medias and playlist
             private void treeGroup_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
             {
                 //Get Medias for specific group
                 List<View_Media> listMediaResult;
                 sample.GetMedia(out listMediaResult, (treeGroup.SelectedItem as TreeNode).Id??0);

                 //Update media thumbnail path
                 listMediaResult.ForEach(x => x.ThumbnailPath = (x.ThumbnailPath != null) ? (GlobalParam.MEDIA_URL + x.ThumbnailPath) : "");

                 //Bind media to List
                 gridMedia.ItemsSource = listMediaResult;

                 //Get Playlist for specific group
                 List<View_Playlist> listPlaylistResult;
                 sample.GetPlaylist(out listPlaylistResult, (new long[] {(treeGroup.SelectedItem as TreeNode).Id ?? 0}).ToList());

                 //Bind playlist to List
                 gridPlaylist.ItemsSource = listPlaylistResult;
             }

             //Select playlist and retrive coresponding playlist medias
             private void gridPlaylist_SelectionChanged(object sender, SelectionChangedEventArgs e)
             {
                 if (gridPlaylist.SelectedItem == null) return;

                 //Get Playlist media from selected playlist
                 List<View_PlaylistComponent> listResult;
                 sample.GetPlaylistMedia(out listResult, (treeGroup.SelectedItem as TreeNode).Id ?? 0, (gridPlaylist.SelectedItem as View_Playlist).Id);

                 //Update media thumbnail path
                 listResult.ForEach(x => x.ThumbnailPath = (x.ThumbnailPath != null) ? (GlobalParam.MEDIA_URL + x.ThumbnailPath) : "");

                 //Bind media to List
                 gridPlComponent.ItemsSource = new ObservableCollection<View_PlaylistComponent>(listResult);
             }

             private void gridMedia_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
             {
                 ListBox box = (ListBox)sender;

                 if (box.SelectedItem == null) return;

                 Point pnt = e.GetPosition(box);
                 ScrollViewer scroll = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(box, 0), 0) as ScrollViewer;
                 box.SelectedIndex = (int)(scroll.VerticalOffset + (pnt.Y / 48));

                 DragDrop.DoDragDrop(box, box.SelectedItem, DragDropEffects.Move);
             }

             //Drop media to playlist
             private void gridPlComponent_Drop(object sender, DragEventArgs e)
             {
                 if (gridPlaylist.SelectedItem == null) return;

                 //Retrive list of playlist media
                 ListBox box = (ListBox)sender;
                 ObservableCollection<View_PlaylistComponent> list = (box.ItemsSource as ObservableCollection<View_PlaylistComponent>);

                 //Retrive media to drop
                 View_Media media = e.Data.GetData(typeof(View_Media)) as View_Media;

                 //Get media position
                 Point pnt = e.GetPosition(box);
                 ScrollViewer scroll = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(box, 0), 0) as ScrollViewer;
                 int position = Math.Min(list.Count, (int)(scroll.VerticalOffset + (pnt.Y / 48)));

                 //Create playlist media data
                 View_PlaylistComponent component = new View_PlaylistComponent()
                     {
                         //Active = media.Active,
                         //Duration = media.Duration,
                         //HideBanner = media.HideBanner,
                         ManagerIdAddedBy = media.ManagerIdAddedBy,
                         MediaId = media.Id,
                         MediaName = media.Name,
                         //MediaPath = media.MediaPath,
                         NoBanner = true,
                         Order = position,
                         //PeriodCount = media.periodCount ?? 0,
                         PlaylistId = (gridPlaylist.SelectedItem as View_Playlist).Id,
                         ThumbnailPath = media.ThumbnailPath,
                         TypeId = 1
                     };

                 //Add playlist media
                 sample.AddPlaylistMedia(ref component, (treeGroup.SelectedItem as TreeNode).Id ?? 0);

                 //Insert result in the collection
                 list.Insert(position, component);
             }
         }

         public class ColorConvert : IMultiValueConverter
         {
             public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
             {
                 return new SolidColorBrush(Color.FromArgb(255, System.Convert.ToByte(values[0]), System.Convert.ToByte(values[1]), System.Convert.ToByte(values[2])));
             }

             public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
             {
                 throw new NotImplementedException();
             }
         }
     }