using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace MacroReplacements.Models
public class PageViewModel
public string PageTitle { get; set; }
public string HeadingColor { get; set; }
public string MainContent { get; set; }
public class MerchantPageViewModel : PageViewModel
public MerchantDetailModel MerchantDetail { get; set; }
public string[] RelatedMerchantContent { get; set; }
public class MerchantModel
public string MerchantName { get; set; }
public string MerchantImageSrc { get; set; }
public string MerchantDescription { get; set; }
public class MerchantDetailModel : MerchantModel
public int CashBackPercent { get; set; }
public IEnumerable<MerchantModel> RelatedMerchants { get; set; }
public class PageSettings
public string NavigationTabText { get; set; }
public string NavigationTabBackgroundColor { get; set; }
public string NavigationTabIconHtml { get; set; }
public class Macro<TParams>
public string Key { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Func<TParams, string> Substitution { get; set; }
public class ReplaceMacrosResult
public ReplaceMacrosResult()
ResponseMessage = new List<string>();
Errors = new List<string>();
public static ReplaceMacrosResult CreateSuccessResult(string result, List<string> responseMessage = null, List<string> errors = null)
return new ReplaceMacrosResult()
ResponseMessage = responseMessage ?? new List<string>(),
Errors = errors ?? new List<string>()
public string Result { get; set; }
public bool Success { get; set; }
public List<string> ResponseMessage { get; set; }
public List<string> Errors { get; set; }
public interface IMacroCollectionParser<TParams>
bool DoesTextContainMacro(string text, string macroKey);
bool DoesTextContainAnyMacros(string text);
IReadOnlyCollection<Macro<TParams>> GetMacros();
ReplaceMacrosResult GetReplaceMacrosResult(string str, TParams src);
public abstract class BaseMacroCollectionParser<TParams> : IMacroCollectionParser<TParams>
public abstract IReadOnlyCollection<Macro<TParams>> GetMacros();
public virtual ReplaceMacrosResult GetReplaceMacrosResult(string str, TParams src)
var result = ReplaceMacrosResult.CreateSuccessResult(str);
if (!DoesTextContainAnyMacros(str))
result.ResponseMessage.Add("Content had no macros to replace");
var macros = GetMacros();
result.ResponseMessage.Add("Macro could not be parsed because it was null");
foreach (var macro in macros)
result.Result = ReplaceMacrosInString(result.Result, src, macro);
result.Errors.Add("There was an error handling macro with key: {" + macro.Key + "}");
public string ReplaceMacrosInString(string str, TParams src, Macro<TParams> macro)
if (macro == null) return str;
var replacement = macro.Substitution(src);
var replacedInput = Regex.Replace(input, "\\{" + pattern + "+\\}", replacement ?? string.Empty);
public bool DoesTextContainMacro(string text, string macroKey)
if (string.IsNullOrEmpty(text))
var macros = GetMacros();
var filteredMacros = macros
.Where(m => m.Key == macroKey || macroKey == null)
.Where(m => !string.IsNullOrEmpty(m.Key))
return filteredMacros != null
&& filteredMacros.Any(m => text.IndexOf(m, StringComparison.CurrentCultureIgnoreCase) >= 0);
public bool DoesTextContainAnyMacros(string text)
return DoesTextContainMacro(text, null);
public class PageMacrosParser : BaseMacroCollectionParser<PageSettings>
public override IReadOnlyCollection<Macro<PageSettings>> GetMacros()
return GetPageMacros().ToList();
private IEnumerable<Macro<PageSettings>> GetPageMacros()
yield return new Macro<PageSettings>
Description = "The title of the page.",
Substitution = m => m.NavigationTabText
yield return new Macro<PageSettings>
Description = "The icon for the page.",
Substitution = m => m.NavigationTabIconHtml
yield return new Macro<PageSettings>
Name = "Page Link Color",
Description = "The color used for the page navigation link.",
Substitution = m => m.NavigationTabBackgroundColor
public class MerchantMacrosParser : BaseMacroCollectionParser<MerchantModel>
public override IReadOnlyCollection<Macro<MerchantModel>> GetMacros()
return GetMerchantMacros().ToList();
private IEnumerable<Macro<MerchantModel>> GetMerchantMacros()
yield return new Macro<MerchantModel>
Description = "The name of the Merchant.",
Substitution = m => m.MerchantName
yield return new Macro<MerchantModel>
Key = "merchantDescription",
Name = "Merchant Description",
Description = "The description for the Merchant.",
Substitution = m => m.MerchantDescription