using BBWY.Controls.Helpers;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace BBWY.Controls
{
    /// <summary>
    /// RoundWaitProgress.xaml 的交互逻辑
    /// </summary>
    public partial class RoundWaitProgress : UserControl
    {
        public RoundWaitProgress()
        {
            InitializeComponent();
            this.Loaded += RoundWaitProgress_Loaded;
            InitAnimationData();
        }

        private void RoundWaitProgress_Loaded(object sender, RoutedEventArgs e)
        {
            if (!IsPlaying)
                this.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// 初始化动画数据
        /// </summary>
        private void InitAnimationData()
        {
            if (ControlList == null)
                ControlList = new List<FrameworkElement>() { g1, g2, g3, g4, g5 };

            if (AngleAnimationParamList == null)
                AngleAnimationParamList = new List<BaseKeyFrame>();
            if (BeginOpacityAnimationParamList == null)
                BeginOpacityAnimationParamList = new List<BaseKeyFrame>();
            if (EndOpacityAnimationParamList == null)
                EndOpacityAnimationParamList = new List<BaseKeyFrame>();

            AngleAnimationParamList.Clear();
            BeginOpacityAnimationParamList.Clear();
            EndOpacityAnimationParamList.Clear();

            AngleAnimationParamList.Add(new BaseKeyFrame() { Value = 0, _KeyTime = new TimeSpan(0, 0, 0, 0, 0) });
            AngleAnimationParamList.Add(new BaseKeyFrame() { Value = 112, _KeyTime = new TimeSpan(0, 0, 0, 0, 400) });
            AngleAnimationParamList.Add(new BaseKeyFrame() { Value = 202, _KeyTime = new TimeSpan(0, 0, 0, 0, 1500) });
            AngleAnimationParamList.Add(new BaseKeyFrame() { Value = 472, _KeyTime = new TimeSpan(0, 0, 0, 0, 2200) });
            AngleAnimationParamList.Add(new BaseKeyFrame() { _KeyTime = new TimeSpan(0, 0, 0, 0, 3100), Value = 562 });
            AngleAnimationParamList.Add(new BaseKeyFrame() { _KeyTime = new TimeSpan(0, 0, 0, 0, 3500), Value = 720 });

            BeginOpacityAnimationParamList.Add(new BaseKeyFrame() { _KeyTime = new TimeSpan(0, 0, 0, 0, 0), Value = 0 });
            BeginOpacityAnimationParamList.Add(new BaseKeyFrame() { _KeyTime = new TimeSpan(0, 0, 0, 0, 1), Value = 1 });

            EndOpacityAnimationParamList.Add(new BaseKeyFrame() { _KeyTime = new TimeSpan(0, 0, 0, 0, 3499), Value = 1 });
            EndOpacityAnimationParamList.Add(new BaseKeyFrame() { _KeyTime = new TimeSpan(0, 0, 0, 0, 3500), Value = 0 });
            EndOpacityAnimationParamList.Add(new BaseKeyFrame() { _KeyTime = new TimeSpan(0, 0, 0, 0, 4500), Value = 0 });
        }

        /// <summary>
        /// 动画属性
        /// </summary>
        public static readonly string AnglePropertyPath = "(UIElement.RenderTransform).(RotateTransform.Angle)";
        public static readonly string OpacityPropertyPath = "(UIElement.Opacity)";
        private IList<BaseKeyFrame> AngleAnimationParamList;
        private IList<BaseKeyFrame> BeginOpacityAnimationParamList;
        private IList<BaseKeyFrame> EndOpacityAnimationParamList;
        private IList<FrameworkElement> ControlList;
        private Storyboard _Storyboard;
        private bool IsPlaying;

        /// <summary>
        /// 间隔时间
        /// </summary>
        public static readonly TimeSpan IntervalTimeSpan = new TimeSpan(0, 0, 0, 0, 200);

        public static readonly DependencyProperty WaitTextProperty = DependencyProperty.Register("WaitText", typeof(string), typeof(RoundWaitProgress), new PropertyMetadata("正在加载数据"));

        public string WaitText
        {
            get { return GetValue(WaitTextProperty).ToString(); }
            set { SetValue(WaitTextProperty, value); }
        }

        public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Brush), typeof(RoundWaitProgress), new PropertyMetadata(new SolidColorBrush(Colors.Black)));

        public Brush Color
        {
            get { return GetValue(ColorProperty) as Brush; }
            set { SetValue(ColorProperty, value); }
        }

        public static readonly DependencyProperty AnimationSizeProperty = DependencyProperty.Register("AnimationSize", typeof(double), typeof(RoundWaitProgress), new PropertyMetadata(80.0));

        public double AnimationSize
        {
            get { return Convert.ToDouble(GetValue(AnimationSizeProperty)); }
            set { SetValue(AnimationSizeProperty, value); }
        }

        public static readonly DependencyProperty PlayProperty = DependencyProperty.Register("Play", typeof(bool), typeof(RoundWaitProgress), new PropertyMetadata(false, (s, e) =>
        {
            var waitControl = s as RoundWaitProgress;
            if (waitControl.Play)
                waitControl.Start();
            else
                waitControl.Stop();
        }));

        public bool Play
        {
            get { return (bool)GetValue(PlayProperty); }
            set { SetValue(PlayProperty, value); }
        }

        private void Start()
        {
            this.IsPlaying = true;
            this.Visibility = Visibility.Visible;
            this._Storyboard = new Storyboard();
            foreach (var frameElement in ControlList)
            {
                (frameElement.RenderTransform as RotateTransform).Angle = 0;
                DoubleAnimationUsingKeyFrames daukf = new DoubleAnimationUsingKeyFrames();
                foreach (var item in AngleAnimationParamList)
                {
                    daukf.KeyFrames.Add(new EasingDoubleKeyFrame(Convert.ToDouble(item.Value), item._KeyTime));
                }
                Storyboard.SetTargetProperty(daukf, new PropertyPath(AnglePropertyPath));
                Storyboard.SetTarget(daukf, frameElement);
                this._Storyboard.Children.Add(daukf);

                DoubleAnimationUsingKeyFrames daukf1 = new DoubleAnimationUsingKeyFrames();
                foreach (var item in BeginOpacityAnimationParamList)
                {
                    daukf1.KeyFrames.Add(new EasingDoubleKeyFrame(Convert.ToDouble(item.Value), item._KeyTime));
                }
                foreach (var item in EndOpacityAnimationParamList)
                {
                    daukf1.KeyFrames.Add(new EasingDoubleKeyFrame(Convert.ToDouble(item.Value), item._KeyTime));
                }
                Storyboard.SetTargetProperty(daukf1, new PropertyPath(OpacityPropertyPath));
                Storyboard.SetTarget(daukf1, frameElement);
                this._Storyboard.Children.Add(daukf1);

                for (var i = 0; i < AngleAnimationParamList.Count; i++)
                {
                    var item = AngleAnimationParamList[i];
                    item._KeyTime = item._KeyTime.Add(IntervalTimeSpan);
                }

                foreach (var item in BeginOpacityAnimationParamList)
                {
                    item._KeyTime = item._KeyTime.Add(IntervalTimeSpan);
                }

                foreach (var item in EndOpacityAnimationParamList)
                {
                    item._KeyTime = item._KeyTime.Add(IntervalTimeSpan);
                }

                this._Storyboard.RepeatBehavior = RepeatBehavior.Forever;
            }
            this._Storyboard.Begin();
        }

        private void Stop()
        {
            this._Storyboard.Stop();
            this._Storyboard.Children.Clear();
            this._Storyboard = null;
            this.IsPlaying = false;
            InitAnimationData();
            this.Visibility = Visibility.Collapsed;
        }
    }
}