using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace MeterVision
{
    public partial class ImageViewerWindow : Window
    {

        private double zoomFactor = 1.0;
        private RotateTransform rotateTransform = new RotateTransform();

        private Point? dragStartPoint = null;
        private TranslateTransform translateTransform = new TranslateTransform();

        public ImageViewerWindow(string imageSourcePath)
        {
            InitializeComponent();

            imgViewer.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(imageSourcePath, UriKind.RelativeOrAbsolute));

            imgViewer.RenderTransform = rotateTransform; // Initialize the rotation transform
        }

        // Zoom in button click event
        private void ZoomIn_Click(object sender, RoutedEventArgs e)
        {
            zoomFactor *= 1.2; // Increase zoom factor by 20%
            ApplyZoom();
        }

        // Zoom out button click event
        private void ZoomOut_Click(object sender, RoutedEventArgs e)
        {
            zoomFactor /= 1.2; // Decrease zoom factor by 20%
            ApplyZoom();
        }

        // Rotate left button click event
        private void RotateLeft_Click(object sender, RoutedEventArgs e)
        {
            //rotateTransform.Angle -= 90; // Rotate 90 degrees counterclockwise
            AnimateRotation(-90); // Rotate 90 degrees counterclockwise
        }

        // Rotate right button click event
        private void RotateRight_Click(object sender, RoutedEventArgs e)
        {
            //rotateTransform.Angle += 90; // Rotate 90 degrees clockwise
            AnimateRotation(90); // Rotate 90 degrees clockwise
        }

        private void AnimateRotation(double angle)
        {
            rotateTransform.Angle += angle;

            // Apply the animation to the rotation transform
            var animation = new DoubleAnimation(rotateTransform.Angle, TimeSpan.FromSeconds(0.3));
            imgViewer.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, animation);
        }

        private void Reset_Click(object sender, RoutedEventArgs e)
        {
            zoomFactor = 1.0;
            rotateTransform.Angle = 0;

            // Reset the transforms
            imgViewer.RenderTransform = null;
            imgViewer.RenderTransform = rotateTransform; // Reapply the rotation transform
            ApplyZoom();
        }

        // Apply the current zoom factor to the image
        private void ApplyZoom()
        {
            // Ensure the zoom factor stays within reasonable bounds
            if (zoomFactor < 0.1) zoomFactor = 0.1;
            if (zoomFactor > 10.0) zoomFactor = 10.0;

            // Create a ScaleTransform to apply the zoom
            ScaleTransform scaleTransform = new ScaleTransform(zoomFactor, zoomFactor);

            // Combine the scale and rotation transforms
            TransformGroup transformGroup = new TransformGroup();
            transformGroup.Children.Add(scaleTransform);
            transformGroup.Children.Add(rotateTransform);

            // Apply the combined transform to the image
            imgViewer.RenderTransform = transformGroup;
        }

        private bool isZoomedToFit = false;
        private void ImgViewer_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                if (isZoomedToFit)
                {
                    // Restore original size
                    zoomFactor = 1.0;
                    isZoomedToFit = false;
                }
                else
                {
                    // Fit image to window
                    double scaleX = imgViewer.ActualWidth / imgViewer.Source.Width;
                    double scaleY = imgViewer.ActualHeight / imgViewer.Source.Height;
                    zoomFactor = Math.Min(scaleX, scaleY);
                    isZoomedToFit = true;
                }

                ApplyZoom();
            }
        }

        private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            // Handle mouse wheel for zooming
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                if (e.Delta > 0)
                {
                    zoomFactor *= 1.2; // Increase zoom factor by 20%
                    ApplyZoom(); // 调用 ApplyTransforms 方法
                }
                else
                {
                    zoomFactor /= 1.2; // Decrease zoom factor by 20%
                    ApplyZoom();
                }
                e.Handled = true;
            }
        }

        private void ScrollViewer_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            dragStartPoint = e.GetPosition(imgViewer);
        }

        private void ScrollViewer_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (dragStartPoint.HasValue && e.LeftButton == MouseButtonState.Pressed)
            {
                Point currentPoint = e.GetPosition(imgViewer);
                double deltaX = currentPoint.X - dragStartPoint.Value.X;
                double deltaY = currentPoint.Y - dragStartPoint.Value.Y;

                translateTransform.X += deltaX;
                translateTransform.Y += deltaY;

                dragStartPoint = currentPoint;
            }
        }

        private void ScrollViewer_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            dragStartPoint = null;
        }

        //private void SaveState()
        //{
        //    //Properties.Settings.Default.ZoomFactor = zoomFactor;
        //    //Properties.Settings.Default.RotationAngle = rotateTransform.Angle;
        //    //Properties.Settings.Default.TranslateX = translateTransform.X;
        //    //Properties.Settings.Default.TranslateY = translateTransform.Y;
        //    Properties.Settings.Default.Save();
        //}

        //private void LoadState()
        //{
        //    //zoomFactor = Properties.Settings.Default.ZoomFactor;
        //    //rotateTransform.Angle = Properties.Settings.Default.RotationAngle;
        //    //translateTransform.X = Properties.Settings.Default.TranslateX;
        //    //translateTransform.Y = Properties.Settings.Default.TranslateY;
        //    ApplyTransforms();
        //}

        //// 调用 LoadState 在窗口加载时恢复状态
        //protected override void OnInitialized(EventArgs e)
        //{
        //    base.OnInitialized(e);
        //    LoadState();
        //}

        // 调用 SaveState 在窗口关闭时保存状态
        //protected override void OnClosing(CancelEventArgs e)
        //{
        //    SaveState();
        //    base.OnClosing(e);
        //}

    }
}