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 = false;

        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 => Canvas.GetZIndex(x))
              .Max();
            Canvas.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;

                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageWindow.Show(ex.Message);
            }
        }

        public void RectRightUp_DragDelta(object sender, DragDeltaEventArgs e)
        {
            try
            {
                if (mouseDown)
                {
                    Thumb thu = (Thumb)sender;

                    #region 判断是否超出 暂无

                    double plul = Canvas.GetLeft(RectLeftUp);
                    double plut = Canvas.GetTop(RectLeftUp);
                    double prdl = Canvas.GetLeft(RectRightDown);
                    double prdt = Canvas.GetTop(RectRightDown);

                    #endregion 判断是否超出 暂无

                    double imgW = 0;
                    double imgH = 0;
                    double imgX = 0;
                    double imgY = 0;
                    switch (thu.Name)
                    {
                        case "RectLeftUp":

                            #region 左上

                            imgW = imgRightDown.X - (Mouse.GetPosition(GridM).X);
                            imgH = imgRightDown.Y - (Mouse.GetPosition(GridM).Y);
                            imgX = 0;
                            imgY = 0;
                            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);
                            imgX = 0;
                            imgY = 0;
                            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;
                            imgX = 0;
                            imgY = 0;
                            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;

                        default:
                            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>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        public void Grid_MouseDown()
        {
            HideAngleBorder();
        }

        /// <summary>
        /// 鼠标左键点击事件
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        public void Window_MouseLeftButtonDown_1()
        {
            if (RectImgBorder.Visibility != Visibility.Hidden)
            {
                HideAngleBorder();
            }
        }

        #endregion 图片拉伸移动
    }
}
