using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Telerik.Windows.Controls;

namespace TelerikTest
{
    public partial class MainWindow : Window
    {
        public Dictionary<string, double> Zooms { get; set; }
        public Dictionary<string, Point> StartPoints { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            Zooms = new Dictionary<string, double>();
            Zooms.Add("CanvasInDocking", 1);
            Zooms.Add("CanvasOutDocking", 1);

            StartPoints = new Dictionary<string, Point>();
            StartPoints.Add("CanvasInDocking", new Point());
            StartPoints.Add("CanvasOutDocking", new Point());
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Wall wall = new Wall(2, 0.4, 0, 0);

            var canvasInDocking = this.ChildrenOfType<Canvas>().FirstOrDefault(c => c.Name == "CanvasInDocking");
            wall.PlotWall(canvasInDocking);
            wall.PlotWall(CanvasOutDocking);
        }

        private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            Canvas cv = (Canvas)sender;

            double Dzoom;
            double zoomActualCV = Zooms[cv.Name];

            if (e.Delta > 0) Dzoom = .1 * zoomActualCV;
            else if (e.Delta < 0) Dzoom = -.1 * zoomActualCV;
            else Dzoom = 0;

            zoomActualCV = zoomActualCV + Dzoom;

            zoomActualCV = Math.Max(zoomActualCV, 1);

            if (zoomActualCV == 1) return;

            Border pr = (Border)cv.Parent;
            TransformGroup tfg = new TransformGroup();

            Point pos_cv = e.GetPosition(cv);
            Point pos_pr = e.GetPosition(pr);

            tfg.Children.Add(new ScaleTransform(zoomActualCV, zoomActualCV));
            tfg.Children.Add(new TranslateTransform(pos_pr.X - pos_cv.X * zoomActualCV, pos_pr.Y - pos_cv.Y * zoomActualCV));
            cv.RenderTransform = tfg;

            Zooms[cv.Name] = zoomActualCV;

         
        }

        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Canvas cv = (Canvas)sender;

            if (e.ClickCount > 1)
            {
                Zooms[cv.Name] = 1;

                TransformGroup tfg = new TransformGroup();
                tfg.Children.Add(new ScaleTransform(1, 1));
                cv.RenderTransform = tfg;
            }
        }

        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            Canvas cv = (Canvas)sender;
            Border pr = (Border)cv.Parent;

            if (cv.IsMouseCaptured)
            {
                TransformGroup tfg = new TransformGroup();

                Point pos_cv = e.GetPosition(cv);
                Point pos_pr = e.GetPosition(pr);

                double zoomActualCV = Zooms[cv.Name];

                double DxZ = pos_pr.X - pos_cv.X * zoomActualCV;
                double DyZ = pos_pr.Y - pos_cv.Y * zoomActualCV;

                double DxP = pos_cv.X - StartPoints[cv.Name].X;
                double DyP = pos_cv.Y - StartPoints[cv.Name].Y;

                tfg.Children.Add(new ScaleTransform(zoomActualCV, zoomActualCV));
                tfg.Children.Add(new TranslateTransform(DxZ + DxP, DyZ + DyP));
                cv.RenderTransform = tfg;

                Zooms[cv.Name] = zoomActualCV;
            }
        }

        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Canvas cv = (Canvas)sender;

            if (e.MiddleButton == MouseButtonState.Pressed)
            {
                StartPoints[cv.Name] = e.GetPosition(cv);
                cv.CaptureMouse();
            }
        }

        private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Canvas cv = (Canvas)sender;

            if (e.MiddleButton == MouseButtonState.Released)
            {
                cv.ReleaseMouseCapture();
            }
        }
    }
}