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 ESRI.ArcGIS.Client;

namespace EsriMapSamples
{
    [System.ComponentModel.Description("Drag items from the toolbox onto the map. This demo uses the C1DragDropManager class to handle the drag and drop operation.")]
    public partial class DragDrop : UserControl
    {
        C1DragDropManager _ddManager = new C1DragDropManager();
        private static ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();

        public DragDrop()
        {
            InitializeComponent();

            // register drag sources (markers)
            _ddManager.RegisterDragSource(bdrFactory, DragDropEffect.Move, ModifierKeys.None);
            _ddManager.RegisterDragSource(bdrOffice, DragDropEffect.Move, ModifierKeys.None);
            _ddManager.RegisterDragSource(bdrStore, DragDropEffect.Move, ModifierKeys.None);

            // register drop target (map)
            _ddManager.RegisterDropTarget(map1, true);

            // handle Drag & Drop event
            _ddManager.DragDrop += new DragDropEventHandler(ddManager_DragDrop);

            // handle drag over event for label
            _ddManager.DragOver += new DragDropEventHandler(_ddManager_DragOver);

            // hide drop target UI
            _ddManager.TargetMarker.Visibility = System.Windows.Visibility.Collapsed;
        }

        void _ddManager_DragOver(object source, DragDropEventArgs e)
        {
            // get MapPoint from client coords
            ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = map1.ScreenToMap(e.GetPosition(map1));
            txtCoords.Text = String.Format("{0:n4}", mapPoint.X) + ", " + String.Format("{0:n4}", mapPoint.Y);
        }

        void ddManager_DragDrop(object source, DragDropEventArgs e)
        {
            GraphicsLayer graphicsLayer = map1.Layers["ItemLayer"] as GraphicsLayer;
            
            if (graphicsLayer == null)
                return;

            Graphic marker = new Graphic();
            ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = map1.ScreenToMap(e.GetPosition(map1));
            marker.Geometry = mercator.FromGeographic(mapPoint);

            // add marker depending on drag source
            if (e.DragSource == bdrFactory)
            {
                marker.Symbol = (ESRI.ArcGIS.Client.Symbols.Symbol)Resources["factoryMarker"];
            }
            else if (e.DragSource == bdrOffice)
            {
                marker.Symbol = (ESRI.ArcGIS.Client.Symbols.Symbol)Resources["officeMarker"];
            }
            else if (e.DragSource == bdrStore)
            {
                marker.Symbol = (ESRI.ArcGIS.Client.Symbols.Symbol)Resources["storeMarker"];
            }
            graphicsLayer.Graphics.Add(marker);

            // clear label
            txtCoords.Text = "";
        }
    }
}
