using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Crysome.Server.Controls;

[TemplatePart(Name = "PART_OldContent", Type = typeof(ContentPresenter))]
[TemplatePart(Name = "PART_NewContent", Type = typeof(ContentPresenter))]
public class PageTransitionControl : ContentControl
{
	private ContentPresenter _oldContent;

	private ContentPresenter _newContent;

	static PageTransitionControl()
	{
		FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(PageTransitionControl), (PropertyMetadata)(object)new FrameworkPropertyMetadata((object)typeof(PageTransitionControl)));
	}

	public override void OnApplyTemplate()
	{
		base.OnApplyTemplate();
		_oldContent = GetTemplateChild("PART_OldContent") as ContentPresenter;
		_newContent = GetTemplateChild("PART_NewContent") as ContentPresenter;
		if (_newContent != null)
		{
			_newContent.Content = base.Content;
		}
	}

	protected override void OnContentChanged(object oldContent, object newContent)
	{
		base.OnContentChanged(oldContent, newContent);
		if (_oldContent != null && _newContent != null)
		{
			_oldContent.Content = oldContent;
			_newContent.Content = newContent;
			_oldContent.RenderTransform = new TranslateTransform();
			_newContent.RenderTransform = new TranslateTransform();
			_oldContent.Opacity = 1.0;
			_newContent.Opacity = 0.0;
			AnimateTransition();
		}
	}

	private void AnimateTransition()
	{
		Storyboard storyboard = new Storyboard();
		TimeSpan timeSpan = TimeSpan.FromMilliseconds(300L, 0L);
		CubicEase easingFunction = new CubicEase
		{
			EasingMode = EasingMode.EaseOut
		};
		DoubleAnimation doubleAnimation = new DoubleAnimation(0.0, -50.0, timeSpan)
		{
			EasingFunction = easingFunction
		};
		DoubleAnimation doubleAnimation2 = new DoubleAnimation(1.0, 0.0, timeSpan);
		Storyboard.SetTarget((DependencyObject)(object)doubleAnimation, (DependencyObject)(object)_oldContent);
		Storyboard.SetTargetProperty((DependencyObject)(object)doubleAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
		Storyboard.SetTarget((DependencyObject)(object)doubleAnimation2, (DependencyObject)(object)_oldContent);
		Storyboard.SetTargetProperty((DependencyObject)(object)doubleAnimation2, new PropertyPath("Opacity"));
		DoubleAnimation doubleAnimation3 = new DoubleAnimation(50.0, 0.0, timeSpan)
		{
			EasingFunction = easingFunction
		};
		DoubleAnimation doubleAnimation4 = new DoubleAnimation(0.0, 1.0, timeSpan);
		Storyboard.SetTarget((DependencyObject)(object)doubleAnimation3, (DependencyObject)(object)_newContent);
		Storyboard.SetTargetProperty((DependencyObject)(object)doubleAnimation3, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
		Storyboard.SetTarget((DependencyObject)(object)doubleAnimation4, (DependencyObject)(object)_newContent);
		Storyboard.SetTargetProperty((DependencyObject)(object)doubleAnimation4, new PropertyPath("Opacity"));
		storyboard.Children.Add(doubleAnimation);
		storyboard.Children.Add(doubleAnimation2);
		storyboard.Children.Add(doubleAnimation3);
		storyboard.Children.Add(doubleAnimation4);
		storyboard.Begin();
	}
}
