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.Media;
using System.Windows.Media.Animation;

namespace Bellwether.Utility
{
    public static class SbAssist
    {
        /// <summary>
        /// FlashDirection denoted by 3*3 position to direction {}
        /// </summary>
        public enum FlashDirection
        {
            direction64,
            direction91,
        }

        public static Storyboard CreateFlashStoryBoard(UIElement flashImg, double width = 1, double duration = 1000, FlashDirection direction = FlashDirection.direction64)
        {
            //if (wireImgContainer.Children.Count != 2)
            //    return null;
            var _flashImg = flashImg;///.Children[1];
            Point[] startPoints = null;
            Point[] endPoints = null;
            if (direction == FlashDirection.direction64)
            {
                startPoints = new Point[] { new Point(1f, 0), new Point(width * -0.1f, 0) };
                endPoints = new Point[] { new Point(1 + width * 0.1f, 0), new Point(0f, 0) };
            }
            else if (direction == FlashDirection.direction91)
            {
                startPoints = new Point[] { new Point(1f, 1f), new Point(width * -0.1f, width * -0.1f) };
                endPoints = new Point[] { new Point(1 + width * 0.1f, 1 + width * 0.1f), new Point(0f, 0) };
            }

            _flashImg.OpacityMask = CreateFlashOpacityMask();
            Storyboard flashSb = new Storyboard() { BeginTime = TimeSpan.FromSeconds(0), Duration = TimeSpan.FromMilliseconds(duration) };

            var startPA = new PointAnimation(startPoints[0], startPoints[1], TimeSpan.FromMilliseconds(duration));
            Storyboard.SetTarget(startPA, (DependencyObject)_flashImg);
            Storyboard.SetTargetProperty(startPA, new PropertyPath("OpacityMask.StartPoint"));
            flashSb.Children.Add(startPA);

            var endPA = new PointAnimation(endPoints[0], endPoints[1], TimeSpan.FromMilliseconds(duration));
            Storyboard.SetTarget(endPA, (DependencyObject)_flashImg);
            Storyboard.SetTargetProperty(endPA, new PropertyPath("OpacityMask.EndPoint"));
            flashSb.Children.Add(endPA);

            flashSb.Begin();
            return flashSb;
        }

        public static Brush CreateFlashOpacityMask()
        {
            //var om = (LinearGradientBrush)FindResource("rsFlashMask");
            var om = new LinearGradientBrush();
            om.MappingMode = BrushMappingMode.RelativeToBoundingBox;
            om.StartPoint = new Point(0, 0);
            om.EndPoint = new Point(1, 0);
            om.GradientStops = new GradientStopCollection()
            {
                new GradientStop(){ Offset = 0, Color = Colors.Transparent },
                new GradientStop() { Offset = 0.3, Color = Colors.White },
                new GradientStop() { Offset = 0.6, Color = Colors.White },
                new GradientStop() { Offset = 1, Color = Colors.Transparent }
            };
            return om;
        }

        public static Storyboard CreateOpacitySb(DependencyObject dependencyObject, double from, double to, double duration, bool isLoop = false, bool isReverse = false)
        {
            var sb = new Storyboard();
            var da = new DoubleAnimation(from, to, TimeSpan.FromMilliseconds(duration));
            if (isLoop)
                da.RepeatBehavior = RepeatBehavior.Forever;
            if (isReverse)
                da.AutoReverse = true;
            Storyboard.SetTarget(da, dependencyObject);
            Storyboard.SetTargetProperty(da, new PropertyPath(FrameworkElement.OpacityProperty));
            sb.Children.Add(da);
            return sb;
        }

        public static Storyboard CreateOpacityBlinkSb(DependencyObject dependencyObject, double from, double to, double duration, double sustainDuration = 0, double timeInterval = 1000, bool IsLoop = true)
        {
            var sb = new Storyboard();
            var a1 = CreateOpacitySb(dependencyObject, from, to, duration);
            a1.BeginTime = TimeSpan.FromMilliseconds(0);
            sb.Children.Add(a1);
            var a2 = CreateOpacitySb(dependencyObject, to, to, sustainDuration);
            a2.BeginTime = TimeSpan.FromMilliseconds(duration);
            sb.Children.Add(a2);
            var a3 = CreateOpacitySb(dependencyObject, to, from, duration);
            a3.BeginTime = TimeSpan.FromMilliseconds(duration + sustainDuration);
            sb.Children.Add(a3);
            var a4 = CreateOpacitySb(dependencyObject, from, from, timeInterval);
            a4.BeginTime = TimeSpan.FromMilliseconds(duration * 2 + sustainDuration);
            sb.Children.Add(a4);
            if (IsLoop)
                sb.RepeatBehavior = RepeatBehavior.Forever;
            return sb;
        }

        public static Storyboard CreateRotationSb(FrameworkElement frameworkElement, double from, double to, double duration, bool isLoop = false, bool isReverse = false)
        {
            //check property
            if (frameworkElement.Width == 0 || frameworkElement.Height == 0)
                throw new Exception("Rotation Stortyboard need static Width and Height");
            frameworkElement.RenderTransform = new RotateTransform() { CenterX = frameworkElement.Width / 2, CenterY = frameworkElement.Height / 2 };

            var sb = new Storyboard();
            var da = new DoubleAnimation(from, to, TimeSpan.FromMilliseconds(duration));
            if (isLoop)
                da.RepeatBehavior = RepeatBehavior.Forever;
            if (isReverse)
                da.AutoReverse = true;
            Storyboard.SetTarget(da, frameworkElement);
            Storyboard.SetTargetProperty(da, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
            sb.Children.Add(da);
            return sb;
        }
    }
}
