using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AIRS
{
    /// <summary>
    /// Picturebox.xaml 的交互逻辑
    /// </summary>
    public partial class Picturebox : UserControl
    {
        public Boolean st = false;
        private Boolean ismove = false;

        private Boolean isDown = false;
        Point ptDown = new Point();
        public String cir_rect = "";

        public double zoom_key = 1;
        public double rect_left = 0;
        public double rect_top = 0;

        public double _PixelWidth = 0;
        public double _PixelHeight = 0;

        public Boolean Frozen = false;

        public string grade_value = "";


        public Picturebox()
        {
            InitializeComponent();
        }

        private void btnanalysis_MouseMove(object sender, MouseEventArgs e)
        {
            if (!ismove)
            {
                btnanalysis.Width = btnanalysis.Width + 2;
                btnanalysis.Height = btnanalysis.Height + 2;
                btnanalysis.FontSize = 14;
                ismove = !ismove;
            }
        }

        private void btnanalysis_MouseLeave(object sender, MouseEventArgs e)
        {
            btnanalysis.Width = btnanalysis.Width - 2;
            btnanalysis.Height = btnanalysis.Height - 2;
            btnanalysis.FontSize = 13;
            ismove = false;
        }

        private void cantest_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if(Frozen==true)
            {
                return;
            }
            ptDown = e.GetPosition((IInputElement)sender);
            if(cir_rect=="Circle")
            {
                double liner = Math.Pow(ptDown.X - circle.Center.X, 2.0) + Math.Pow(ptDown.Y - circle.Center.Y, 2.0);
                if(liner>Math.Pow(circle.RadiusX,2.0))
                {
                    return;
                }
                this.Cursor = System.Windows.Input.Cursors.Hand;
                isDown = true;
            }
            else if(cir_rect=="Rectangle")
            {
                if(ptDown.X<rectrr.Rect.X || ptDown.X>(rectrr.Rect.X+rectrr.Rect.Width) ||
                    ptDown.Y < rectrr.Rect.Y || ptDown.Y > (rectrr.Rect.Y + rectrr.Rect.Height))
                {
                    return;
                }
                this.Cursor = System.Windows.Input.Cursors.Hand;
                isDown = true;
            }
            
        }

        private void cantest_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDown)
            {
                
                if (cir_rect == "Circle")
                {
                    this.Cursor = System.Windows.Input.Cursors.ScrollAll;
                    Point pt = e.GetPosition((IInputElement)sender);
                    double x = pt.X - ptDown.X;
                    double y = pt.Y - ptDown.Y;
                    Point ptm = circle.Center;
                    ptm.X = ptm.X + x;
                    ptm.Y = ptm.Y + y;
                    ptDown = new Point(ptDown.X + x, ptDown.Y + y);
                    circle.Center = new Point(circle.Center.X + x, circle.Center.Y + y);
                    //判断圆是否出图像
                    if ((circle.Center.X-circle.RadiusX) < rect_left)
                    {
                        circle.Center = new Point(circle.RadiusX + rect_left, circle.Center.Y);
                    }
                    if((circle.Center.Y - circle.RadiusY) < rect_top)
                    {
                        circle.Center = new Point(circle.Center.X, circle.RadiusY + rect_top);
                    }
                    if((circle.Center.X+circle.RadiusX)>(img.Width-rect_left))
                    {
                        circle.Center = new Point(img.Width - rect_left - circle.RadiusX, circle.Center.Y);
                    }
                    if ((circle.Center.Y + circle.RadiusY) > (img.Height - rect_top))
                    {
                        circle.Center = new Point(circle.Center.X, img.Height - rect_top - circle.RadiusY);
                    }
                }
                else if (cir_rect == "Rectangle")
                {
                    this.Cursor = System.Windows.Input.Cursors.ScrollAll;
                    Point pt = e.GetPosition((IInputElement)sender);
                    double x = pt.X - ptDown.X;
                    double y = pt.Y - ptDown.Y;
                    Rect rtm = rectrr.Rect;
                    rtm.X = rtm.X + x;
                    rtm.Y = rtm.Y + y;
                    ptDown = new Point(ptDown.X + x, ptDown.Y + y);
                    rectrr.Rect = rtm;
                    if(rectrr.Rect.X<rect_left)
                    {
                        rtm.X = rect_left;
                        rectrr.Rect = rtm;
                    }
                    if (rectrr.Rect.Y < rect_top)
                    {
                        rtm.Y = rect_top;
                        rectrr.Rect = rtm;
                    }
                    if ((rectrr.Rect.X + rectrr.Rect.Width) > (img.Width- rect_left))
                    {
                        rtm.X = img.Width - rect_left - rectrr.Rect.Width;
                        rectrr.Rect = rtm;
                    }
                    if ((rectrr.Rect.Y + rectrr.Rect.Height) > (img.Height - rect_top))
                    {
                        rtm.Y = img.Height - rect_top - rectrr.Rect.Height;
                        rectrr.Rect = rtm;
                    }
                }
            }
        }

        private void cantest_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (isDown)
            {
                isDown = false;
                this.Cursor = System.Windows.Input.Cursors.Arrow;
            }
        }

        private void cantest_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isDown)
            {
                isDown = false;
                this.Cursor = System.Windows.Input.Cursors.Arrow;
            }
        }
    }
}
