using Avalonia.Controls;
using Avalonia.Animation;
using VeloeMinecraftLauncher.ViewModels;
using Avalonia.Media;
using Avalonia.Styling;
using System;

namespace VeloeMinecraftLauncher.Views
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void MainWindow_Closing(object sender, WindowClosingEventArgs e)
        {
            if (DataContext is MainWindowViewModel model)
            {
                model.OnClosing(sender, e);
            }
        }

        private void OnTabSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((e.AddedItems[0] as TabItem)?.Content is Control selectedContent)
            {
                // Define the slide-and-fade animation
                var slideAnimation = new Animation
                {
                    Duration = TimeSpan.FromSeconds(0.2),
                    Children =
                {
                    new KeyFrame
                    {
                        Cue = new Cue(0),
                        Setters =
                        {
                            new Setter(TranslateTransform.XProperty, -(e.AddedItems[0] as TabItem).Bounds.Width),
                            new Setter(OpacityProperty, 0.0)
                        }
                    },
                    new KeyFrame
                    {
                        Cue = new Cue(1),
                        Setters =
                        {
                            new Setter(TranslateTransform.XProperty, 0),
                            new Setter(OpacityProperty, 1.0)
                        }
                    }
                }
                };

                // Apply the animation
                selectedContent.RenderTransform = new TranslateTransform();
                slideAnimation.RunAsync(selectedContent, default);
            }
        }
    }
}
