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 C1.Silverlight;
using EsriMapSamples.ViewModel;

namespace EsriMapSamples
{
    [System.ComponentModel.Description("This demo detects mouse click and tap gestures to add pins to a map. It demonstrates data binding to a collection of items using MVVM.")]
    public partial class AddPins : UserControl
    {
        public AddPins()
        {
            InitializeComponent();
        }

        private void map1_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
        {
            AddUser(e.ScreenPoint, e.MapPoint);
        }

        private void map1_MapGesture(object sender, ESRI.ArcGIS.Client.Map.MapGestureEventArgs e)
        {
            if (e.Gesture == ESRI.ArcGIS.Client.GestureType.Tap)
            {
                AddUser(e.GetPosition(map1), e.MapPoint);
            }
        }

        private void AddUser(Point screenPoint, ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint)
        {
            if (map1.Extent != null)
            {
                if (map1.WrapAroundIsActive)
                    mapPoint = ESRI.ArcGIS.Client.Geometry.Geometry.NormalizeCentralMeridian(mapPoint) as ESRI.ArcGIS.Client.Geometry.MapPoint;

                if (mapPoint != null)
                {
                    AddUser addUserControl = new AddUser();
                    addUserControl.DataContext = new User(Math.Round(mapPoint.X, 4), Math.Round(mapPoint.Y, 4));
                    C1Window userWindow = new C1Window();
                    userWindow.Content = addUserControl;
                    userWindow.Header = "Add User";
                    userWindow.Left = screenPoint.X;
                    userWindow.Top = screenPoint.Y;
                    userWindow.ShowModal();
                }
            }
        } 

        private void ArcGISTiledMapServiceLayer_InitializationFailed(object sender, EventArgs e)
        {
            C1MessageBox.Show("Esri Map Services down at the moment. Try again later.");
        }
    }
}
