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

namespace Noctalk_client.Splash_screen;

public partial class SplashWindow : Window
{
    private const int Minimum_splash_seconds = 13;
    private const int Route_animation_start_delay_seconds = 3;
    private RouteAnimationController? animationController;
    private SplashBootTextController? bootTextController;
    private readonly bool flagReviewAllPlaces;

    public SplashWindow(bool Flag_review_all_places = false)
    {
        flagReviewAllPlaces = Flag_review_all_places;
        InitializeComponent();
        Loaded += SplashWindow__loaded;
        Closed += (_, _) =>
        {
            animationController?.Animation__stop();
            bootTextController?.Boot_text__stop();
        };
    }

    public Task Wait_minimum_duration_async()
    {
        return Task.Delay(TimeSpan.FromSeconds(Minimum_splash_seconds));
    }

    public Task Wait_review_duration_async()
    {
        return Task.Delay(TimeSpan.FromSeconds(30));
    }

    public Task Fade_out_async()
    {
        TaskCompletionSource taskCompletionSource = new();
        DoubleAnimation fadeAnimation = new(Opacity, 0, TimeSpan.FromMilliseconds(520))
        {
            EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseInOut }
        };
        fadeAnimation.Completed += (_, _) => taskCompletionSource.SetResult();
        BeginAnimation(OpacityProperty, fadeAnimation);
        return taskCompletionSource.Task;
    }

    private async void SplashWindow__loaded(object sender, RoutedEventArgs e)
    {
        bootTextController = new SplashBootTextController(Boot_text_border, Boot_text);
        bootTextController.Boot_text__start();
        animationController = new RouteAnimationController(Map_view, Map_scale, Route_canvas, Map_translate, flagReviewAllPlaces);
        await Task.Delay(TimeSpan.FromSeconds(Route_animation_start_delay_seconds));
        if (!IsVisible)
        {
            return;
        }

        animationController.Animation__start();
    }
}
