using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using SPC.Kiosk.Base;
namespace SPC.Kiosk.Common
{
    /// <summary>
    /// Animation Actions
    /// </summary>
    public static class Animations
    {
        /// <summary>
        /// ScrollAnimation for 
        /// </summary>
        /// <param name="_animationTarget"></param>
        /// <param name="_animationType"></param>
        /// <param name="_fromValue"></param>
        /// <param name="_toValue"></param>
        /// <param name="_sconds"></param>
        /// <returns></returns>
        public static Storyboard GetScrollAnimation(FrameworkElement _animationTarget, Orientation _animationType, double _fromValue, double _toValue, double _sconds = 0.3)
        {
            Storyboard resultStoryboard = null;
            try
            {
                resultStoryboard = new Storyboard();
                var totalDuration = new Duration(TimeSpan.FromSeconds(_sconds));
                var scrollAnimation = new DoubleAnimation(_fromValue, _toValue, totalDuration);
                Storyboard.SetTarget(scrollAnimation, _animationTarget);
                Storyboard.SetTargetProperty(scrollAnimation, new PropertyPath(_animationType.Equals(Orientation.Horizontal) ? "HorizontalOffset" : "VerticalOffset"));
                resultStoryboard.Children.Add(scrollAnimation);
            }
            catch
            {
                resultStoryboard = null;
            }
            return resultStoryboard;
        }
        /// <summary>
        /// Create MoveAnimation with Opacity 
        /// </summary>
        /// <param name="_animationTarget"></param>
        /// <param name="_animationType"></param>
        /// <param name="_fromValue"></param>
        /// <param name="_toValue"></param>
        /// <param name="_opacityFrom"></param>
        /// <param name="_opacityTo"></param>
        /// <param name="_sconds"></param>
        /// <returns></returns>
        public static List<DoubleAnimation> GetMoveAnimation(FrameworkElement _animationTarget, OpenCloseAnimationType _animationType, double _fromValue, double _toValue, double _opacityFrom, double _opacityTo, double _sconds)
        {
            List<DoubleAnimation> resultAnimation = null;
            try
            {
                resultAnimation = new List<DoubleAnimation>();
                var totalDuration = new Duration(TimeSpan.FromSeconds(_sconds));
                var opacityAnimation = new DoubleAnimation(_opacityFrom, _opacityTo, totalDuration);
                //var centerX = _animationTarget.ActualWidth / 2;
                //var centerY = _animationTarget.ActualHeight / 2;
                var centerX = _animationTarget.Width / 2;
                var centerY = _animationTarget.Height / 2;
                Storyboard.SetTarget(opacityAnimation, _animationTarget);
                Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
                resultAnimation.Add(opacityAnimation);
                
                switch (_animationType)
                {
                    case OpenCloseAnimationType.GotoLeft:
                    case OpenCloseAnimationType.GotoRight:
                        _animationTarget.RenderTransform = new TranslateTransform(0, 0);
                        var addAnimation = new DoubleAnimation(_fromValue, _toValue, totalDuration);
                        Storyboard.SetTarget(addAnimation, _animationTarget);
                        Storyboard.SetTargetProperty(addAnimation, new PropertyPath("RenderTransform.X"));
                        resultAnimation.Add(addAnimation);
                        break;
                    case OpenCloseAnimationType.GotoUp:
                    case OpenCloseAnimationType.GotoDown:
                        _animationTarget.RenderTransform = new TranslateTransform(0, 0);
                        var add2Animation = new DoubleAnimation(_fromValue, _toValue, totalDuration);
                        Storyboard.SetTarget(add2Animation, _animationTarget);
                        Storyboard.SetTargetProperty(add2Animation, new PropertyPath("RenderTransform.Y"));
                        resultAnimation.Add(add2Animation);
                        break;
                    case OpenCloseAnimationType.FullSizeDown:
                    case OpenCloseAnimationType.FullSizeUp:
                        _animationTarget.RenderTransform = new ScaleTransform(1, 1, centerX, centerY);
                        var flipXAni = new DoubleAnimation(_animationType.Equals(OpenCloseAnimationType.FullSizeUp) ? 0 : 1
                                                         , _animationType.Equals(OpenCloseAnimationType.FullSizeUp) ? 1 : 0
                                                         , totalDuration);
                        var flipYAni = new DoubleAnimation(_animationType.Equals(OpenCloseAnimationType.FullSizeUp) ? 0 : 1
                                                         , _animationType.Equals(OpenCloseAnimationType.FullSizeUp) ? 1 : 0
                                                         , totalDuration);
                        Storyboard.SetTarget(flipXAni, _animationTarget);
                        Storyboard.SetTarget(flipYAni, _animationTarget);
                        Storyboard.SetTargetProperty(flipXAni, new PropertyPath("RenderTransform.ScaleX"));
                        Storyboard.SetTargetProperty(flipYAni, new PropertyPath("RenderTransform.ScaleY"));
                        resultAnimation.Add(flipXAni);
                        resultAnimation.Add(flipYAni);
                        break;
                }
            }
            catch
            {
                resultAnimation = null;
            }
            return resultAnimation;
        }
        /// <summary>
        /// Close and Open Animation
        /// </summary>
        /// <param name="_closeTarget"></param>
        /// <param name="_openTarget"></param>
        /// <param name="_animationType"></param>
        /// <param name="_objectSize"></param>
        /// <param name="_sconds"></param>
        /// <returns></returns>
        public static Storyboard GetOpenAndCloseAnimation(FrameworkElement _closeTarget, FrameworkElement _openTarget, OpenCloseAnimationType _animationType, double _objectSize, double _sconds = 0.5)
        {
            return GetOpenAndCloseAnimation(_closeTarget, _openTarget, _animationType, _objectSize, 1, _sconds);
        }
        /// <summary>
        /// Close and Open Animation
        /// </summary>
        /// <param name="_closeTarget"></param>
        /// <param name="_openTarget"></param>
        /// <param name="_animationType"></param>
        /// <param name="_objectSize"></param>
        /// <param name="_sconds"></param>
        /// <returns></returns>
        public static Storyboard GetOpenAndCloseAnimation(FrameworkElement _closeTarget, FrameworkElement _openTarget, OpenCloseAnimationType _animationType, double _objectSize, double _opacityValue , double _sconds = 0.5)
        {
            Storyboard resultStoryboard = null;
            try
            {
                resultStoryboard = new Storyboard();
                _openTarget.Width = _objectSize;
                _openTarget.Visibility = Visibility.Visible;
                _closeTarget.IsEnabled = false;
                double toValue = _objectSize;
                switch (_animationType)
                {
                    case OpenCloseAnimationType.GotoLeft:
                    case OpenCloseAnimationType.GotoUp:
                        toValue = toValue * -1;
                        break;
                }
                foreach (var aAnimation in GetMoveAnimation(_closeTarget, _animationType, 0, toValue, 1, _opacityValue, _sconds)) resultStoryboard.Children.Add(aAnimation);
                double fromValue = _objectSize;
                switch (_animationType)
                {
                    case OpenCloseAnimationType.GotoRight:
                    case OpenCloseAnimationType.GotoDown:
                        fromValue = fromValue * -1;
                        break;
                }
                foreach (var aAnimation in GetMoveAnimation(_openTarget, _animationType, fromValue, 0, _opacityValue, 1, _sconds)) resultStoryboard.Children.Add(aAnimation);
            }
            catch
            {
                resultStoryboard = null;
            }
            return resultStoryboard;
        }
        /// <summary>
        /// Open Animation
        /// </summary>
        /// <param name="_animationTarget"></param>
        /// <param name="_animationType"></param>
        /// <param name="_objectSize"></param>
        /// <param name="_sconds"></param>
        /// <returns></returns>
        public static Storyboard GetOpenAnimation(FrameworkElement _animationTarget, OpenCloseAnimationType _animationType, double _objectSize, double _sconds = 0.5)
        {
            return GetOpenAnimation( _animationTarget,  _animationType,  _objectSize, 0,  _sconds );
        }
        /// <summary>
        /// Open Animation
        /// </summary>
        /// <param name="_animationTarget"></param>
        /// <param name="_animationType"></param>
        /// <param name="_objectSize"></param>
        /// <param name="_sconds"></param>
        /// <returns></returns>
        public static Storyboard GetOpenAnimation(FrameworkElement _animationTarget, OpenCloseAnimationType _animationType, double _objectSize, double _opacityValue, double _sconds = 0.5)
        {
            Storyboard resultStoryboard = null;
            try
            {
                resultStoryboard = new Storyboard();
                switch (_animationType)
                {
                    case OpenCloseAnimationType.GotoLeft:
                    case OpenCloseAnimationType.GotoRight:
                        _animationTarget.Width = _objectSize;
                        break;
                    case OpenCloseAnimationType.GotoUp:
                    case OpenCloseAnimationType.GotoDown:
                        _animationTarget.Height = _objectSize;
                        break;
                }
                _animationTarget.Visibility = Visibility.Visible;
                double fromValue = _objectSize;
                switch (_animationType)
                {
                    case OpenCloseAnimationType.GotoRight:
                    case OpenCloseAnimationType.GotoDown:
                        fromValue = fromValue * -1;
                        break;
                }
                foreach (var aAnimation in GetMoveAnimation(_animationTarget, _animationType, fromValue, 0, _opacityValue, 1, _sconds)) resultStoryboard.Children.Add(aAnimation); 
            }
            catch 
            {
                resultStoryboard = null;
            }
            return resultStoryboard;
        }
        /// <summary>
        /// Close Animation
        /// </summary>
        /// <param name="_animationTarget"></param>
        /// <param name="_animationType"></param>
        /// <param name="_objectSize"></param>
        /// <param name="_sconds"></param>
        /// <returns></returns>
        public static Storyboard GetCloseAnimation(FrameworkElement _animationTarget, OpenCloseAnimationType _animationType, double _objectSize,  double _sconds = 0.5)
        {
            return GetCloseAnimation(_animationTarget,  _animationType,  _objectSize, 0,  _sconds);
        }
        /// <summary>
        /// Close Animation
        /// </summary>
        /// <param name="_animationTarget"></param>
        /// <param name="_animationType"></param>
        /// <param name="_objectSize"></param>
        /// <param name="_sconds"></param>
        /// <returns></returns>
        public static Storyboard GetCloseAnimation(FrameworkElement _animationTarget, OpenCloseAnimationType _animationType, double _objectSize, double _opacityValue, double _sconds = 0.5)
        {
            Storyboard resultStoryboard = null;
            try
            {
                resultStoryboard = new Storyboard();
                switch (_animationType)
                {
                    case OpenCloseAnimationType.GotoLeft:
                    case OpenCloseAnimationType.GotoRight:
                        _animationTarget.Width = _objectSize;
                        break;
                    case OpenCloseAnimationType.GotoUp:
                    case OpenCloseAnimationType.GotoDown:
                        _animationTarget.Height = _objectSize;
                        break;
                }
                _animationTarget.IsEnabled = false;
                double toValue = _objectSize;
                switch (_animationType)
                {
                    case OpenCloseAnimationType.GotoLeft:
                    case OpenCloseAnimationType.GotoUp:
                        toValue = toValue * -1;
                        break;
                }
                foreach (var aAnimation in GetMoveAnimation(_animationTarget, _animationType, 0, toValue, 1, _opacityValue, _sconds)) resultStoryboard.Children.Add(aAnimation);
            }
            catch
            {
                resultStoryboard = null;
            }
            return resultStoryboard;
        }
        /// <summary>
        /// Get a Shutter Animation Storyboard
        /// </summary>
        /// <param name="_animationTarget"></param>
        /// <param name="_directionType"></param>
        /// <param name="_fromValue"></param>
        /// <param name="_toValue"></param>
        /// <param name="_opacityFrom"></param>
        /// <param name="_opacityTo"></param>
        /// <param name="_sconds"></param>
        /// <returns></returns>
        public static Storyboard GetShutterAnimation(FrameworkElement _animationTarget, ShutterAnchor _directionType, double _fromValue, double _toValue,double _opacityFrom, double _opacityTo, double _sconds=0.5)
        {
            Storyboard gridAnimation = null;
            try
            {
                var targetType = _animationTarget.GetType();
                _animationTarget.RenderTransform = new TranslateTransform(0, 0);
                gridAnimation = new Storyboard(); 
                string targetProperty = _directionType.Equals(ShutterAnchor.Left) || _directionType.Equals(ShutterAnchor.Right) ? "Width":"Height";
                switch (_directionType)
                {
                    case ShutterAnchor.Left:
                        _animationTarget.HorizontalAlignment = HorizontalAlignment.Left;
                        //var sizeBounds1 = new DoubleAnimation(0, _animationTarget.Width * -1, new Duration(TimeSpan.FromSeconds(_sconds)));
                        //Storyboard.SetTarget(sizeBounds1, _animationTarget);
                        //Storyboard.SetTargetProperty(sizeBounds1, new PropertyPath("RenderTransform.X"));
                        //gridAnimation.Children.Add(sizeBounds1);

                        break;
                    case ShutterAnchor.Right:
                        _animationTarget.HorizontalAlignment = HorizontalAlignment.Right;
                        //var sizeAni = new DoubleAnimation(_animationTarget.Width * -1, 0, new Duration(TimeSpan.FromSeconds(_sconds)));
                        //Storyboard.SetTarget(sizeAni, _animationTarget);
                        //Storyboard.SetTargetProperty(sizeAni, new PropertyPath("RenderTransform.X"));
                        //gridAnimation.Children.Add(sizeAni);
                        break;
                    case ShutterAnchor.Top:
                        _animationTarget.VerticalAlignment = VerticalAlignment.Top;
                        break;
                    case ShutterAnchor.Bottom:
                        _animationTarget.VerticalAlignment = VerticalAlignment.Bottom;
                        break;
                }
                var sizeAni = new DoubleAnimation(_fromValue, _toValue, new Duration(TimeSpan.FromSeconds(_sconds)));
                Storyboard.SetTarget(sizeAni, _animationTarget);
                Storyboard.SetTargetProperty(sizeAni, new PropertyPath(targetProperty));
                gridAnimation.Children.Add(sizeAni);

                var opacityAni = new DoubleAnimation(_opacityFrom, _opacityTo, new Duration(TimeSpan.FromSeconds(_sconds)));
                Storyboard.SetTarget(opacityAni, _animationTarget);
                Storyboard.SetTargetProperty(opacityAni, new PropertyPath("Opacity"));
                gridAnimation.Children.Add(opacityAni);

            }
            catch
            {
                gridAnimation = null;
            }
            return gridAnimation;
        }
        /// <summary>
        /// Get a Button Animation
        /// </summary>
        /// <param name="_target">Target</param>
        /// <param name="_clickAnimationType">Animation Type</param>
        /// <returns></returns>
        public static Storyboard GetClickAnimation(FrameworkElement _target, ButtonAnimationType _clickAnimationType,double _durationSeconds = 0.2)
        {
            Storyboard targetAnimation = null;
            try
            {
                targetAnimation = new Storyboard();

                var boucedHeight = 0d;
                var sizeScale = 1d;
                switch (_clickAnimationType)
                {
                    case ButtonAnimationType.BounceUp:
                        boucedHeight = _target.ActualHeight * -0.2;
                        break;
                    case ButtonAnimationType.TextBounceUp:
                        boucedHeight = _target.ActualHeight * -0.3;
                        break;
                    case ButtonAnimationType.BounceDown:
                        boucedHeight = _target.ActualHeight * 0.2;
                        break;
                    case ButtonAnimationType.TextBounceDown:
                        boucedHeight = _target.ActualHeight * 0.3;
                        break;
                    case ButtonAnimationType.SizeDown:
                    case ButtonAnimationType.TextSizeDown:
                        sizeScale = 0.85;
                        break;
                    case ButtonAnimationType.SizeUp:
                    case ButtonAnimationType.TextSizeUp:
                        sizeScale = 1.15;
                        break;
                    case ButtonAnimationType.FlipX:
                    case ButtonAnimationType.FlipY:
                        sizeScale = 0;
                        break;
                }

                var centerX = _target.ActualWidth / 2;
                var centerY = _target.ActualHeight / 2;
                switch (_clickAnimationType)
                {
                    case ButtonAnimationType.OpacityDown:
                        var opacityDown = new DoubleAnimation(1, 0.5, new Duration(TimeSpan.FromSeconds(_durationSeconds)))
                        {
                            AutoReverse = true
                        };
                        Storyboard.SetTarget(opacityDown, _target);
                        Storyboard.SetTargetProperty(opacityDown, new PropertyPath("Opacity"));
                        targetAnimation.Children.Add(opacityDown);
                        break;
                    case ButtonAnimationType.OpacityUp:
                        var opacityUp = new DoubleAnimation(0.5, 1, new Duration(TimeSpan.FromSeconds(_durationSeconds)))
                        {
                            AutoReverse = true
                        };
                        Storyboard.SetTarget(opacityUp, _target);
                        Storyboard.SetTargetProperty(opacityUp, new PropertyPath("Opacity"));
                        targetAnimation.Children.Add(opacityUp);
                        break;
                    case ButtonAnimationType.BounceUp:
                    case ButtonAnimationType.BounceDown:
                    case ButtonAnimationType.TextBounceUp:
                    case ButtonAnimationType.TextBounceDown:
                        _target.RenderTransform = new TranslateTransform(0, 0);
                        var sizeBounds1 = new DoubleAnimation(0, boucedHeight, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.75)));
                        var sizeBounds2 = new DoubleAnimation(boucedHeight, 0, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.25)));
                        sizeBounds1.BeginTime = TimeSpan.FromSeconds(0);
                        sizeBounds2.BeginTime = TimeSpan.FromSeconds(_durationSeconds * 0.75);
                        Storyboard.SetTarget(sizeBounds1, _target);
                        Storyboard.SetTargetProperty(sizeBounds1, new PropertyPath("RenderTransform.Y"));
                        Storyboard.SetTarget(sizeBounds2, _target);
                        Storyboard.SetTargetProperty(sizeBounds2, new PropertyPath("RenderTransform.Y"));
                        targetAnimation.Duration = TimeSpan.FromSeconds(_durationSeconds);
                        targetAnimation.Children.Add(sizeBounds1);
                        targetAnimation.Children.Add(sizeBounds2);
                        break;
                    case ButtonAnimationType.SizeDown:
                    case ButtonAnimationType.TextSizeDown:
                    case ButtonAnimationType.SizeUp:
                    case ButtonAnimationType.TextSizeUp:
                        _target.RenderTransform = new ScaleTransform(1, 1, centerX, centerY); 
                        var sizeXAni = new DoubleAnimation(1, sizeScale, new Duration(TimeSpan.FromSeconds(_durationSeconds)));
                        var sizeYAni = new DoubleAnimation(1, sizeScale, new Duration(TimeSpan.FromSeconds(_durationSeconds)));
                        sizeXAni.AutoReverse = true;
                        sizeYAni.AutoReverse = true;
                        Storyboard.SetTarget(sizeXAni, _target);
                        Storyboard.SetTargetProperty(sizeXAni, new PropertyPath("RenderTransform.ScaleX"));
                        Storyboard.SetTarget(sizeYAni, _target);
                        Storyboard.SetTargetProperty(sizeYAni, new PropertyPath("RenderTransform.ScaleY"));
                        targetAnimation.Children.Add(sizeXAni);
                        targetAnimation.Children.Add(sizeYAni);
                        break;
                    case ButtonAnimationType.Twist:
                    case ButtonAnimationType.TextTwist:
                        _target.RenderTransform = new RotateTransform(0, centerX, centerY);
                        var sizeAngleLeft = new DoubleAnimation(0, 9, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.25)));
                        var sizeAngleRight = new DoubleAnimation(0, -9, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.5)));
                        var sizeAngleCenter = new DoubleAnimation(-9, 0, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.25)));
                        sizeAngleLeft.BeginTime = TimeSpan.FromSeconds(0);
                        sizeAngleRight.BeginTime = TimeSpan.FromSeconds(_durationSeconds * 0.25);
                        sizeAngleCenter.BeginTime = TimeSpan.FromSeconds(_durationSeconds * 0.75);
                        Storyboard.SetTarget(sizeAngleLeft, _target);
                        Storyboard.SetTargetProperty(sizeAngleLeft, new PropertyPath("RenderTransform.Angle"));
                        Storyboard.SetTarget(sizeAngleRight, _target);
                        Storyboard.SetTargetProperty(sizeAngleRight, new PropertyPath("RenderTransform.Angle"));
                        Storyboard.SetTarget(sizeAngleCenter, _target);
                        Storyboard.SetTargetProperty(sizeAngleCenter, new PropertyPath("RenderTransform.Angle"));
                        targetAnimation.Duration = TimeSpan.FromSeconds(_durationSeconds);
                        targetAnimation.Children.Add(sizeAngleLeft);
                        targetAnimation.Children.Add(sizeAngleRight);
                        targetAnimation.Children.Add(sizeAngleCenter);
                        break;
                    case ButtonAnimationType.FlipX:
                        _target.RenderTransform = new ScaleTransform(1, 1, centerX, centerY);
                        var flipXAni = new DoubleAnimation(1, sizeScale, new Duration(TimeSpan.FromSeconds(_durationSeconds)));
                        flipXAni.AutoReverse = true;
                        Storyboard.SetTarget(flipXAni, _target);
                        Storyboard.SetTargetProperty(flipXAni, new PropertyPath("RenderTransform.ScaleX"));
                        targetAnimation.Children.Add(flipXAni);
                        break;
                    case ButtonAnimationType.FlipY:
                        _target.RenderTransform = new ScaleTransform(1, 1, centerX, centerY);
                        var flipYAni = new DoubleAnimation(1, sizeScale, new Duration(TimeSpan.FromSeconds(_durationSeconds)));
                        flipYAni.AutoReverse = true;
                        Storyboard.SetTarget(flipYAni, _target);
                        Storyboard.SetTargetProperty(flipYAni, new PropertyPath("RenderTransform.ScaleY"));
                        targetAnimation.Children.Add(flipYAni);
                        break;
                    default:
                        targetAnimation = null;
                        break;
                }
            }
            catch 
            {
                targetAnimation = null;
            }
            return targetAnimation;
        }
        /// <summary>
        /// Send Image Animation
        /// </summary>
        /// <param name="_target"></param>
        /// <param name="_sendAnimationType"></param>
        /// <param name="_moveX"></param>
        /// <param name="_moveY"></param>
        /// <param name="_durationSeconds"></param>
        /// <returns></returns>
        public static Storyboard GetSendAnimation(FrameworkElement _target, ButtonSendAnimationType _sendAnimationType, double _moveX,double _moveY, double _durationSeconds = 0.5)
        {
            Storyboard targetAnimation = null;
            try
            {   if (!_sendAnimationType.Equals(ButtonSendAnimationType.None))
                {

                    targetAnimation = new Storyboard();
                    var transformGroup = new TransformGroup();
                    var translateTransform = new TranslateTransform(0, 0);
                    transformGroup.Children.Add(translateTransform);
                    
                    if (_sendAnimationType.Equals(ButtonSendAnimationType.SpeedChage)
                        || _sendAnimationType.Equals(ButtonSendAnimationType.SizeUpDown))
                    {
                        var moveX1 = new DoubleAnimation(0, _moveX * 0.25, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.5)));
                        var moveX2 = new DoubleAnimation(_moveX * 0.25, _moveX, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.5)));
                        var moveY1 = new DoubleAnimation(0, _moveY * 0.25, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.5)));
                        var moveY2 = new DoubleAnimation(_moveY * 0.25, _moveY, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.5)));
                        moveX1.BeginTime = TimeSpan.FromSeconds(0);
                        moveX2.BeginTime = TimeSpan.FromSeconds(_durationSeconds * 0.5);
                        moveY1.BeginTime = TimeSpan.FromSeconds(0);
                        moveY2.BeginTime = TimeSpan.FromSeconds(_durationSeconds * 0.5);
                        Storyboard.SetTarget(moveX1, _target);
                        Storyboard.SetTargetProperty(moveX1, new PropertyPath(string.Format("RenderTransform.Children[{0}].(TranslateTransform.X)", transformGroup.Children.Count - 1)));
                        Storyboard.SetTarget(moveX2, _target);
                        Storyboard.SetTargetProperty(moveX2, new PropertyPath(string.Format("RenderTransform.Children[{0}].(TranslateTransform.X)", transformGroup.Children.Count - 1)));
                        Storyboard.SetTarget(moveY1, _target);
                        Storyboard.SetTargetProperty(moveY1, new PropertyPath(string.Format("RenderTransform.Children[{0}].(TranslateTransform.Y)", transformGroup.Children.Count - 1)));
                        Storyboard.SetTarget(moveY2, _target);
                        Storyboard.SetTargetProperty(moveY2, new PropertyPath(string.Format("RenderTransform.Children[{0}].(TranslateTransform.Y)", transformGroup.Children.Count - 1)));
                        targetAnimation.Duration = TimeSpan.FromSeconds(_durationSeconds);
                        targetAnimation.Children.Add(moveX1);
                        targetAnimation.Children.Add(moveX2);
                        targetAnimation.Children.Add(moveY1);
                        targetAnimation.Children.Add(moveY2);
                        if (_sendAnimationType.Equals(ButtonSendAnimationType.SizeUpDown))
                        {
                            var scaleTransform = new ScaleTransform(1, 1, 0, 0);
                            transformGroup.Children.Add(scaleTransform);
                            var sizeAniX1 = new DoubleAnimation(1, 1.5, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.1)));
                            var sizeAniX2 = new DoubleAnimation(1.5, 1, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.9)));
                            var sizeAniY1 = new DoubleAnimation(1, 1.5, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.1)));
                            var sizeAniY2 = new DoubleAnimation(1.5, 1, new Duration(TimeSpan.FromSeconds(_durationSeconds * 0.9)));
                            sizeAniX1.BeginTime = TimeSpan.FromSeconds(0);
                            sizeAniX2.BeginTime = TimeSpan.FromSeconds(_durationSeconds * 0.1);
                            sizeAniY1.BeginTime = TimeSpan.FromSeconds(0);
                            sizeAniY2.BeginTime = TimeSpan.FromSeconds(_durationSeconds * 0.1);
                            Storyboard.SetTarget(sizeAniX1, _target);
                            Storyboard.SetTargetProperty(sizeAniX1, new PropertyPath(string.Format("RenderTransform.Children[{0}].(ScaleTransform.ScaleX)", transformGroup.Children.Count - 1)));
                            Storyboard.SetTarget(sizeAniX2, _target);
                            Storyboard.SetTargetProperty(sizeAniX2, new PropertyPath(string.Format("RenderTransform.Children[{0}].(ScaleTransform.ScaleX)", transformGroup.Children.Count - 1)));
                            Storyboard.SetTarget(sizeAniY1, _target);
                            Storyboard.SetTargetProperty(sizeAniY1, new PropertyPath(string.Format("RenderTransform.Children[{0}].(ScaleTransform.ScaleY)", transformGroup.Children.Count - 1)));
                            Storyboard.SetTarget(sizeAniY2, _target);
                            Storyboard.SetTargetProperty(sizeAniY2, new PropertyPath(string.Format("RenderTransform.Children[{0}].(ScaleTransform.ScaleY)", transformGroup.Children.Count - 1)));
                            targetAnimation.Children.Add(sizeAniX1);
                            targetAnimation.Children.Add(sizeAniX2);
                            targetAnimation.Children.Add(sizeAniY1);
                            targetAnimation.Children.Add(sizeAniY2);

                        }
                    }
                    else if (_sendAnimationType.Equals(ButtonSendAnimationType.Normal))
                    {
                        var moveX = new DoubleAnimation(0, _moveX, new Duration(TimeSpan.FromSeconds(_durationSeconds)));
                        var moveY = new DoubleAnimation(0, _moveY, new Duration(TimeSpan.FromSeconds(_durationSeconds)));
                        Storyboard.SetTarget(moveX, _target);
                        Storyboard.SetTargetProperty(moveX, new PropertyPath(string.Format("RenderTransform.Children[{0}].(TranslateTransform.X)", transformGroup.Children.Count - 1)));
                        Storyboard.SetTarget(moveY, _target);
                        Storyboard.SetTargetProperty(moveY, new PropertyPath(string.Format("RenderTransform.Children[{0}].(TranslateTransform.Y)", transformGroup.Children.Count - 1)));
                        targetAnimation.Children.Add(moveX);
                        targetAnimation.Children.Add(moveY);
                    }
                    _target.RenderTransform = transformGroup;
                }
            }
            catch 
            {
                targetAnimation = null;
            }
            return targetAnimation;
        }
    }
}
