using InABox.Wpf;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;

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

        private bool on = true;

        public ProgressForm()
        {
            InitializeComponent();
            Topmost = true;
            var timer = new DispatcherTimer(DispatcherPriority.Send)
            {
                Interval = new TimeSpan(0, 0, 0, 0, 500)
            };
            timer.Tick += (o, e) =>
            {
                on = !on;
                Progress.Background = new SolidColorBrush(on ? Colors.LightGreen : Colors.LightSalmon);
            };
            timer.IsEnabled = false;
        }

        public ImageSource DisplayImage
        {
            get => _image;
            set
            {
                Image.Width = value.Width;
                Image.Height = value.Height;
                Image.Background = new ImageBrush(value) { Stretch = Stretch.UniformToFill };
            }
        }

        //public void OpenWindow(String message)
        //{
        //    if (Progress.Dispatcher.CheckAccess())
        //        Update(message);
        //    else
        //        Progress.Dispatcher.BeginInvoke(new Action(() => { Update(message); }), DispatcherPriority.Send);
        //}

        //private delegate void UpdateDelegate(string message);
        //private void Update(String message)
        //{
        //    Progress.Content = message;
        //}

        public void UpdateWindow(string message)
        {
            if (Progress.Dispatcher.CheckAccess())
                Progress.Content = message;
            else
                Progress.Dispatcher.Invoke(() => { Progress.Content = message; });
            //Progress.Dispatcher.BeginInvoke(new Action(() => { Update(message);  }), DispatcherPriority.Send);
        }

        //private delegate void HideDelegate();

        public void CloseWindow()
        {
            if (Progress.Dispatcher.CheckAccess())
                Close();
            else
                Progress.Dispatcher.Invoke(() => { Close(); });
            //Progress.Dispatcher.BeginInvoke(new Action(() => { Close(); }), DispatcherPriority.Send);
        }

        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();
            }
        }
    }
}