using MainShell.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace MainShell.Common
{
    public static class CommonUti
    {
        public static void RunOnUi(Action action)
        {
            if (action == null)
            {
                return;
            }

            try
            {
                if (Application.Current == null || Application.Current.Dispatcher.CheckAccess())
                {
                    // 同步在当前线程执行
                    action();
                }
                else
                {

                    // 在 UI 线程同步执行
                    Application.Current.Dispatcher.Invoke(action);

                }
            }
            catch (Exception ex)
            {
                MaxwellControl.Controls.MessageBox.Show($"方法执行异常：{ex.ToString()}", icon: MessageBoxImage.Error);
            }
        }

        public static Brush BrushFromString(string s)
        {
            if (string.IsNullOrWhiteSpace(s))
                return Brushes.Transparent;

            try
            {
                var brush = (Brush)new BrushConverter().ConvertFromString(s);
                return brush ?? Brushes.Transparent;
            }
            catch
            {
                // 如果解析失败，返回默认颜色（可根据需要改为 Brushes.Black 或抛异常）
                return Brushes.Transparent;
            }
        }
    }
}
