using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace Earthblend.Tts.Common.Core.Statics
{
    public static class UIServices
    {
        public static void DoEvents()
        {
            if (Application.Current == null) return;
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
        }

        public static void DoEventsRender()
        {
            if (Application.Current == null) return;
            Application.Current.Dispatcher.Invoke(delegate () { }, DispatcherPriority.Render);
        }


        //private static ImageDataItem _showControlsImageGreen = new ImageDataItem() { ImagePath = @"Images\WorkflowSelector\", ImageName = "Start.png" };
        //private static ImageDataItem _showControlsImageRed = new ImageDataItem() { ImagePath = @"Images\WorkflowSelector\", ImageName = "Stop.png" };

        //private static ImageDataItem ImageGreen { get; set; } = new ImageDataItem() { ImagePath = @"Images\WorkflowSelector\", ImageName = "Start.png" };
        //private static ImageDataItem ImageRed { get; set; } = new ImageDataItem() { ImagePath = @"Images\WorkflowSelector\", ImageName = "Stop.png" };

        //public static String ImageGreenFullImageName { get { return ImageGreen.FullImageName; } }
        //public static String ImageRedFullImageName { get { return ImageRed.FullImageName; } }


        private static bool IsBusy;

        public static void Busy()
        {
            SetBusyState(true);
        }
        public static void SetBusyState()
        {
            SetBusyState(true);
        }

        private static void SetBusyState(bool busy)
        {
            if (busy != IsBusy)
            {
                IsBusy = busy;
                Application.Current.Dispatcher.Invoke(() => { Mouse.OverrideCursor = busy ? Cursors.Wait : null; });
                if (IsBusy) new DispatcherTimer(TimeSpan.FromSeconds(0), DispatcherPriority.ApplicationIdle, dispatcherTimer_Tick, System.Windows.Application.Current.Dispatcher);
            }
        }

        private static void dispatcherTimer_Tick(Object sender, System.EventArgs e)
        {
            var dispatcherTimer = sender as DispatcherTimer;
            if (dispatcherTimer != null)
            {
                SetBusyState(false);
                dispatcherTimer.Stop();
            }
        }
    }
}
