using System.Windows;
using System.Windows.Input;
using Ab2d.Controls;
using Valg.EVA.Skanning.UserInterfaceElements.Help;

namespace Valg.Eva.Skanning.Scan.Presentation.ScanInProgress
{
    public partial class ScanInProgressControl : IHelpContributor
    {
        private Point _lastMoveMousePosition;

        public ScanInProgressControl()
        {
            InitializeComponent();

            PreviewMouseRightButtonDown += ZoomPanelMultiMouseButtonSample_PreviewMouseRightButtonDown;
            PreviewMouseRightButtonUp += ZoomPanelMultiMouseButtonSample_PreviewMouseRightButtonUp;
            MouseMove += PreviewImageView_MouseMove;

            UpdateButtonState(ImageZoomPanel.ZoomMode);
            ImageZoomPanel.ZoomModeChanged += ImageZoomPanel_ZoomModeChanged;
        }

        void PreviewImageView_MouseMove(object sender, MouseEventArgs e)
        {
            
            if (ImageZoomPanel.ZoomMode != ZoomPanel.ZoomModeType.Move || e.RightButton != MouseButtonState.Pressed)
                return;

            Point newMousePosition;
            Size actualViewboxSize;
            double dx, dy;

            
            
            actualViewboxSize = ImageZoomPanel.ActualViewbox.Size;

            newMousePosition = e.GetPosition(ImageZoomPanel);

            
            dx = (newMousePosition.X - _lastMoveMousePosition.X) * actualViewboxSize.Width;
            dy = (newMousePosition.Y - _lastMoveMousePosition.Y) * actualViewboxSize.Height;

            
            ImageZoomPanel.IsAnimated = false;
            ImageZoomPanel.Translate(dx, dy);
            ImageZoomPanel.IsAnimated = true;

            _lastMoveMousePosition = newMousePosition;

            e.Handled = true;
        }

        private void ZoomPanelMultiMouseButtonSample_PreviewMouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            ImageZoomPanel.ZoomMode = ZoomPanel.ZoomModeType.Rectangle;
        }

        private void ZoomPanelMultiMouseButtonSample_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            ImageZoomPanel.ZoomMode = ZoomPanel.ZoomModeType.Move;

            _lastMoveMousePosition = e.GetPosition(ImageZoomPanel);
        }

        private void UIElement_OnMouseEnter(object sender, MouseEventArgs e)
        {
            ZoomPanelButtons.Visibility = Visibility.Visible;
        }

        private void UIElement_OnMouseLeave(object sender, MouseEventArgs e)
        {
            ZoomPanelButtons.Visibility = Visibility.Hidden;
        }

        void UpdateButtonState(ZoomPanel.ZoomModeType zoomMode)
        {
            switch (zoomMode)
            {
                case ZoomPanel.ZoomModeType.Move:
                    MoveButton.IsChecked = true;
                    break;
                case ZoomPanel.ZoomModeType.Rectangle:
                    RectangleButton.IsChecked = true;
                    break;
            }
        }

        void ImageZoomPanel_ZoomModeChanged(object sender, ZoomModeChangedRoutedEventArgs e)
        {
            switch (e.OldZoomMode)
            {
                case ZoomPanel.ZoomModeType.Rectangle:
                    RectangleButton.IsChecked = false;
                    break;
                case ZoomPanel.ZoomModeType.Move:
                    MoveButton.IsChecked = false;
                    break;
            }
        }

        public string HelpContributionId { get { return HelpContributionIds.SkanningAvKassePågårISkanning; } }
    }
}

