# PlayerMonitoring [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.Collections.ObjectModel;

     namespace WpfApplication1
     {

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

             List<View_StatusM> listPLayer;

             public PlayerMonitoring()
             {
                 InitializeComponent();
             }

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

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

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

                 //Retrive the list of player monitoring
                 long serverDate;
                 sample.GetMonitoring(out listPLayer, out serverDate);

                 //Update the Server date for retrive the player status
                 Monitoring.ServerDate = serverDate;
             }

             //Select group and retrive coresponding player
             private void treeGroup_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
             {
                 //Retrive player by group from list of player
                 ObservableCollection<Monitoring> listMonitoring = new ObservableCollection<Monitoring>();
                 (from l1 in listPLayer where l1.GroupId == (treeGroup.SelectedItem as TreeNode).Id select l1).ToList().ForEach(x => listMonitoring.Add(new Monitoring(x)));

                 //Bind player to list
                 gridPlayer.ItemsSource = listMonitoring;
             }

             //Send update to specific player(s)
             private void btnValidate_Click(object sender, RoutedEventArgs e)
             {
                 //Retrive list of selected player
                 List<long> listPLayerId = (from l1 in (gridPlayer.ItemsSource as ObservableCollection<Monitoring>) where l1.IsChecked select l1.player.PlayerId).ToList();
                 sample.SendUpdate(listPLayerId);

                 MessageBox.Show("Updates Sent");
             }

             private void gridPlayer_LoadingRow(object sender, DataGridRowEventArgs e)
             {
                 e.Row.Header = (e.Row.GetIndex() + 1).ToString();
             }
         }

         public class Monitoring
         {
             static long serverDate;
             static long clientDate;
             static public long ServerDate
             {
                 get { return serverDate + DateTime.Now.Ticks - clientDate; }
                 set { serverDate = value; clientDate = DateTime.Now.Ticks; }
             }

             public View_StatusM player;

             public Monitoring(View_StatusM player)
             {
                 this.player = player;
             }

             public Boolean IsChecked { get; set; }
             public string PlayerName { get { return player.PlayerName; } }
             public string EditDate { get { return ((player.LastEditDate == null) ? "" : (player.LastEditDate ?? DateTime.Now).ToShortDateString()); } }
             public string SentBy { get { return (player.ManagerName == null && player.Time != null) ? "Auto Update" : player.ManagerName;} }
             public string DownloadStatus
             {
                 get
                 {
                     if (player.OptionTypeId == 0 && player.DownloadPercent == 0) return "Differed";
                     else if (player.DownloadPercent == 100) return "Done";
                     else return player.DownloadPercent + "%";
                 }
             }
             public string Playlist { get { return player.PlaylistName; } }
             public SolidColorBrush Status
             {
                 get
                 {
                     bool isAlive = TimeSpan.FromTicks(ServerDate - (long)(player.LastNotify??0)).TotalMinutes < 10;
                     if (isAlive && (player.AlertCount == 0 || player.AlertCount == null)) return new SolidColorBrush(Color.FromArgb(255, 178, 204, 227));
                     else if (isAlive) return new SolidColorBrush(Color.FromArgb(255, 252, 242, 190));
                     else return new SolidColorBrush(Color.FromArgb(255, 209, 180, 184));
                 }
             }
             public string Alert { get { return player.AlertCount.ToString(); } }
             public string ScreenStatus { get { return player.RSstatus; } }
             public string ScreenColor { get { return player.colorId.ToString(); } }
             public string Version { get { return player.Version; } }
         }
     }