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.Shell;
namespace Tiles
{
    public partial class Peter : PhoneApplicationPage
    {
        public Peter()
        {
            InitializeComponent();
        }

        private void Add_Click(object sender, System.EventArgs e)
        {
            ShellTile Tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("Peter"));
            if (Tile == null)
            {
                StandardTileData tileData = new StandardTileData
                {
                    BackgroundImage = new Uri("Images/Peter_Tile.png", UriKind.Relative),
                    Title = "Peter",
                    BackTitle = "Peter Nowak",
                    BackContent = "Entwickeln für Windows Phone 7"
                };
                ShellTile.Create(new Uri("/Peter.xaml?Name=Peter", UriKind.Relative), tileData);
            }
        }

        private void Delete_Click(object sender, System.EventArgs e)
        {
            ShellTile Tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("Peter"));
            if (Tile != null)
            {
                Tile.Delete();
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;
                ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = false;
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            ShellTile Tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("Peter"));
            if (Tile == null)
            {
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;
                ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = false;
            }
            else
            {
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = false;
                ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = true;
            }
            base.OnNavigatedTo(e);
        }
    }
}