/*
 *  protected async override void OnUpdate()
 *  {
 *    Cursor nowCursor = Cursor;
 *    Cursor = _containsFeatures ? Cursors.Arrow : _thisCursor;
 *
 *    if (nowCursor != Cursor)
 *    {
 *      await FrameworkApplication.SetCurrentToolAsync("esri_mapping_exploreTool");
 *      await FrameworkApplication.SetCurrentToolAsync("globeSpotterArcGISPro_openImageTool");
 *    }
 *
 *    base.OnUpdate();
 *  }
 *
 *  protected override async void OnToolMouseMove(MapViewMouseEventArgs e)
 *  {
 *    await QueuedTask.Run(() =>
 *    {
 *      var constants = ConstantsRecordingLayer.Instance;
 *      double size = constants.SizeLayer;
 *      double halfSize = size / 2;
 *      MapView activeView = MapView.Active;
 *
 *      WinPoint clientPoint = e.ClientPoint;
 *      WinPoint pointScreen = activeView.ClientToScreen(clientPoint);
 *      double x = pointScreen.X;
 *      double y = pointScreen.Y;
 *      WinPoint minPoint = new WinPoint(x - halfSize, y - halfSize);
 *      WinPoint maxPoint = new WinPoint(x + halfSize, y + halfSize);
 *      MapPoint minPoint1 = activeView.ScreenToMap(minPoint);
 *      MapPoint maxPoint1 = activeView.ScreenToMap(maxPoint);
 *      Envelope envelope = EnvelopeBuilder.CreateEnvelope(minPoint1, maxPoint1, minPoint1.SpatialReference);
 *      var features = MapView.Active?.GetFeatures(envelope);
 *      _containsFeatures = (features != null) && (features.Count >= 1);
 *    });
 *
 *    base.OnToolMouseMove(e);
 *  }
 */
        protected override Task <bool> OnSketchCompleteAsync(Geometry geometry)
        {
            return(QueuedTask.Run(() =>
            {
                MapPoint point = geometry as MapPoint;
                MapView activeView = MapView.Active;

                if (point != null && activeView != null)
                {
                    var constants = ConstantsRecordingLayer.Instance;
                    double size = constants.SizeLayer;
                    double halfSize = size / 2;

                    SpatialReference pointSpatialReference = point.SpatialReference;
                    var pointScreen = activeView.MapToScreen(point);
                    double x = pointScreen.X;
                    double y = pointScreen.Y;
                    WinPoint pointScreenMin = new WinPoint(x - halfSize, y - halfSize);
                    WinPoint pointScreenMax = new WinPoint(x + halfSize, y + halfSize);
                    var pointMapMin = activeView.ScreenToMap(pointScreenMin);
                    var pointMapMax = activeView.ScreenToMap(pointScreenMax);

                    Envelope envelope = EnvelopeBuilder.CreateEnvelope(pointMapMin, pointMapMax, pointSpatialReference);
                    var features = activeView.GetFeatures(envelope);

                    GlobeSpotter globeSpotter = GlobeSpotter.Current;
                    CycloMediaGroupLayer groupLayer = globeSpotter?.CycloMediaGroupLayer;

                    if (features != null && groupLayer != null)
                    {
                        _nearest = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);

                        if (_nearest)
                        {
                            Settings settings = Settings.Instance;
                            MySpatialReference cycloCoordSystem = settings.CycloramaViewerCoordinateSystem;

                            if (cycloCoordSystem != null)
                            {
                                SpatialReference cycloSpatialReference = cycloCoordSystem.ArcGisSpatialReference ??
                                                                         cycloCoordSystem.CreateArcGisSpatialReferenceAsync().Result;

                                if (pointSpatialReference.Wkid != cycloSpatialReference.Wkid)
                                {
                                    ProjectionTransformation projection = ProjectionTransformation.Create(pointSpatialReference,
                                                                                                          cycloSpatialReference);
                                    point = GeometryEngine.Instance.ProjectEx(point, projection) as MapPoint;
                                }

                                if (point != null)
                                {
                                    CultureInfo ci = CultureInfo.InvariantCulture;
                                    _location = string.Format(ci, "{0},{1}", point.X, point.Y);

                                    if (!globeSpotter.InsideScale())
                                    {
                                        double minimumScale = ConstantsRecordingLayer.Instance.MinimumScale;
                                        double scale = minimumScale / 2;
                                        Camera camera = new Camera(point.X, point.Y, scale, 0.0);
                                        MapView.Active?.ZoomTo(camera);
                                    }
                                }
                            }
                        }
                        else
                        {
                            foreach (var feature in features)
                            {
                                Layer layer = feature.Key;
                                CycloMediaLayer cycloMediaLayer = groupLayer.GetLayer(layer);

                                if (cycloMediaLayer != null)
                                {
                                    foreach (long uid in feature.Value)
                                    {
                                        Recording recording = cycloMediaLayer.GetRecordingAsync(uid).Result;

                                        if (recording.IsAuthorized == null || (bool)recording.IsAuthorized)
                                        {
                                            _location = recording.ImageId;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                return true;
            }));
        }