using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SlnMesnac.WPF.ViewModel.Base
{
    public class BaseViewModel : ObservableObject
    {

        public string EscapeUnicode(string input)
        {
            return Regex.Replace(input, "&#x([0-9a-fA-F]+);", match =>
            {
                string hexValue = match.Groups[1].Value;
                return $"\\u{hexValue}";
            });
        }

        /// <summary>
        /// 获取当前类中的方法
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, IRelayCommand> GetRelayCommandsAsDictionary()
        {
            var commandDict = new Dictionary<string, IRelayCommand>();
            var properties = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (var property in properties)
            {
                if (property.PropertyType == typeof(IRelayCommand))
                {
                    var command = property.GetValue(this) as IRelayCommand;
                    if (command != null)
                    {
                        commandDict[property.Name] = command;
                    }
                }
            }
            var fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
            foreach (var field in fields)
            {
                if (field.FieldType == typeof(IRelayCommand))
                {
                    var command = field.GetValue(this) as IRelayCommand;
                    if (command != null)
                    {
                        commandDict[field.Name] = command;
                    }
                }
            }
            return commandDict;
        }
    }
}
