using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.Diagrams;
using Telerik.Windows.Diagrams.Core;

namespace ConnectionManipulation
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<IConnectionEditPoint> points = new List<IConnectionEditPoint>();
        private System.Windows.Controls.Orientation orientation;
        private Point startPosition;
        private MyRoutingService router;

        public MainWindow()
        {
            InitializeComponent();

            this.router = new MyRoutingService(this.diagram);
            this.diagram.ServiceLocator.Register<IRoutingService>(this.router);

            this.diagram.RoutingService.Router = new AStarRouter(this.diagram);
            this.diagram.RoutingService.FreeRouter = new AStarRouter(this.diagram);
        }

        public static double Middle(double first, double second)
        {
            return Math.Min(first, second) + Math.Abs(first - second) / 2;
        }

        private void OnConnectionManipulationStarted(object sender, ManipulationRoutedEventArgs e)
        {
            points.Clear();
            this.router.IsManipulation = true;
            if (e.ManipulationStatus == ManipulationStatus.Moving && e.ManipulationPoint.Type == ManipulationPointType.Intermediate)
            {
                var index = e.Connection.ManipulationPoints.IndexOf(e.ManipulationPoint);
                if (index > 0 && index < e.Connection.ManipulationPoints.Count - 1)
                {
                    var prev = e.Connection.ManipulationPoints[index - 1];
                    var next = e.Connection.ManipulationPoints[index + 1];
                    this.startPosition = e.CurrentPosition;
                    if (prev.Position.Y == next.Position.Y && prev.Position.Y == e.ManipulationPoint.Position.Y)
                    {
                        this.orientation = System.Windows.Controls.Orientation.Horizontal;
                        points.Add(prev);
                        points.Add(next);
                        points.Add(e.ManipulationPoint);
                    }
                    else if ((prev.Position.X == next.Position.X && prev.Position.X == e.ManipulationPoint.Position.X))
                    {
                        this.orientation = System.Windows.Controls.Orientation.Vertical;
                        points.Add(prev);
                        points.Add(next);
                        points.Add(e.ManipulationPoint);
                    }
                }
            }
        }

        private void OnConnectionManipulationCompleted(object sender, ManipulationRoutedEventArgs e)
        {
            this.router.IsManipulation = false;
            if (e.ManipulationStatus == ManipulationStatus.Moved && e.ManipulationPoint.Type == ManipulationPointType.Intermediate)
            {
                if (this.points.Count == 3)
                {
                    if (this.orientation == System.Windows.Controls.Orientation.Horizontal)
                    {
                        var delta = e.CurrentPosition.Y - this.startPosition.Y;
                        var newY = this.points[0].Position.Y + delta;
                        for (int i = 0; i < this.points.Count; i++)
                        {
                            var item = this.points[i];
                            if (i == this.points.Count - 1)
                                item.Position = new Point(Middle(points[0].Position.X, points[1].Position.X), newY);
                            else
                                item.Position = new Point(item.Position.X, newY);
                        }
                    }
                    else
                    {
                        var delta = e.CurrentPosition.X - this.startPosition.X;
                        var newX = this.points[0].Position.X + delta;
                        for (int i = 0; i < this.points.Count; i++)
                        {
                            var item = this.points[i];
                            if (i == this.points.Count - 1)
                                item.Position = new Point(newX, Middle(points[0].Position.Y, points[1].Position.Y));
                            else
                                item.Position = new Point(newX, item.Position.Y);
                        }
                    }
                }
            }
        }
    }
}
