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

namespace InABox.WPF
{
    /// <summary>
    ///     Interaction logic for ProgressForm.xaml
    /// </summary>
    public partial class ProgressForm : ThemableWindow
    {
        private ImageSource? _image = null;

        private DoubleAnimation _fadein = new DoubleAnimation(1d, new Duration(TimeSpan.FromSeconds(3)));
        private DoubleAnimation _fadeout = new DoubleAnimation(0.2d, new Duration(TimeSpan.FromSeconds(3)));

        public ProgressForm()
        {
            InitializeComponent();
            Topmost = true;
            Loaded += (sender, args) =>
            {
                _fadeout.Completed += (o, e) => Splash.BeginAnimation(Image.OpacityProperty, _fadein);
                _fadein.Completed += (o, e) => Splash.BeginAnimation(Image.OpacityProperty, _fadeout);
                Splash.BeginAnimation(Image.OpacityProperty, _fadeout);
            };
        }
        
        public ImageSource? DisplayImage
        {
            get => Splash.Source;
            set => Splash.Source = value;
        }
        
        public void UpdateWindow(string message)
        {
            if (Progress.Dispatcher.CheckAccess())
                Progress.Content = message;
            else
                Progress.Dispatcher.Invoke(() => { Progress.Content = message; });
        }

        public void CloseWindow()
        {
            if (Progress.Dispatcher.CheckAccess())
                Close();
            else
                Progress.Dispatcher.Invoke(() => { Close(); });
        }

        public string GetMessage()
        {
            return Progress.Content as string;
        }

        private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if(e.ChangedButton == System.Windows.Input.MouseButton.Left)
            {
                DragMove();
            }
        }
    }
}