using System.Linq.Expressions; using TelegramBotBase.Base; using TelegramBotBase.Form; namespace TelegramBotBase.Experiments.ActionManager.Navigation { public class StartWithNavigation : IExternalAction { public Type FormType { get; } public string Value { get; set; } public Action SetProperty { get; set; } public StartWithNavigation(Type formType, string value, Action setProperty) { FormType = formType; Value = value; SetProperty = setProperty; } public bool DoesFit(string raw_data) => raw_data.StartsWith(Value); public async Task DoAction(UpdateResult ur, MessageResult mr) { await mr.ConfirmAction(); var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase; if (mr.RawData != null) { SetProperty(new_form, mr.RawData); } await ur.Device.ActiveForm.NavigateTo(new_form); } } public class StartWithNavigation : IExternalAction where TForm : FormBase { public string Value { get; set; } public Action SetProperty { get; set; } public StartWithNavigation(string value, Action setProperty) { Value = value; SetProperty = setProperty; } public bool DoesFit(string raw_data) => raw_data.StartsWith(Value); public async Task DoAction(UpdateResult ur, MessageResult mr) { await mr.ConfirmAction(); var type = typeof(TForm); TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm; if (mr.RawData != null) { SetProperty(new_form, mr.RawData); } await ur.Device.ActiveForm.NavigateTo(new_form); } } public static class StartWithNavigation_Extensions { public static void AddStartsWithNavigation(this ExternalActionManager manager, string method, Expression> propertySelector) where TForm : FormBase { if (!typeof(FormBase).IsAssignableFrom(typeof(TForm))) { throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type"); } var newValue = Expression.Parameter(propertySelector.Body.Type); var assign = Expression.Lambda>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue); var setter = assign.Compile(true); manager.Add(new StartWithNavigation(method, setter)); } public static void AddStartsWithNavigation(this ExternalActionManager manager, string value, Action setProperty) where TForm : FormBase { if (!typeof(FormBase).IsAssignableFrom(typeof(TForm))) { throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type"); } manager.Add(new StartWithNavigation(value, setProperty)); } public static void AddStartsWithNavigation(this ExternalActionManager manager, Type formType, string value, Expression> propertySelector) { if (!typeof(FormBase).IsAssignableFrom(formType)) { throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type"); } var newValue = Expression.Parameter(propertySelector.Body.Type); var assign = Expression.Lambda>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue); var setter = assign.Compile(true); manager.Add(new StartWithNavigation(formType, value, setter)); } public static void AddStartsWithNavigation(this ExternalActionManager manager, Type formType, string value, Action setProperty) { if (!typeof(FormBase).IsAssignableFrom(formType)) { throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type"); } manager.Add(new StartWithNavigation(formType, value, setProperty)); } } }