using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using Microsoft.CodeAnalysis.VisualBasic.Syntax; using PRS.Shared.Events; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace PRS.Shared.Grids.EventEditor; public interface IEventActionEditor { bool Edit(IEventAction action, IEventDataModelDefinition dataModelDefinition); } /// /// Indicates that this class is the editor for . /// /// /// It is expected that if is generic, then this type should have exactly the same /// generic arguments. /// public interface IEventActionEditor : IEventActionEditor where TAction : IEventAction { bool Edit(TAction action, IEventDataModelDefinition dataModelDefinition); bool IEventActionEditor.Edit(IEventAction action, IEventDataModelDefinition dataModelDefinition) => Edit((TAction)action, dataModelDefinition); } public static class EventActionEditors { private static Dictionary? _actionEditors; private static Dictionary GetActionEditors() { if(_actionEditors is null) { _actionEditors = new Dictionary(); foreach(var type in CoreUtils.TypeList(x => true)) { if (type.GetInterfaceDefinition(typeof(IEventActionEditor<>)) is Type editorInterface) { var actionType = editorInterface.GenericTypeArguments[0]; actionType = actionType.IsGenericType ? actionType.GetGenericTypeDefinition() : actionType; _actionEditors[actionType] = type; } } } return _actionEditors; } public static Type? GetActionEditorType(Type actionType) { var genericActionType = actionType.IsGenericType ? actionType.GetGenericTypeDefinition() : actionType; if(GetActionEditors().GetValueOrDefault(genericActionType) is Type editorType) { return editorType.IsGenericType ? editorType.MakeGenericType(actionType.GenericTypeArguments) : editorType; } else { return null; } } public static bool EditAction(IEventAction action, IEventDataModelDefinition dataModelDefinition) where TEvent : IEvent where TDataModel : IEventDataModel { var editorType = GetActionEditorType(action.GetType()); if (editorType is null) return true; var editor = (Activator.CreateInstance(editorType) as IEventActionEditor)!; return editor.Edit(action, dataModelDefinition); } }