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;
using System.Windows.Media.Animation;
using MECF.Framework.UI.Client.IndustrialControl.Converters;
namespace MECF.Framework.UI.Client.IndustrialControl
{
    /// 
    /// UserControl1.xaml 的交互逻辑
    /// 
    public partial class GasSingularLine : UserControl
    {
        #region Contructor
        /// 
        /// 实例化一个管道对象
        /// 
        public GasSingularLine()
        {
            InitializeComponent();
            //Binding binding = new Binding( );
            //binding.Source = grid1;
            //binding.Path = new PropertyPath( "ActualHeight" );
            //binding.Converter = new MultiplesValueConverter( );
            //binding.ConverterParameter = -1;
            //ellipe1.SetBinding( Canvas.TopProperty, binding );
        }
        #endregion
        #region Property Dependency
        #region LeftDirection Property
        /// 
        /// 设置左边的方向
        /// 
        public HslPipeTurnDirection LeftDirection
        {
            get { return (HslPipeTurnDirection)GetValue(LeftDirectionProperty); }
            set { SetValue(LeftDirectionProperty, value); }
        }
        // Using a DependencyProperty as the backing store for LeftDirection.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LeftDirectionProperty =
            DependencyProperty.Register("LeftDirection", typeof(HslPipeTurnDirection), typeof(GasSingularLine),
                new PropertyMetadata(HslPipeTurnDirection.Left, new PropertyChangedCallback(LeftDirectionPropertyChangedCallback)));
        public static void LeftDirectionPropertyChangedCallback(System.Windows.DependencyObject dependency, System.Windows.DependencyPropertyChangedEventArgs e)
        {
            GasSingularLine pipeLine = (GasSingularLine)dependency;
            pipeLine.UpdateLeftDirectionBinding();
        }
        public void UpdateLeftDirectionBinding()
        {
            BindingOperations.ClearBinding(ellipe1, Canvas.TopProperty);
            if (LeftDirection == HslPipeTurnDirection.Left)
            {
                canvas1.Visibility = Visibility.Visible;
                Binding binding = new Binding();
                binding.Source = grid1;
                binding.Path = new PropertyPath("ActualHeight");
                binding.Converter = new MultiplesValueConverter();
                binding.ConverterParameter = 0;
                ellipe1.SetBinding(Canvas.TopProperty, binding);
            }
            else if (LeftDirection == HslPipeTurnDirection.Right)
            {
                canvas1.Visibility = Visibility.Visible;
                Binding binding = new Binding();
                binding.Source = grid1;
                binding.Path = new PropertyPath("ActualHeight");
                binding.Converter = new MultiplesValueConverter();
                binding.ConverterParameter = -1;
                ellipe1.SetBinding(Canvas.TopProperty, binding);
            }
            else
            {
                canvas1.Visibility = Visibility.Collapsed;
            }
            //  UpdatePathData();
        }
        #endregion
        #region RightDirection Property
        /// 
        /// 设置右边的方向
        /// 
        public HslPipeTurnDirection RightDirection
        {
            get { return (HslPipeTurnDirection)GetValue(RightDirectionProperty); }
            set { SetValue(RightDirectionProperty, value); }
        }
        // Using a DependencyProperty as the backing store for LeftDirection.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RightDirectionProperty =
            DependencyProperty.Register("RightDirection", typeof(HslPipeTurnDirection), typeof(GasSingularLine),
                new PropertyMetadata(HslPipeTurnDirection.Right, new PropertyChangedCallback(RightDirectionPropertyChangedCallback)));
        public static void RightDirectionPropertyChangedCallback(System.Windows.DependencyObject dependency, System.Windows.DependencyPropertyChangedEventArgs e)
        {
            GasSingularLine pipeLine = (GasSingularLine)dependency;
            pipeLine.UpdateRightDirectionBinding();
        }
        public void UpdateRightDirectionBinding()
        {
            BindingOperations.ClearBinding(ellipe2, Canvas.TopProperty);
            if (RightDirection == HslPipeTurnDirection.Left)
            {
                canvas2.Visibility = Visibility.Visible;
                Binding binding = new Binding();
                binding.Source = grid1;
                binding.Path = new PropertyPath("ActualHeight");
                binding.Converter = new MultiplesValueConverter();
                binding.ConverterParameter = 0;
                ellipe2.SetBinding(Canvas.TopProperty, binding);
            }
            else if (RightDirection == HslPipeTurnDirection.Right)
            {
                canvas2.Visibility = Visibility.Visible;
                Binding binding = new Binding();
                binding.Source = grid1;
                binding.Path = new PropertyPath("ActualHeight");
                binding.Converter = new MultiplesValueConverter();
                binding.ConverterParameter = -1;
                ellipe2.SetBinding(Canvas.TopProperty, binding);
            }
            else
            {
                canvas2.Visibility = Visibility.Collapsed;
            }
            //  UpdatePathData();
        }
        #endregion
        #region PipeLineActive Property
        public bool PipeLineActive
        {
            get { return (bool)GetValue(PipeLineActiveProperty); }
            set { SetValue(PipeLineActiveProperty, value); }
        }
        // Using a DependencyProperty as the backing store for PipeLineActive.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PipeLineActiveProperty =
            DependencyProperty.Register("PipeLineActive", typeof(bool), typeof(GasSingularLine), new PropertyMetadata(false));
        #endregion
        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            //   UpdatePathData();
            base.OnRenderSizeChanged(sizeInfo);
        }
        #region LineOffect Property
        public double LineOffect
        {
            get { return (double)GetValue(LineOffectProperty); }
            set { SetValue(LineOffectProperty, value); }
        }
        // Using a DependencyProperty as the backing store for LineOffect.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LineOffectProperty =
            DependencyProperty.Register("LineOffect", typeof(double), typeof(GasSingularLine), new PropertyMetadata(0d));
        StreamGeometry sg = new StreamGeometry();
        public void UpdatePathData()
        {
            //Console.WriteLine( "Size Changed" );
            double height = Height;
            double width = Width;
            using (StreamGeometryContext context = sg.Open())
            {
                if (LeftDirection == HslPipeTurnDirection.Left)
                {
                    context.BeginFigure(new Point(height * 0.5, height), false, false);
                    context.ArcTo(new Point(height, height * 0.5), new Size(height * 0.5, height * 0.5), 0, false, SweepDirection.Clockwise, true, false);
                }
                else if (LeftDirection == HslPipeTurnDirection.Right)
                {
                    context.BeginFigure(new Point(height * 0.5, 0), false, false);
                    context.ArcTo(new Point(height, height * 0.5), new Size(height * 0.5, height * 0.5), 0, false, SweepDirection.Counterclockwise, true, false);
                }
                else
                {
                    context.BeginFigure(new Point(0, height * 0.5), false, false);
                    context.LineTo(new Point(height, height * 0.5), true, false);
                }
                context.LineTo(new Point(width - height, height * 0.5), true, false);
                if (RightDirection == HslPipeTurnDirection.Left)
                {
                    context.ArcTo(new Point(width - height * 0.5, height), new Size(height * 0.5, height * 0.5), 0, false, SweepDirection.Clockwise, true, false);
                }
                else if (RightDirection == HslPipeTurnDirection.Right)
                {
                    context.ArcTo(new Point(width - height * 0.5, 0), new Size(height * 0.5, height * 0.5), 0, false, SweepDirection.Counterclockwise, true, false);
                }
                else
                {
                    context.LineTo(new Point(width, height * 0.5), true, false);
                }
            }
            //sg.Freeze();
            path1.Data = sg;
        }
        #endregion
        #region MoveSpeed Property
        /// 
        /// 获取或设置流动的速度
        /// 
        public double MoveSpeed
        {
            get { return (double)GetValue(MoveSpeedProperty); }
            set { SetValue(MoveSpeedProperty, value); }
        }
        // Using a DependencyProperty as the backing store for MoveSpeed.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MoveSpeedProperty =
            DependencyProperty.Register("MoveSpeed", typeof(double), typeof(GasSingularLine), new PropertyMetadata(0.0d, new PropertyChangedCallback(MoveSpeedPropertyChangedCallback)));
        public static void MoveSpeedPropertyChangedCallback(DependencyObject dependency, DependencyPropertyChangedEventArgs e)
        {
            GasSingularLine pipeLine = (GasSingularLine)dependency;
            pipeLine.UpdateMoveSpeed();
        }
        private Storyboard storyboard = new Storyboard();
        public void UpdateMoveSpeed()
        {
            //jet炉管删除流向动画
            if (path1.Data != null)
            {
                path1.Data = null;
                //offectDoubleAnimation.FillBehavior = FillBehavior.Stop;
                //offectDoubleAnimation = null;
                path1.Visibility = Visibility.Hidden;
            }
            return;
            if (MoveSpeed > 0)
            {
                UpdatePathData();
                path1.Visibility = Visibility.Visible;
                VisualStateManager.GoToState(this, "WEFlowState", false);
                //offectDoubleAnimation = new DoubleAnimation(0d, 10d, TimeSpan.FromMilliseconds(300 / MoveSpeed));
                //offectDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
                //BeginAnimation(LineOffectProperty, offectDoubleAnimation);
                //Storyboard.SetTarget(offectDoubleAnimation, path1);
                //Storyboard.SetTargetProperty(offectDoubleAnimation, "X");
                //storyboard.Children.Add(offectDoubleAnimation);
                //storyboard.Begin();
            }
            else if (MoveSpeed < 0)
            {
                UpdatePathData();
                path1.Visibility = Visibility.Visible;
                VisualStateManager.GoToState(this, "EWFlowState", false);
                //offectDoubleAnimation = new DoubleAnimation(0d, -10d, TimeSpan.FromMilliseconds(300 / Math.Abs(MoveSpeed)));
                //offectDoubleAnimation.FillBehavior = FillBehavior.HoldEnd;
                //offectDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
                //BeginAnimation(LineOffectProperty, offectDoubleAnimation);
                //Storyboard.SetTarget(offectDoubleAnimation, path1);
                ////Storyboard.SetTargetProperty(offectDoubleAnimation, "(UIElement.RenderTransform).(CompositeTransform.ScaleY)");
                //storyboard.Children.Add(offectDoubleAnimation);
                //storyboard.Begin();
            }
            else
            {
                if (path1.Data != null)
                {
                    path1.Data = null;
                    //offectDoubleAnimation.FillBehavior = FillBehavior.Stop;
                    //offectDoubleAnimation = null;
                    path1.Visibility = Visibility.Hidden;
                }
            }
        }
        #endregion
        #region CenterColor Property
        /// 
        /// 管道的中心颜色
        /// 
        public Color CenterColor
        {
            get { return (Color)GetValue(CenterColorProperty); }
            set { SetValue(CenterColorProperty, value); }
        }
        // Using a DependencyProperty as the backing store for CenterColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CenterColorProperty =
            DependencyProperty.Register("CenterColor", typeof(Color), typeof(GasSingularLine), new PropertyMetadata(Colors.Green));
        #endregion
        #region PipeLineWidth Property
        /// 
        /// 管道活动状态时的中心线的线条宽度
        /// 
        public int PipeLineWidth
        {
            get { return (int)GetValue(PipeLineWidthProperty); }
            set { SetValue(PipeLineWidthProperty, value); }
        }
        // Using a DependencyProperty as the backing store for PipeLineWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PipeLineWidthProperty =
            DependencyProperty.Register("PipeLineWidth", typeof(int), typeof(GasSingularLine), new PropertyMetadata(2));
        #endregion
        #region ActiveLineCenterColor Property
        /// 
        /// 管道活动状态时的中心线的颜色信息
        /// 
        public Color ActiveLineCenterColor
        {
            get { return (Color)GetValue(ActiveLineCenterColorProperty); }
            set { SetValue(ActiveLineCenterColorProperty, value); }
        }
        // Using a DependencyProperty as the backing store for ActiveLineCenterColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ActiveLineCenterColorProperty =
            DependencyProperty.Register("ActiveLineCenterColor", typeof(Color), typeof(GasSingularLine), new PropertyMetadata(Colors.DodgerBlue));
        #endregion
        #region MyRegion
        /// 
        /// 管道控件的边缘颜色
        /// 
        public Color EdgeColor
        {
            get { return (Color)GetValue(EdgeColorProperty); }
            set { SetValue(EdgeColorProperty, value); }
        }
        // Using a DependencyProperty as the backing store for EdgeColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EdgeColorProperty =
            DependencyProperty.Register("EdgeColor", typeof(Color), typeof(GasSingularLine), new PropertyMetadata(Colors.Black));
        #endregion
        #endregion
    }
    
}