using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace RobotClientWpf.Utilities
{
    public static class UIAccessHelpers
    {
        public static void SetButtonState(ButtonBase button, bool enabled)
        {
            if (!Application.Current.Dispatcher.CheckAccess())
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    SetButtonState(button, enabled);
                }));
            }
            else
            {
                button.IsEnabled = enabled;
            }
        }
        public static void SetButtonContent(ButtonBase button, string content)
        {
            if (!Application.Current.Dispatcher.CheckAccess())
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    SetButtonContent(button, content);
                }));
            }
            else
            {
                button.Content = content;
            }
        }

        public static string GetTextboxText(TextBox textBox)
        {
            string text = String.Empty;
            if (!Application.Current.Dispatcher.CheckAccess())
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    text = GetTextboxText(textBox);
                })).Wait();
            }
            else
            {
                text = textBox.Text;
            }
            return text;
        }

        public static void SetTextboxState(TextBoxBase textBox, bool enabled)
        {
            if (!Application.Current.Dispatcher.CheckAccess())
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    SetTextboxState(textBox, enabled);
                }));
            }
            else
            {
                textBox.IsEnabled = enabled;
            }
        }

        public static void SetTextboxText(TextBox textBox, string text)
        {
            if (!Application.Current.Dispatcher.CheckAccess())
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    SetTextboxText(textBox, text);
                }));
            }
            else
            {
                textBox.Text = text;
            }
        }

        public static void SetTextblockTextAndForegroundColor(TextBlock textBlock, string text, Brush foregroundColor)
        {
            if (!Application.Current.Dispatcher.CheckAccess())
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    SetTextblockTextAndForegroundColor(textBlock, text, foregroundColor);
                }));
            }
            else
            {
                textBlock.Text = text;
                textBlock.Foreground = foregroundColor;
            }
        }
    }
}
