using System;
using System.Device.Location;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Platform;

namespace BingMapsGeoLocation
{
    public partial class MainPage : PhoneApplicationPage
    {
        GeoCoordinateWatcher watcher;
        private MapLayer PushpinLayer = new MapLayer();

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
            App.Current.Exit += Current_Exit;
            if (watcher == null)
            {
                watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
                watcher.MovementThreshold = 20;
                watcher.PositionChanged += watcher_PositionChanged;
                watcher.Start();
            }
        }

        void Current_Exit(object sender, EventArgs e)
        {
            watcher.Stop();
        }

        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            //Set Pushpin
            if (!bingMap.Children.Contains(PushpinLayer))
            {
                bingMap.Children.Add(PushpinLayer);
            }

            PushpinLayer.Children.Clear();
            Pushpin pin = new Pushpin();
            pin.Location = e.Position.Location;
            pin.Content = "Sie sind hier!";
            PushpinLayer.Children.Add(pin);

            //Center Map
            bingMap.Center = e.Position.Location;
            if (bingMap.ZoomLevel != 19)
            {
                bingMap.ZoomLevel = 19;
               
            }
        }

        #region Controls
        private void btnZoomIn_Click(object sender, RoutedEventArgs e)
        {
            bingMap.ZoomLevel++;
        }

        private void btnZoomOut_Click(object sender, RoutedEventArgs e)
        {
            bingMap.ZoomLevel--;
        }


        private void btnAerial_Click(object sender, EventArgs e)
        {
            bingMap.Mode = new AerialMode();
        }

        private void btnAerialLbl_Click(object sender, EventArgs e)
        {
            bingMap.Mode = new AerialMode(true);
        }

        private void btnRoad_Click(object sender, EventArgs e)
        {
            bingMap.Mode = new RoadMode();
        }

        private void btnZoomBar_Click(object sender, EventArgs e)
        {
            if (bingMap.ZoomBarVisibility == System.Windows.Visibility.Collapsed)
            {
                bingMap.ZoomBarVisibility = System.Windows.Visibility.Visible;
            }
            else
            {
                bingMap.ZoomBarVisibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void btnScale_Click(object sender, EventArgs e)
        {
            if (bingMap.ScaleVisibility == System.Windows.Visibility.Collapsed)
            {
                bingMap.ScaleVisibility = System.Windows.Visibility.Visible;
            }
            else
            {
                bingMap.ScaleVisibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void btnLogo_Click(object sender, EventArgs e)
        {
            if (bingMap.LogoVisibility == System.Windows.Visibility.Collapsed)
            {
                bingMap.LogoVisibility = System.Windows.Visibility.Visible;
            }
            else
            {
                bingMap.LogoVisibility = System.Windows.Visibility.Collapsed;
            }
        }
        #endregion

    }
}