﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using SharpHelper.Logic;
using Newtonsoft.Json;
using System.Threading;

namespace SharpHelper.Views
{
    /// <summary>
    /// UserlistWindow.xaml 的互動邏輯
    /// </summary>
    public partial class UserlistWindow : MetroWindow
    {
        public UserlistWindow()
        {
            InitializeComponent();
        }

        private void UserListWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Core.SocketIo.Emit("GetUsers");
            Core.SocketIo.On("ReceiveUsersList", (list) =>
            {
                this.Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(() =>
                {
                    string[] users = list.ToString().Replace("\"", "").Replace("[", "").Replace("]", "").Split(',');
                    foreach (var user in users)
                        lbUserList.Items.Add(user);
                }));
            });
            Core.SocketIo.On("UserJoined", (username) =>
            {
                this.Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(() =>
                {
                    if (!lbUserList.Items.Contains(username.ToString()))
                        lbUserList.Items.Add(username);
                }));
            });
            Core.SocketIo.On("UserLeft", (username) =>
            {
                this.Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(() =>
                {
                    if (lbUserList.Items.Contains(username.ToString()))
                        lbUserList.Items.Remove(username);
                }));
            });
        }
    }
}
