using System;
using System.Drawing;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace XHWK.WKTool.Utils
{
    internal class ImageOperationUtil
    {
        #region 图片拉伸移动

        private System.Windows.Point _initialPoint;

        private Thumb _rectLeftUp;
        private Thumb _rectRightUp;
        private Thumb _rectLeftDown;
        private Thumb _rectRightDown;
        private System.Windows.Shapes.Rectangle _rectImgBorder;
        private System.Windows.Controls.Image _imgCanvas;
        private Grid _gridM;
        private bool _mouseDown;

        public void SetMouseDown(bool mouseDown)
        {
            this._mouseDown = mouseDown;
        }

        public ImageOperationUtil
        (
            Thumb rectLeftUp,
            Thumb rectRightUp,
            Thumb rectLeftDown,
            Thumb rectRightDown,
            System.Windows.Shapes.Rectangle rectImgBorder,
            System.Windows.Controls.Image imgCanvas,
            Grid gridM
        )
        {
            this._rectLeftUp = rectLeftUp;
            this._rectRightUp = rectRightUp;
            this._rectLeftDown = rectLeftDown;
            this._rectRightDown = rectRightDown;
            this._rectImgBorder = rectImgBorder;
            this._imgCanvas = imgCanvas;
            this._gridM = gridM;
        }

        /// <summary>
        /// 隐藏图片四个点和线
        /// </summary>
        public void HideAngleBorder()
        {
            _rectLeftUp.Visibility = Visibility.Hidden;
            _rectRightUp.Visibility = Visibility.Hidden;
            _rectLeftDown.Visibility = Visibility.Hidden;
            _rectRightDown.Visibility = Visibility.Hidden;
            _rectImgBorder.Visibility = Visibility.Hidden;
        }

        private void ShowAngleBorder()
        {
            PointLocation();
            _rectLeftUp.Visibility = Visibility.Visible;
            _rectRightUp.Visibility = Visibility.Visible;
            _rectLeftDown.Visibility = Visibility.Visible;
            _rectRightDown.Visibility = Visibility.Visible;
            _rectImgBorder.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 确定四个点和边框的位置大小
        /// </summary>
        private void PointLocation()
        {
            _rectImgBorder.Width = _imgCanvas.ActualWidth + 10.0;
            _rectImgBorder.Height = _imgCanvas.ActualHeight + 10.0;
            _rectImgBorder.Margin = new Thickness(
                _imgCanvas.Margin.Left - 5.0,
                _imgCanvas.Margin.Top - 5.0,
                0,
                0
            );
            Canvas.SetLeft(_rectLeftUp, _imgCanvas.Margin.Left - 10.0);
            Canvas.SetTop(_rectLeftUp, _imgCanvas.Margin.Top - 10.0);
            Canvas.SetLeft(_rectRightUp, _imgCanvas.Margin.Left + _imgCanvas.ActualWidth - 10.0);
            Canvas.SetTop(_rectRightUp, _imgCanvas.Margin.Top - 10.0);
            Canvas.SetLeft(_rectLeftDown, _imgCanvas.Margin.Left - 10.0);
            Canvas.SetTop(_rectLeftDown, _imgCanvas.Margin.Top + _imgCanvas.ActualHeight - 10.0);
            Canvas.SetLeft(_rectRightDown, _imgCanvas.Margin.Left + _imgCanvas.ActualWidth - 10.0);
            Canvas.SetTop(_rectRightDown, _imgCanvas.Margin.Top + _imgCanvas.ActualHeight - 10.0);
        }

        public void PicEMap_MouseDown(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Point point = e.GetPosition(_imgCanvas);
            _initialPoint = point;
            HideAngleBorder();
        }

        public void imgCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && _mouseDown)
            {
                System.Windows.Point point = e.GetPosition(_imgCanvas);
                _imgCanvas.Margin = new Thickness(
                    _imgCanvas.Margin.Left + (point.X - _initialPoint.X),
                    _imgCanvas.Margin.Top + (point.Y - _initialPoint.Y),
                    0,
                    0
                );
                App.PageDrawList[App.PageContextData.currpage - 1].ImageLocation = new TranslateTransform { X = _imgCanvas.Margin.Left, Y = _imgCanvas.Margin.Top };
                App.PageDrawList[App.PageContextData.currpage - 1].ImageSizes = new ScaleTransform { CenterX = _imgCanvas.ActualWidth, CenterY = _imgCanvas.ActualHeight };
                App.PageDrawList[App.PageContextData.currpage - 1].IsImageLocation = false;
            }
        }

        public void imgCanvas_MouseUp()
        {
            if (_mouseDown)
            {
                ShowAngleBorder();
            }
        }

        private PointF _imgRightDown;

        /// <summary>
        /// 设置控件最上层
        /// </summary>
        /// <param name="element">
        /// </param>
        public void BringToFront(Thumb element) //图片置于最顶层显示
        {
            if (element == null)
            {
                return;
            }
            Canvas parent = element.Parent as Canvas;
            if (parent == null)
            {
                return;
            }
            int maxZ = parent.Children.OfType<UIElement>() //linq语句，取Zindex的最大值
                .Where(x => x != element)
                .Select(x => Panel.GetZIndex(x))
                .Max();
            Panel.SetZIndex(element, maxZ + 1);
        }

        public void RectRightUp_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
        {
            try
            {
                Thumb thu = (Thumb)sender;
                BringToFront(thu);
                _imgRightDown = new System.Drawing.PointF((float)(_imgCanvas.Margin.Left + _imgCanvas.ActualWidth), (float)(_imgCanvas.Margin.Top + _imgCanvas.ActualHeight));
                HideAngleBorder();
                switch (thu.Name)
                {
                    case "RectLeftUp":
                        _rectLeftUp.Visibility = Visibility.Visible;
                        break;
                    case "RectRightUp":
                        _rectRightUp.Visibility = Visibility.Visible;
                        break;
                    case "RectLeftDown":
                        _rectLeftDown.Visibility = Visibility.Visible;
                        break;
                    case "RectRightDown":
                        _rectRightDown.Visibility = Visibility.Visible;
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageWindow.Show(ex.Message);
            }
        }

        public void RectRightUp_DragDelta(object sender, DragDeltaEventArgs e)
        {
            try
            {
                if (_mouseDown)
                {
                    Thumb thu = (Thumb)sender;
                    double imgW;
                    double imgH;
                    double imgX;
                    double imgY;
                    switch (thu.Name)
                    {
                        case "RectLeftUp":

                            #region 左上

                            imgW = _imgRightDown.X - (Mouse.GetPosition(_gridM).X);
                            imgH = _imgRightDown.Y - (Mouse.GetPosition(_gridM).Y);
                            if (imgW < 50)
                            {
                                _imgCanvas.Width = 50;
                                imgX = _imgCanvas.Margin.Left;
                                Canvas.SetLeft(thu, Canvas.GetLeft(_rectRightDown) - 50);
                            }
                            else
                            {
                                _imgCanvas.Width = imgW;
                                imgX = Mouse.GetPosition(_gridM).X;
                                Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
                            }
                            if (imgH < 50)
                            {
                                _imgCanvas.Height = 50;
                                imgY = _imgCanvas.Margin.Top;
                                Canvas.SetTop(thu, Canvas.GetTop(_rectRightDown) - 50);
                            }
                            else
                            {
                                _imgCanvas.Height = imgH;
                                imgY = Mouse.GetPosition(_gridM).Y;
                                Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
                            }
                            _imgCanvas.Margin = new Thickness(
                                imgX,
                                imgY,
                                0,
                                0
                            );

                            #endregion 左上

                            break;
                        case "RectRightUp":

                            #region 右上

                            imgW = Mouse.GetPosition(_gridM).X - _imgCanvas.Margin.Left;
                            imgH = _imgRightDown.Y - (Mouse.GetPosition(_gridM).Y);
                            if (imgW < 50)
                            {
                                _imgCanvas.Width = 50;
                                imgX = _imgCanvas.Margin.Left;
                                Canvas.SetLeft(thu, Canvas.GetLeft(_rectLeftUp) + 50);
                            }
                            else
                            {
                                _imgCanvas.Width = imgW;
                                imgX = _imgCanvas.Margin.Left;
                                Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
                            }
                            if (imgH < 50)
                            {
                                _imgCanvas.Height = 50;
                                imgY = _imgCanvas.Margin.Top;
                                Canvas.SetTop(thu, Canvas.GetTop(_rectRightDown) - 50);
                            }
                            else
                            {
                                _imgCanvas.Height = imgH;
                                imgY = Mouse.GetPosition(_gridM).Y;
                                Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
                            }
                            _imgCanvas.Margin = new Thickness(
                                imgX,
                                imgY,
                                0,
                                0
                            );

                            #endregion 右上

                            break;
                        case "RectLeftDown":

                            #region 左下

                            imgW = _imgRightDown.X - (Mouse.GetPosition(_gridM).X);
                            imgH = Mouse.GetPosition(_gridM).Y - _imgCanvas.Margin.Top;
                            if (imgW < 50)
                            {
                                _imgCanvas.Width = 50;
                                imgX = _imgCanvas.Margin.Left;
                                Canvas.SetLeft(thu, Canvas.GetLeft(_rectRightDown) - 50);
                            }
                            else
                            {
                                _imgCanvas.Width = imgW;
                                imgX = Mouse.GetPosition(_gridM).X;
                                Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
                            }
                            if (imgH < 50)
                            {
                                _imgCanvas.Height = 50;
                                imgY = _imgCanvas.Margin.Top;
                                Canvas.SetTop(thu, Canvas.GetTop(_rectLeftUp) + 50);
                            }
                            else
                            {
                                _imgCanvas.Height = imgH;
                                imgY = _imgCanvas.Margin.Top;
                                Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
                            }
                            _imgCanvas.Margin = new Thickness(
                                imgX,
                                imgY,
                                0,
                                0
                            );
                            //imgCanvas.Width = imgRightDown.X - (Mouse.GetPosition(GridM).X);
                            //imgCanvas.Height = Mouse.GetPosition(GridM).Y - imgCanvas.Margin.Top;
                            //imgCanvas.Margin = new Thickness(Mouse.GetPosition(GridM).X, imgCanvas.Margin.Top, 0, 0);

                            #endregion 左下

                            break;
                        case "RectRightDown":

                            #region 右下

                            imgW = Mouse.GetPosition(_gridM).X - _imgCanvas.Margin.Left;
                            imgH = Mouse.GetPosition(_gridM).Y - _imgCanvas.Margin.Top;
                            if (imgW < 50)
                            {
                                _imgCanvas.Width = 50;
                                Canvas.SetLeft(thu, Canvas.GetLeft(_rectLeftUp) + 50);
                            }
                            else
                            {
                                _imgCanvas.Width = imgW;
                                Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
                            }
                            if (imgH < 50)
                            {
                                _imgCanvas.Height = 50;
                                Canvas.SetTop(thu, Canvas.GetTop(_rectLeftUp) + 50);
                            }
                            else
                            {
                                _imgCanvas.Height = imgH;
                                Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
                            }
                            //imgCanvas.Margin = new Thickness(imgX, imgY, 0, 0);
                            //imgCanvas.Width += e.HorizontalChange;
                            //imgCanvas.Height += e.VerticalChange;

                            #endregion 右下

                            break;
                    }
                    App.PageDrawList[App.PageContextData.currpage - 1].ImageLocation = new TranslateTransform { X = _imgCanvas.Margin.Left, Y = _imgCanvas.Margin.Top };
                    App.PageDrawList[App.PageContextData.currpage - 1].ImageSizes = new ScaleTransform { CenterX = _imgCanvas.ActualWidth, CenterY = _imgCanvas.ActualHeight };
                    App.PageDrawList[App.PageContextData.currpage - 1].IsImageLocation = false;
                }
            }
            catch (Exception)
            {
                MessageWindow.Show("图片过小！");
            }
        }

        public void RectRightUp_DragCompleted()
        {
            ShowAngleBorder();
        }

        /// <summary>
        /// 点击标题栏 隐藏截图的四个点和线
        /// </summary>
        public void Grid_MouseDown()
        {
            HideAngleBorder();
        }

        /// <summary>
        /// 鼠标左键点击事件
        /// </summary>
        public void Window_MouseLeftButtonDown_1()
        {
            if (_rectImgBorder.Visibility != Visibility.Hidden)
            {
                HideAngleBorder();
            }
        }

        #endregion 图片拉伸移动
    }
}