using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Windows.Data.Xml.Dom;

using Windows.UI.Notifications;

using Windows.ApplicationModel.Background;

using Windows.Devices.Enumeration;



namespace Tasks

{

    public sealed class ReaderConnectionNotify : IBackgroundTask

    {

        public async void Run(IBackgroundTaskInstance taskInstance)

        {

            var template = $@"

                            <toast>

                            <visual>

                            <binding template=""ToastGeneric"">

                            </binding>

                            </visual>

                            </toast>

                            ";

            DeviceConnectionChangeTriggerDetails details = (DeviceConnectionChangeTriggerDetails)taskInstance.TriggerDetails;

            if (details != null)

            {

                var deviceInformation = await DeviceInformation.CreateFromIdAsync(details.DeviceId, new List<string> { "System.Devices.Aep.IsConnected" });

                bool isConnected = (bool)deviceInformation.Properties.FirstOrDefault(p => p.Key == "System.Devices.Aep.IsConnected").Value;





                XmlDocument xml = new XmlDocument();

            xml.LoadXml(template);

                var binding = xml.SelectSingleNode("//binding");



                var el = xml.CreateElement("text");

                el.InnerText = deviceInformation.Name;



                binding.AppendChild(el);



                el = xml.CreateElement("text");

                el.InnerText = isConnected?"Connected":"Disconnected";

                binding.AppendChild(el);



            var toast = new ToastNotification(xml);

            ToastNotificationManager.CreateToastNotifier().Show(toast);

            }

        }

    }

}

