using Avalonia.Controls;
using Avalonia;
using Avalonia.Input;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Avalonia.Media;
using Avalonia.Media.Immutable;
using Avalonia.Markup.Xaml.Templates;
using Avalonia.Controls.Primitives;
using static System.Math;
using Avalonia.Animation;
using Avalonia.Media.Transformation;
using System.Xml.Linq;
using Avalonia.Remote.Protocol.Input;
using BluePrint.Core.IJoin;
using BluePrint.Core.INode;
using BluePrint.Core.DataType;
using BluePrint.Core.Controls;
using BluePrint.Core.Tool;
using BluePrint.Core.Join;

namespace BluePrint.Core
{
    public class BParent : Panel
    {
        /// <summary>
        /// 设置节点上下文
        /// </summary>
        /// <param name="context"></param>
        public void SetContext(List<Type> context) {
            _NodeTypes = context;
        }
        /// <summary>
        /// 设置节点上下文
        /// </summary>
        /// <param name="context"></param>
        public void SetContext(Type context)
        {
            _NodeTypes.Add(context);
        }
        /// <summary>
        /// 在蓝图0，0位置创建一个指定类型的节点
        /// </summary>
        /// <param name="type"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public Control CreateNode(Type type, double x1 = 0, double y1 = 0)
        {
            Control control = null;
            if (_NodeTypes != null)
            {
                foreach (var item in _NodeTypes.Where(x => x == type).ToList())
                {
                    var Control = System.Activator.CreateInstance(item, new object[] { this });
                    //(Control as Control).Margin = $"{x1},{y1},auto,auto";
                    Blueprint_Canvas.SetLeft((Control as Control), x1);
                    Blueprint_Canvas.SetTop((Control as Control), y1);
                    bluePrint.AddChildren((Control)Control);
                    control = (Control)Control;
                }
            }
            bluePrint.InvalidateVisual();
            bluePrint.InvalidateArrange();
            bluePrint.InvalidateMeasure();
            return control;
        }
        /// <summary>
        /// 创建一个接头
        /// </summary>
        /// <param name="type"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public IJoinControl CreateJoin(Type type, IJoinControl.NodePosition position, NodeBase node)
        {
            var Control = System.Activator.CreateInstance(type, new object[] { this, position, node });//,new object[] { this, position, node }
            return (IJoinControl)Control;
        }
        /// <summary>
        /// 在蓝图指定位置创建一个指定类型的节点
        /// </summary>
        /// <param name="type">节点类型</param>
        /// <param name="element">鼠标相对元素</param>
        /// <param name="point">元素内部偏移</param>
        public void CreateNode(Type type,Control element,Point point) {
            if (_NodeTypes!=null)
            {
                Point p = element.GetPosition(point);
                foreach (var item in _NodeTypes.Where(x => x == type).ToList())
                {
                    var Control = System.Activator.CreateInstance(item, new object[] { this });
                    //(Control as Control).Margin = $"{p.X},{p.Y},auto,auto";
                    Blueprint_Canvas.SetLeft((Control as Control), p.X);
                    Blueprint_Canvas.SetTop((Control as Control), p.Y);
                    bluePrint.AddChildren((Control)Control);
                }
            }
        }
        public struct BPNodeType
        {
            /// <summary>
            /// 节点类型
            /// </summary>
            public Type node { set; get; }
        }
        public struct BPPutJoin
        {
            /// <summary>
            /// 序列化用的id
            /// </summary>
            public IJoinControl.NodePosition Position { set; get; }
            /// <summary>
            /// 序列化用的id
            /// </summary>
            public int ID { set; get; }
            /// <summary>
            /// 接头类型
            /// </summary>
            public Type type { set; get; }
            /// <summary>
            /// 接头类内参数
            /// </summary>
            public Dictionary<string, object> PropretyValue { set; get; }
            /// <summary>
            /// 接头需要的数据
            /// </summary>
            public Node_Interface_Data Data { set; get; }
        }
        public struct BPNodedata {
            /// <summary>
            /// 序列化用的id
            /// </summary>
            public int ID { set; get; }
            /// <summary>
            /// 位置
            /// </summary>
            public Data_Point Point { set; get; }
            /// <summary>
            /// 节点
            /// </summary>
            public BPNodeType node { set; get; }
            /// <summary>
            /// 节点的输出数据
            /// </summary>
            public List<BPPutJoin> _OutPutJoin {set;get;}
            /// <summary>
            /// 节点的输入数据
            /// </summary>
            public List<BPPutJoin> _IntPutJoin { set; get; }
        }
        public struct BPJoindata
        {
            /// <summary>
            /// 执行线的头节点
            /// </summary>
            public List<int> _Star { set; get; }
            /// <summary>
            /// 执行线的尾节点
            /// </summary>
            public List<int> _End { set; get; }
        }
        public class BPByte
        {
            /// <summary>
            /// 节点列表
            /// </summary>
            public List<BPNodedata> NodeList { set; get; }
            /// <summary>
            /// 执行线列表
            /// </summary>
            public List<BPJoindata> JoinList { set; get; }
        }

        public void Deserialize(BPByte data) {
            //数据反序列化到蓝图
            List<KeyValuePair<int, KeyValuePair<int, IJoinControl>>> keyValues = new List<KeyValuePair<int, KeyValuePair<int, IJoinControl>>>();
            List<NodeBase> controlList = new List<NodeBase>();
            var indexa = 1;
            foreach (var item in data.NodeList)
            {
                //CreateNode((Type)tree1.Tag, Blueprint_Canvas.GetLeft(parent.MousepanelPupopPos), Blueprint_Canvas.GetTop(parent.MousepanelPupopPos));
                var control = CreateNode(item.node.node,item.Point.X, item.Point.Y);
                if (control == null)
                {
                    continue;
                }
                var basenode = control as NodeBase;
                controlList.Add(basenode);
                basenode.ID = item.ID;
                //反序列化创建的 把他输入输出都删掉重新创建
                basenode._IntPutJoin.Clear();
                basenode.ClearIntPut();
                basenode._OutPutJoin.Clear();
                basenode.ClearOntPut();
                //然后再重新创建
                foreach (var item1 in item._IntPutJoin)
                {
                    indexa++;
                    var join = CreateJoin(item1.type, item1.Position, basenode);
                    //var data1 = item1.Data.Value.GetType();
                    //Newtonsoft.Json.JObject
                    //data1.Value = (typeof(data1.Type))(data1.Value);
                    join.Set(item1.Data);
                    join.ID = item1.ID;
                    keyValues.Add(new KeyValuePair<int, KeyValuePair<int, IJoinControl>>(item.ID, new KeyValuePair<int, IJoinControl>(item1.ID, join)));
                    foreach (var pv in item1.PropretyValue)
                    {
                        //join.SetPropretyValue(pv.Key, pv.Value);
                        var property = AvaloniaPropertyRegistry.Instance.FindRegistered(pv.Value.GetType(), pv.Key);
                        join.SetValue(property, pv.Value);
                    }
                    basenode._IntPutJoin.Add((join, item1.Data));
                }
                foreach (var item1 in item._OutPutJoin)
                {
                    indexa++;
                    var join = CreateJoin(item1.type, item1.Position, basenode);
                    join.ID = item1.ID;
                    join.Set(item1.Data);
                    keyValues.Add(new KeyValuePair<int, KeyValuePair<int, IJoinControl>>(item.ID,new KeyValuePair<int, IJoinControl>(item1.ID, join)));
                    foreach (var pv in item1.PropretyValue)
                    {
                        //join.SetPropretyValue(pv.Key, pv.Value);
                        var property = AvaloniaPropertyRegistry.Instance.FindRegistered(pv.Value.GetType(), pv.Key);
                        join.SetValue(property, pv.Value);
                    }
                    basenode._OutPutJoin.Add((join, item1.Data));
                }
                basenode.RefreshNodes();
                //basenode.RefreshDrawBezier();
            }
            //节点创建完创建执行线
            foreach (var item1 in data.JoinList)
            {
                //执行线只支持一对一
                var bP_Line1 = new BP_Line(bluePrint)
                {
                    Width = 1f,
                    Height = 1f,
                    //backound_color = "rgb(255,255,255)",
                };
                Blueprint_Canvas.SetLeft(bP_Line1, 0);
                Blueprint_Canvas.SetTop(bP_Line1, 0);
                //var nodelist = bluePrint.GetChildrenList();
                var star1 = from grade in keyValues
                            where grade.Key == item1._Star[0] && grade.Value.Key == item1._Star[1]
                            select grade.Value.Value;
                var end1 = from grade in keyValues
                           where grade.Key == item1._End[0] && grade.Value.Key == item1._End[1]
                           select grade.Value.Value;

                var star = star1.FirstOrDefault();
                var end = end1.FirstOrDefault();
                if (star != null && end != null)
                {
                    if (star.GetNodeType() == Runtime.Token.NodeToken.CallValue || star.GetNodeType() == Runtime.Token.NodeToken.Call)
                    {
                        //bP_Line1.backound_color = "rgb(255,255,255)";
                        //执行节点设置为白色执行线
                    }
                    bluePrint.AddLineChildren(bP_Line1);
                    bP_Line1.SetJoin(star, end);
                    bP_Line.InvalidateVisual();
                    //创建完成让他先刷新一下
                    bP_Line1.RefreshDrawBezier();

                }
            }
            /*foreach (var item in controlList)
            {
                BeginInvoke(new Action(() => {
                    (item as BP_Line).RefreshDrawBezier();
                }));
            }*/
            /*this.Delay(TimeSpan.FromSeconds(1), () => {
                foreach (var item in controlList)
                {
                    BeginInvoke(new Action(() => {
                        item.RefreshDrawBezier();
                        //item.InvalidateMeasure();
                        //item.InvalidateArrange();
                        //item.Invalidate();
                    }));
                }
                controlList.Clear();
            });*/
            controlList.Clear();
            keyValues.Clear();

            //this.OnLayoutUpdated();
        }
        public BPByte SerializeBP()
        {
            //蓝图序列化到数据保存
            //bluePrint.Instances.Add(control);
            List<BPNodedata> pdata = new List<BPNodedata>();
            var index_id = 0;
            var ChildList = bluePrint.GetChildrenList();
            foreach (var item5 in ChildList)
            {
                if (item5 is NodeBase item)
                {
                    item.ID = index_id;
                    index_id++;
                    List<BPPutJoin> IntPutJoin = new List<BPPutJoin>();
                    foreach (var item1 in item._IntPutJoin)
                    {
                        var data_temp = item1.Item1.Get();
                        if (!data_temp.IsSerialize)
                        {
                            data_temp.Value = default;
                        }
                        IntPutJoin.Add(new BPPutJoin
                        {
                            Position = item1.Item1._position,
                            type = item1.Item1.GetType(),//Type
                            Data = data_temp,
                            ID = index_id,
                            PropretyValue = item1.Item1.Dump()
                        }) ;
                        item1.Item1.ID = index_id;
                        index_id++;
                    }

                    List<BPPutJoin> OutPutJoin = new List<BPPutJoin>();
                    foreach (var item1 in item._OutPutJoin)
                    {
                        var data_temp = item1.Item1.Get();
                        if (!data_temp.IsSerialize)
                        {
                            data_temp.Value = default;
                        }
                        OutPutJoin.Add(new BPPutJoin
                        {
                            Position = item1.Item1._position,
                            type = item1.Item1.GetType(),
                            Data = data_temp,
                            ID = index_id,
                            PropretyValue = item1.Item1.Dump()
                        });
                        item1.Item1.ID = index_id;
                        index_id++;
                    }
                    pdata.Add(new BPNodedata
                    {
                       
                        Point = new Data_Point(Blueprint_Canvas.GetLeft(item), Blueprint_Canvas.GetTop(item)),//item.ActualOffset
                        ID = item.ID,
                        node = new BPNodeType
                        {
                            node = item.GetType()
                        },
                        _IntPutJoin = IntPutJoin,
                        _OutPutJoin = OutPutJoin,
                    });
                }
            }
            List<BPJoindata> IntPutJoin1 = new List<BPJoindata>();
            foreach (BP_Line item in bluePrint.GetLinesList())
            {
                //        _Star = null;
                //private IJoinControl _End = null;
                //item._IntPutJoin
                IntPutJoin1.Add(new BPJoindata
                {
                    _Star = new List<int> { (item.GetStar().Get_NodeRef() as NodeBase).ID , item.GetStar().ID},
                    _End = new List<int> { (item.GetEnd().Get_NodeRef() as NodeBase).ID, item.GetEnd().ID },
                });
            }
            var bp = new BPByte
            {
                NodeList = pdata,
                JoinList = IntPutJoin1,
            };
            return bp;
        }

        //public override void Render(DrawingContext dc)
        //{
        //    var rect = this.Bounds;
        //    base.Render(dc);
        //    // new SolidColorBrush(Color.FromRgb(39, 39, 39));
        //    dc.FillRectangle(new SolidColorBrush(Color.FromRgb(255, 255, 255)), rect);

        //    dc.DrawGeometry(null, XPathColor, XPath);
        //    dc.DrawGeometry(null, YPathColor, YPath);

        //}
        //private Brush XPathColor = new SolidColorBrush(Color.FromArgb(255,52, 52, 52 )) ;
        //private Brush YPathColor = new SolidColorBrush(Color.FromArgb(150,0, 0, 0));


        public BluePrint bluePrint;
        //public P
        protected override void OnInitialized()
        {
            //Instances.Add(this);
            //模板定义
            ClipToBounds = true;
            //base.OnInitialized();

            ///Background = Color.FromRgb(39, 39, 39);
            bluePrint = new BluePrint
            {
                //MarginLeft = 0f,
                //MarginTop = 0f,
                //Width = "100%",
                //Height = "100%",
                Tag = 1f,
                //RenderTransform = new ScaleTransform(0.8, 0.8),
                //RenderTransformOrigin = new RelativePoint(new Point(29, 29),RelativeUnit.Relative),
            };
            Blueprint_Canvas.SetLeft(bluePrint,0);
            Blueprint_Canvas.SetTop(bluePrint, 0);
            //panel = new Panel
            //{
            //    MarginLeft = 0f,
            //    MarginTop = 0f,
            //    Children = { bluePrint }
            //};ControlbluePrint
            Children.Add(new GridLinesControl { });
            
            Children.Add(bluePrint);
            //添加拖动节点
            MouseJoin = new MouseJoin(this, IJoinControl.NodePosition.Left, this) {
                //MarginLeft = 0f,
                //MarginTop = 0f,
                Width = 1f,
                Height = 1f,
                IsVisible = false
                //Visibility = Visibility.Hidden
            };
            Blueprint_Canvas.SetLeft(MouseJoin, 0);
            Blueprint_Canvas.SetTop(MouseJoin, 0);
            bluePrint.AddChildren1(MouseJoin);
            //
            //添加默认拖动显示线条
            bP_Line = new BP_Line(bluePrint)
            {
                //MarginLeft = 0f,
                //MarginTop = 0f,
                //backound_color = Color.Parse("#89C4F8"),
                LineWidth = 5,
            };
            Blueprint_Canvas.SetLeft(bP_Line, 0);
            Blueprint_Canvas.SetTop(bP_Line, 0);
            MousepanelPupopPos = new Panel
            {
                Width = 1,
                Height = 1,
                IsVisible = false,
            };
            VisualChildren.Add(MousepanelPupopPos);
            //bP_Line.Size = new SizeField(1,1);AddLineChildren
            //ClearState();AddChildren
            //之所以不用AddLineChildren 是因为此线条为默认，不参与计算判断
            bluePrint.AddChildren1(bP_Line);
            //ClearState();

        }
        /// <summary>
        /// 缩放因子
        /// </summary>
        public float ScaleRate = 0.8f;
        Matrix _matrix = Matrix.Identity;
        private TransformOperations.Builder _transformBuilder;

        /// <summary>
        /// Invalidate child element.
        /// </summary>
        /// <param name="skipTransitions">The flag indicating whether transitions on the child element should be temporarily disabled.</param>
        private void InvalidateElement(bool skipTransitions)
        {
            if (bluePrint == null)
            {
                return;
            }

            bluePrint.RenderTransformOrigin = new RelativePoint(new Point(0, 0), RelativeUnit.Relative);
            _transformBuilder = new TransformOperations.Builder(1);
            _transformBuilder.AppendMatrix(_matrix);
            bluePrint.RenderTransform = _transformBuilder.Build();
            bluePrint.InvalidateVisual();
        }
        /// <summary>
        /// Zoom to provided zoom ratio and provided center point.
        /// </summary>
        /// <param name="ratio">The zoom ratio.</param>
        /// <param name="x">The center point x axis coordinate.</param>
        /// <param name="y">The center point y axis coordinate.</param>
        /// <param name="skipTransitions">The flag indicating whether transitions on the child element should be temporarily disabled.</param>
        public void ZoomTo(double ratio, double x, double y, bool skipTransitions = false)
        {
            _matrix = MatrixHelper.ScaleAtPrepend(_matrix, ratio, ratio, x, y);
            InvalidateElement(skipTransitions);
        }
        /// <summary>
        /// Zoom to provided zoom delta ratio and provided center point.
        /// </summary>
        /// <param name="delta">The zoom delta ratio.</param>
        /// <param name="x">The center point x axis coordinate.</param>
        /// <param name="y">The center point y axis coordinate.</param>
        /// <param name="skipTransitions">The flag indicating whether transitions on the child element should be temporarily disabled.</param>
        public void ZoomDeltaTo(double delta, double x, double y, bool skipTransitions = false)
        {
            //double realDelta = Sign(delta) * Pow(Abs(delta), 1);
            double realDelta = Sign(delta) * Pow(Abs(delta), 1);
            Debug.WriteLine($"realDelta:{realDelta}");
            ZoomTo(Pow(ScaleRate, realDelta), x, y, false);
        }
        protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
        {
            var point = e.GetPosition(bluePrint);
            ZoomDeltaTo(-e.Delta.Y, point.X, point.Y);
        }


        Point? mousePos;
        protected override void OnPointerPressed(PointerPressedEventArgs e)
        {
            IsLeftButtonPressed = true;
            ClearState();
            base.OnPointerPressed(e);
            e.Pointer.Capture(this);
            //var key = Root.InputManager.KeyboardDevice.Modifiers;

            ////Console.WriteLine($"key:{key}");
            ////cpf接收不到alt的消息暂时用ctrl代替，等待修复
            //if (e.MouseButton == MouseButton.Right &&
            //    (key == InputModifiers.Control|| key == (InputModifiers.Control | InputModifiers.RightMouseButton)))
            //{
            //    mousePos = e.Location / scale;
            //    CaptureMouse();
            //}
            MousePoint = e.GetPosition(this);
            //var tf = this.TransformToVisual(bluePrint);
            //if (tf is Matrix matrix)
            //    MousePoint = MatrixHelper.TransformPoint(matrix, e.GetPosition(this));
            Blueprint_Canvas.SetLeft(MousepanelPupopPos, MousePoint.X);
            Blueprint_Canvas.SetTop(MousepanelPupopPos, MousePoint.Y);
        }
        
        /// <summary>
        /// 鼠标位置为了弹窗IList, ICollection
        /// </summary>
        public Panel MousepanelPupopPos;

        private List<Type> _NodeTypes = new List<Type>();
        public List<Type> NodeContextTypes
        {
            set {
                _NodeTypes = value;
            }
            get {
                return _NodeTypes;
            }
        }
        public Flyout? popup;
        protected override void OnPointerReleased(PointerReleasedEventArgs e)
        {
            IsLeftButtonPressed = false;
            base.OnPointerReleased(e);
            e.Pointer.Capture(null);
            //Console.ForegroundColor = ConsoleColor.Red;
            if (e.InitialPressMouseButton == Avalonia.Input.MouseButton.Right)
            {
                //Console.WriteLine($"if (e.MouseButton == MouseButton.Right&& key == InputModifiers.None)");
                if (popup == null)
                {
                    //Console.WriteLine($"popup == null");
                    //var SearchElTextBox = new TextBox
                    //{
                    //    Name = "SearchElTextBox",
                    //    Watermark = "搜索",
                    //};

                    //var p2 = new Panel
                    //{
                    //    Children =
                    //    {
                    //        SearchElTextBox,
                    //        new TextBlock
                    //        {
                    //            Name = "closetext",
                    //            //Classes = "el-icon,el-icon-close",
                    //            //MarginRight =0f,
                    //            Width = 20,
                    //            Foreground = new SolidColorBrush(Color.FromArgb(255,0,0,0)),
                    //        },
                    //    },
                    //};

                    //var textBox1 = p2.LogicalChildren().Where(x => x. == name)
                    popup = new Flyout
                    {
                        Placement = PlacementMode.Pointer,
                        Content = new SearchMenuItem(NodeContextTypes, this),
                    };
                    //this.popup = popup;
                }
                popup.ShowAt(this,true);
                //var aa = popup.Focus(NavigationMethod.Click);
            }
        }
        public ElTextBox earchStr = null;
        /// <summary>
        /// 清空当前拖放状态
        /// </summary>
        public void ClearState() {
            ParentJoin = null; 
            IsMouseJoin = false;
            bP_Line.SetJoin(null, null);
            //bP_Line.Width = 1f;
            //bP_Line.Height = 1f;
            //bP_Line.Margin = "0,0,auto,auto";
            Blueprint_Canvas.SetLeft(bP_Line, 0);
            Blueprint_Canvas.SetTop(bP_Line, 0);
            //MouseJoin.Margin = "0,0,auto,auto";
            Blueprint_Canvas.SetLeft(MouseJoin, 0);
            Blueprint_Canvas.SetTop(MouseJoin, 0);
            //MouseJoin.MarginTop = -1;
            bP_Line.IsVisible = false;//.Visibility = Visibility.Hidden;
            MouseJoin.IsVisible = false;//Visibility.Hidden;
        }
        /// <summary>
        /// 用于鼠标拖动的接口，本身只是为了拖动 
        /// </summary>
        public IJoinControl MouseJoin;// = new MouseJoin(this,IJoinControl.NodePosition.Left);
        /// <summary>
        /// 用于模拟拖动的线，不参与流程
        /// </summary>
        public BP_Line bP_Line;
        /// <summary>
        /// 上一个选中的接口
        /// </summary>
        public IJoinControl ParentJoin = null;
        /// <summary>
        /// 设置鼠标状态
        /// </summary>
        /// <param name="State"></param>
        public void SetMouseState(IJoinControl State, PointerReleasedEventArgs e1) {
            //IsMouseJoin = false;
            //bP_Line.InvalidateVisual();
            //bP_Line.RefreshDrawBezier();
            if (ParentJoin == null)
            {
                bP_Line.Width = 0;
                bP_Line.Height = 0;
                bP_Line.RefreshDrawBezier();
                ParentJoin = State;
                //Point p = State.GetPosition(State.GetJoinPos(IJoinControl.NodePosition.Left));
                //MouseJoin.MarginLeft = p.X;
                //MouseJoin.MarginTop = p.Y;
                //bP_Line.MarginLeft = p.X;
                //bP_Line.MarginTop = p.Y;
                //Debug.WriteLine($"State.p:{p}");
                //Debug.WriteLine($"e.Location1:{bP_Line.Margin}");
                //Debug.WriteLine($"-----------------------------------------------");
                //bP_Line.SetJoin(null, null);
                if (ParentJoin.GetDir() == IJoinControl.NodePosition.Left)
                {
                    MouseJoin.SetDir(IJoinControl.NodePosition.right);
                    bP_Line.SetJoin(MouseJoin, ParentJoin);
                }
                if (ParentJoin.GetDir() == IJoinControl.NodePosition.right)
                {
                    MouseJoin.SetDir(IJoinControl.NodePosition.Left);
                    bP_Line.SetJoin(ParentJoin, MouseJoin);
                }
                
                //bP_Line.PositionReckon();
                IsMouseJoin = true;
                //MouseJoin.Visibility = Visibility.Visible;
                /*this.Delay(TimeSpan.FromSeconds(1), () => {
                    bP_Line.Visibility = Visibility.Visible;
                });*/
                bP_Line.IsVisible = true;// Visibility.Visible;

            }
            else {
                Point p = State.GetPosition(State.GetJoinPos(IJoinControl.NodePosition.Left));
                //输入输出一样或者父元素一样全部不可连接
                if (ParentJoin.GetDir() == State.GetDir() ||
                    ParentJoin.GetParnt() == State.GetParnt())
                {
                    ClearState();
                    UIElementTool.Toast(bluePrint, "接口不匹配||不能驲自己",p);
                }
                else {
                    //现在就可以初始化线条用于连接
                    //再判断一下两个节点是否已经有线条了

                    IJoinControl a,b;
                    
                    if (ParentJoin.GetDir() == IJoinControl.NodePosition.Left)
                    {
                        a = State;
                        b = ParentJoin;
                    }
                    else {
                        a = ParentJoin;
                        b = State;
                        //bP_Line1.SetJoin(ParentJoin, State);
                    }
                    
                    if (a.GetJoinType() == b.GetJoinType())
                    {
                        //var add = a.GetJoinType();
                        /*if (star.GetNodeType() == Runtime.Token.NodeToken.CallValue || star.GetNodeType() == Runtime.Token.NodeToken.Call)
                        {
                            bP_Line1.backound_color = "rgb(255,255,255)";
                            //执行节点设置为白色执行线
                        }*/
                        if (a.GetJoinType() == typeof(JoinType)|| 
                            a.GetNodeType() == Runtime.Token.NodeToken.CallValue || 
                            a.GetNodeType() == Runtime.Token.NodeToken.Call)
                        {
                            if (!bluePrint.FildIsJoinRef(a) && !bluePrint.FildIsJoinRef(b))
                            {
                                //执行线只支持一对一
                                var bP_Line1 = new BP_Line(bluePrint)
                                {
                                    //MarginLeft = 0f,
                                    //MarginTop = 0f,
                                    Width = 1f,
                                    Height = 1f,
                                    //backound_color = "rgb(255,255,255)",
                                };
                                bP_Line1.SetJoin(a, b);
                                
                                bluePrint.AddLineChildren(bP_Line1);
                                
                                //创建完成让他先刷新一下
                                
                                Blueprint_Canvas.SetLeft(bP_Line1, 0);
                                Blueprint_Canvas.SetTop(bP_Line1, 0);
                                bP_Line1.RefreshDrawBezier();
                                bP_Line.InvalidateVisual();
                            }
                            else {
                                UIElementTool.Toast(bluePrint, "流程只支持一对一", p);
                            }
                        }
                        else {
                            //剩下的是普通线条
                            if (!bluePrint.FildLine(a, b))
                            {
                                var bP_Line1 = new BP_Line(bluePrint)
                                {
                                    //MarginLeft = 0f,
                                    //MarginTop = 0f,
                                    Width = 1f,
                                    Height = 1f
                                };
                                
                                Blueprint_Canvas.SetLeft(bP_Line1, 0);
                                Blueprint_Canvas.SetTop(bP_Line1, 0);
                                bP_Line1.RefreshDrawBezier();
                                bP_Line.InvalidateVisual();
                                bluePrint.AddLineChildren(bP_Line1);
                                bP_Line1.SetJoin(a, b);
                                ///连上之后，输入禁用操作
                                b.SetEnabled(false);
                                //创建完成让他先刷新一下
                                bP_Line1.RefreshDrawBezier();
                            }
                            else
                            {
                                //已经连过了
                                UIElementTool.Toast(bluePrint, "已经连过了", p);
                            }
                        }
                        
                    }
                    else {
                        //判断接口数据类型
                        if (!a.GetIsTypeCheck() || !b.GetIsTypeCheck())
                        {
                            //接口数据类型不匹配
                            UIElementTool.Toast(bluePrint, "已消除类型检查", p,0.35f);
                            //不进行强类型检测
                            var csharp的goto有限制所以只能复制一份 = "";
                            //剩下的是普通线条
                            if (!bluePrint.FildLine(a, b))
                            {
                                var bP_Line1 = new BP_Line(bluePrint)
                                {
                                    //MarginLeft = 0f,
                                    //MarginTop = 0f,
                                    Width = 1f,
                                    Height = 1f
                                };
                                Blueprint_Canvas.SetLeft(bP_Line1, 0);
                                Blueprint_Canvas.SetTop(bP_Line1, 0);
                                bluePrint.AddLineChildren(bP_Line1);
                                bP_Line1.SetJoin(a, b);
                                ///连上之后，输入禁用操作
                                b.SetEnabled(false);
                                //创建完成让他先刷新一下
                                bP_Line1.RefreshDrawBezier();
                            }
                            else
                            {
                                //已经连过了
                                UIElementTool.Toast(bluePrint, "已经连过了", p);
                            }
                        }
                        else {
                            //接口数据类型不匹配
                            var log = $"a:{a.GetJoinType()},b:{b.GetJoinType()}";
                            //test.Log(log);
                            UIElementTool.Toast(bluePrint, "接口数据类型不匹配", p);
                        }
                    }
                    
                    ClearState(); 
                }
                ParentJoin = null;
            }
            //Invalidate();
        }
        /// <summary>
        /// 是否已经点击接口拖动
        /// </summary>
        public bool IsMouseJoin = false;
        /// <summary>
        /// 鼠标当前位置
        /// </summary>
        public Point MousePoint;

        public bool IsLeftButtonPressed { set; get; } = false;
        protected override void OnPointerMoved(PointerEventArgs e)
        {
            //base.OnPointerMoved(e);

            var temp_mouse_point = e.GetCurrentPoint(this).Position;
            
            if (e.KeyModifiers == KeyModifiers.Control && IsLeftButtonPressed)
            {
                var temp_point = MousePoint- temp_mouse_point;
                bluePrint.Margin -= new Thickness(temp_point.X, temp_point.Y, 0, 0);
            }
            MousePoint = temp_mouse_point;//.Location;


            var tf = this.TransformToVisual(bluePrint);
            // 计算缩放后的椭圆中心坐标
            if (tf is Matrix matrix)
                temp_mouse_point = MatrixHelper.TransformPoint(matrix, temp_mouse_point);
            foreach (var item in bluePrint.GetFocusNodes())
            {
                if (item is NodeBase node)
                {
                    node.SetOffset(temp_mouse_point);
                }
            }
            Blueprint_Canvas.SetLeft(MouseJoin, temp_mouse_point.X);
            Blueprint_Canvas.SetTop(MouseJoin, temp_mouse_point.Y);


            // 计算缩放后的椭圆中心坐标
            //var tf = this.TransformToVisual(bluePrint);
            //// 计算缩放后的椭圆中心坐标
            //if (tf is Matrix matrix)
            //{
            //    Point scaledCenter = matrix.Transform(MousePoint);
            //    foreach (var item in bluePrint.GetFocusNodes())
            //    {
            //        if (item is NodeBase node)
            //        {
            //            node.SetOffset(scaledCenter);
            //        }
            //    }
            //    Blueprint_Canvas.SetLeft(MouseJoin, scaledCenter.X);
            //    Blueprint_Canvas.SetTop(MouseJoin, scaledCenter.Y);
            //}

            if (IsMouseJoin)
            {
                bP_Line.InvalidateVisual();
                bP_Line.RefreshDrawBezier();

                /*var p1 = e.MouseDevice.GetPosition(bluePrint);
                MouseJoin.MarginLeft = p1.X;
                MouseJoin.MarginTop = p1.Y;*/
                
                //Debug.WriteLine($"e.Location11:{p}--{e.Location}");
            }
            //Blueprint_Canvas.SetLeft(MouseJoin, MousePoint.X);
            //Blueprint_Canvas.SetTop(MouseJoin, MousePoint.Y);



            //if (mousePos.HasValue && e.RightButton == MouseButtonState.Pressed)
            //{
            //    //bluePrint.MarginLeft
            //    var aa = e.Location.X - bluePrint.MarginLeft.Value / scale;
            //    var p = e.Location / scale;
            //    var a = p - mousePos.Value;
            //    Matrix matrix = Matrix.Identity;
            //    if (bluePrint.RenderTransform is MatrixTransform transform)
            //    {
            //        matrix = transform.Value;
            //    }

            //    matrix.TranslatePrepend(a.X, a.Y);
            //    System.Diagnostics.Debug.WriteLine(a);
            //    bluePrint.RenderTransform = new MatrixTransform(matrix);
            //    mousePos = p;
            //}
            //Parent.Invalidate();
        }

    }
}
