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 Microsoft.Phone.UserData;
using System.Text;
using System.Windows.Media.Imaging;
using System.IO;

namespace PhoneAppContacts
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Contacts cons = new Contacts();

            //Identify the method that runs after the asynchronous search completes.
            cons.SearchCompleted += Contacts_SearchCompleted;

            //Start the asynchronous search.
            cons.SearchAsync(String.Empty, FilterKind.None, null);
        }

        void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Results: " + e.Results.Count().ToString()).AppendLine();
            foreach (var item in e.Results)
            {
                BitmapImage img = new BitmapImage();
                Stream s = item.GetPicture();
                if (s != null)
                {
                    img.SetSource(s);
                    imgPicture.Source = img;
                } 
                
                sb.AppendLine(item.DisplayName);
                foreach (var emailAdress in item.EmailAddresses)
                {
                    sb.AppendLine("    " + emailAdress.Kind.ToString() + ": " + emailAdress.EmailAddress);
                }
            }
            MessageBox.Show(sb.ToString());


        }

        

    }
}