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 Microsoft.Phone.Controls;
using System.Net.Sockets;
using System.Windows.Threading;
using System.Text;

namespace UDPMultiChat
{
    public partial class MainPage : PhoneApplicationPage
    {
        UdpAnySourceMulticastClient anySourceClient;
        DispatcherTimer timer = new DispatcherTimer();
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            anySourceClient = new UdpAnySourceMulticastClient(IPAddress.Parse("239.0.10.1"), 50000);
            anySourceClient.BeginJoinGroup(result =>
                {
                    anySourceClient.EndJoinGroup(result);
                    timer.Stop();
                    timer.Interval = new TimeSpan(0, 0, 0, 0, 5000);
                    timer.Tick += new EventHandler(dt_Tick);
                    timer.Start();
                    Lauschen();
                }, null);
        }

        void dt_Tick(object sender, EventArgs e)
        {
            byte[] data = Encoding.UTF8.GetBytes("Hallo von " + Microsoft.Phone.Info.DeviceStatus.DeviceName);
            anySourceClient.BeginSendToGroup(data, 0, data.Length,
                result =>
                {
                    anySourceClient.EndSendToGroup(result);
                }, null);
        }

        public void Lauschen()
        {
            byte[] bNachricht = new byte[255];
            anySourceClient.BeginReceiveFromGroup(bNachricht, 0, bNachricht.Length,
                    result =>
                    {
                        IPEndPoint Sender;
                        anySourceClient.EndReceiveFromGroup(result, out Sender);
                        string sNachricht = DateTime.Now.ToLongTimeString() + " " + Sender.Address.ToString() + ": " + Encoding.UTF8.GetString(bNachricht, 0, bNachricht.Length);
                        Deployment.Current.Dispatcher.BeginInvoke(delegate()
                            {
                                if (listNachrichten.Items.Count > 5000)
                                    listNachrichten.Items.Clear();
                                listNachrichten.Items.Add(sNachricht);
                                listNachrichten.ScrollIntoView(sNachricht);
                            });
                        Lauschen();
                    }, null);


        }

        private void CheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            anySourceClient.MulticastLoopback = false;
        }

        private void CheckBox_Unchecked(object sender, System.Windows.RoutedEventArgs e)
        {
            anySourceClient.MulticastLoopback = true;
        }
    }
}