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 CyberX8_Themes.UserControls
{
    /// <summary>
    /// Axes2D.xaml 的交互逻辑
    /// </summary>
    public partial class Axes2D : UserControl
    {
        bool flag = false;
        public Axes2D()
        {
            InitializeComponent();
        }

        private void axes_Loaded(object sender, RoutedEventArgs e)
        {
            //仅触发一次绘制 不允许重复绘制
            if (!flag)
            {
                DrawAxisAndText();
                flag = true;
            }
            //DrawPoint();
        }
        //晶圆实际半径
        public double WaferRadius
        {
            get
            {
                return (double)GetValue(WaferRadiusProperty);
            }
            set
            {
                SetValue(WaferRadiusProperty, value);
            }
        }
        //传送安全半径阈值
        public double SafeRadius
        {
            get
            {
                return (double)GetValue(SafeRadiusProperty);
            }
            set
            {
                SetValue(SafeRadiusProperty, value);
            }
        }

        public double AxesWidth
        {
            get { return (double)GetValue(AxesWidthProperty); }
            set
            {
                SetValue(AxesWidthProperty, value);
            }
        }
        public double AxesHeight
        {
            get { return (double)GetValue(AxesHeightProperty); }
            set
            {
                SetValue(AxesHeightProperty, value);
            }
        }

        /// <summary>
        /// 存储x,y,点位信息的List 
        /// </summary>
        public List<(double x, double y, int arm, string info)> PositionAndKey
        {
            get { return (List<(double x, double y, int arm, string info)>)GetValue(PositionAndKeyProperty); }
            set
            {
                SetValue(PositionAndKeyProperty, value);
                //在更新的同时 对点位数据进行重新绘制
                if (value != null)
                    DrawPoint();
            }
        }

        private void DrawPoint()
        {
            List<UIElement> needdelete = new List<UIElement>();
            foreach (UIElement child in CanvasInPath.Children)
            {
                if (child.GetType() == typeof(Ellipse)) needdelete.Add(child);
            }
            foreach (UIElement i in needdelete)
                CanvasInPath.Children.Remove(i);

            float rc = 5;
            foreach ((double x, double y, int arm, string info) point in PositionAndKey)
            {
                Ellipse ellipse = new Ellipse()
                {
                    Width = rc,
                    Height = rc,
                    ToolTip = point.info,
                    Fill = point.arm == 1 ? new SolidColorBrush(Colors.Blue) : new SolidColorBrush(Colors.Yellow),
                };
                Canvas.SetZIndex(ellipse, 10);//显示层级
                double bottom = (point.y / 10000 + 5) * (CanvasInPath.Height) / 10 - rc / 2 - .8;
                double left = (point.x / 10000 + 5) * (CanvasInPath.Width) / 10 - rc / 2 + .4;
                Canvas.SetLeft(ellipse, left);//x位置
                Canvas.SetBottom(ellipse, bottom);//y位置
                CanvasInPath.Children.Add(ellipse);
            }

            //晶圆半径示意图
            if (WaferRadius > 0)
            {
                double radius = (WaferRadius / 100) * (CanvasInPath.Width) / 10 * 2;
                Ellipse wafer = new Ellipse()
                {
                    Width = radius,
                    Height = radius,
                    Stroke = new SolidColorBrush(Colors.Blue),
                    StrokeThickness = 1
                };
                Canvas.SetLeft(wafer, (WaferRadius / 100 + 5) * (CanvasInPath.Width) / 10 - radius);
                Canvas.SetTop(wafer, (WaferRadius / 100 + 5) * (CanvasInPath.Height) / 10 - radius);
                Canvas.SetZIndex(wafer, 1);
                CanvasInPath.Children.Add(wafer);
            }

            if (SafeRadius > 0)
            {
                double radius = SafeRadius / 100 * (CanvasInPath.Width) / 10 * 2;
                Ellipse safer = new Ellipse()
                {
                    Width = radius,
                    Height = radius,
                    Stroke = new SolidColorBrush(Colors.Yellow),
                    StrokeThickness = 1
                };
                Canvas.SetLeft(safer, (SafeRadius / 100 + 5) * (CanvasInPath.Width) / 10 - radius);
                Canvas.SetTop(safer, (SafeRadius / 100 + 5) * (CanvasInPath.Height) / 10 - radius);
                Canvas.SetZIndex(safer, 1);
                CanvasInPath.Children.Add(safer);
            }

        }

        public static readonly DependencyProperty AxesWidthProperty = DependencyProperty.Register(
                "AxesWidth", typeof(double), typeof(Axes2D));
        public static readonly DependencyProperty AxesHeightProperty = DependencyProperty.Register(
                "AxesHeight", typeof(double), typeof(Axes2D));
        public static readonly DependencyProperty WaferRadiusProperty = DependencyProperty.Register(
                "WaferRadius", typeof(double), typeof(Axes2D));
        public static readonly DependencyProperty SafeRadiusProperty = DependencyProperty.Register(
                "SafeRadius", typeof(double), typeof(Axes2D));
        public static readonly DependencyProperty PositionAndKeyProperty = DependencyProperty.Register(
                "PositionAndKey", typeof(List<(double, double, int, string)>), typeof(Axes2D),
                new PropertyMetadata(null, OnDataPropertyChanged));
        private static void OnDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Axes2D axes2)
            {
                if (e.NewValue is List<(double x, double y, int arm, string info)> postiondata)
                {
                    axes2.PositionAndKey = postiondata;
                }
            }
        }

        private void DrawAxisAndText()
        {
            CanvasInPath.Width = AxesWidth;
            CanvasInPath.Height = AxesHeight;

            //笛卡尔坐标系
            for (int i = 0; i < 11; ++i)
            {
                //坐标线
                Line lineX = new Line()
                {
                    X1 = (double)((decimal)CanvasInPath.Width / 10) * i,
                    X2 = (double)((decimal)CanvasInPath.Width / 10) * i,
                    Y1 = 0,
                    Y2 = CanvasInPath.Height,
                    Stroke = new SolidColorBrush(Colors.DarkGray),
                    StrokeThickness = 1,
                };
                Line lineY = new Line()
                {
                    X1 = 0,
                    X2 = CanvasInPath.Width,
                    Y1 = (double)((decimal)CanvasInPath.Height / 10) * i,
                    Y2 = (double)((decimal)CanvasInPath.Height / 10) * i,
                    Stroke = new SolidColorBrush(Colors.DarkGray),
                    StrokeThickness = 1,
                };
                //中心和边缘线换色加重
                if (i == 0 || i == 10 || i == 5)
                {
                    lineX.Stroke = new SolidColorBrush(Colors.Black);
                    lineY.Stroke = new SolidColorBrush(Colors.Black);
                    lineX.StrokeThickness = 1;
                    lineY.StrokeThickness = 1;
                }
                else
                {
                    lineX.StrokeDashArray = new DoubleCollection() { 2, 2 };
                    lineY.StrokeDashArray = new DoubleCollection() { 2, 2 };
                }
                Canvas.SetZIndex(lineX, 0);
                Canvas.SetZIndex(lineY, 0);
                CanvasInPath.Children.Add(lineX);
                CanvasInPath.Children.Add(lineY);


                //刻度
                if (i < 11)
                {
                    TextBlock xblock = new TextBlock();
                    xblock.Foreground = new SolidColorBrush(Colors.Black);
                    xblock.FontSize = 10;
                    TranslateTransform translateTransform = new TranslateTransform(0, xblock.ActualHeight);
                    ScaleTransform scaleTransform = new ScaleTransform();
                    scaleTransform.ScaleY = -1;

                    xblock.Text = (i - 5) + "";
                    Canvas.SetLeft(xblock, TransFromX((i) * 10));
                    Canvas.SetTop(xblock, CanvasInPath.Width + 12);
                    CanvasInPath.Children.Add(xblock);
                    Canvas.SetZIndex(xblock, 1);

                    TextBlock yblock = new TextBlock();
                    yblock.Foreground = new SolidColorBrush(Colors.Black);
                    yblock.FontSize = 10;
                    translateTransform = new TranslateTransform(0, yblock.ActualHeight);
                    scaleTransform = new ScaleTransform();
                    scaleTransform.ScaleY = -1;

                    yblock.Text = -(i - 5) + "";
                    Canvas.SetLeft(yblock, -12);
                    Canvas.SetTop(yblock, TransFromY((i) * 10));
                    CanvasInPath.Children.Add(yblock);
                    Canvas.SetZIndex(yblock, 1);
                }
            }



        }

        private double TransFromX(double value)
        {
            return (double)(((decimal)value / 10) * (decimal)(CanvasInPath.Width) / 10 - (decimal)5);
        }
        private double TransFromY(double value)
        {
            return (double)(((decimal)value / 10) * (decimal)(CanvasInPath.Height) / 10 - (decimal)3);
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
        }

    }
}