using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Ab2d.Controls;

namespace Valg.EVA.Skanning.Sample.Startup.Preview
{
    
    
    
    public partial class PreviewImageView : UserControl
    {
        private Point _lastMoveMousePosition;

        public PreviewImageView()
        {
            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;

            
            
            var actualViewboxSize = ImageZoomPanel.ActualViewbox.Size;

            var newMousePosition = e.GetPosition(ImageZoomPanel);

            
            var dx = (newMousePosition.X - _lastMoveMousePosition.X) * actualViewboxSize.Width;
            var 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;
            }
        }
    }
}

