using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace FLY.ControlLibrary
{
    /// <summary>
    /// BUTTON 移动行为,附加行为。
    /// </summary>
    public class DrapMoveXBehavior : Behavior<UIElement>
    {

        //
        // 摘要:
        //     发生时 System.Windows.Controls.Button 时发生。
        [Category("Behavior")]
        public event EventHandler Move;

        bool isDown = false;
        Point downPoint;
        /// <summary>
        /// Move 事件滞后5秒触发
        /// </summary>
        public bool IsMoveEventDelayTrigger { get; set; } = true;


        T GetParent<T>(DependencyObject dobj) where T : DependencyObject
        {
            //一直向上找,直到找到window
            do
            {
                if (dobj is T)
                {
                    break;
                }
                dobj = System.Windows.Media.VisualTreeHelper.GetParent(dobj);
            } while (dobj != null);
            return (T)dobj;
        }


        DispatcherTimer timer = new DispatcherTimer();
        /// <summary>
        /// 
        /// </summary>
        public DrapMoveXBehavior()
        {
            timer.Tick += Timer_Tick;
            timer.Interval = TimeSpan.FromSeconds(3);
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            Move?.Invoke(AssociatedObject, null);
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();
            UIElement dobj = AssociatedObject;

            dobj.MouseRightButtonDown += Dobj_MouseRightButtonDown;
            dobj.MouseRightButtonUp += Dobj_MouseRightButtonUp;
            dobj.MouseMove += Dobj_MouseMove;
            dobj.MouseLeave += Dobj_MouseLeave;

            parent = GetParent<Grid>(dobj);
        }

        private void Dobj_MouseLeave(object sender, MouseEventArgs e)
        {
            isDown = false;
        }

        private void Dobj_MouseMove(object sender, MouseEventArgs e)
        {


            //string json = Newtonsoft.Json.JsonConvert.SerializeObject(e);
            if (!isDown)
                return;
            FrameworkElement fe = (FrameworkElement)AssociatedObject;
            var downPoint = e.GetPosition(parent);

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(downPoint);


            //移动
            int x = (int)(downPoint.X - this.downPoint.X);
            if (Math.Abs(x) < 2)
                return;

            this.downPoint = downPoint;
            var m = fe.Margin;
            if (fe.HorizontalAlignment == HorizontalAlignment.Center)
            {
                m.Right -= x * 2;
            }
            else if(fe.HorizontalAlignment == HorizontalAlignment.Right) 
            { 
                m.Right -= x;
            }
            else if(fe.HorizontalAlignment == HorizontalAlignment.Left)
            {
                m.Left += x;
            }
        
            
            fe.Margin = m;
            if (IsMoveEventDelayTrigger)
            {
                timer.Start();
            }
            else
            {
                Move?.Invoke(AssociatedObject, null);
            }
        }
        Grid parent;
        private void Dobj_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            downPoint = e.GetPosition(parent);
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(downPoint);
            isDown = true;
        }

        private void Dobj_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            isDown = false;
        }

        /// <summary>
        /// 
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();
            timer.Stop();

            UIElement dobj = AssociatedObject;

            dobj.MouseRightButtonDown -= Dobj_MouseRightButtonDown;
            dobj.MouseRightButtonUp -= Dobj_MouseRightButtonUp;
            dobj.MouseMove -= Dobj_MouseMove;
            dobj.MouseLeave -= Dobj_MouseLeave;
        }
    }
}