namespace FeedbackGeneratorQQ.Utils { using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using FeedbackGeneratorQQ.Models; public static class AttributesHelper { /// /// Default attributes root folder /// private const string _attributesFolder = "AttributeTemplates"; /// /// Default template name /// private const string _templateName = "Template.xml"; /// /// Default template root node /// public const string TemplateRootNode = "Template"; /// /// %sheHe% and %heShe% replacer /// private static string _sheHeReplacer; /// /// %SheHe% and %HeShe% replacer /// private static string _sheHeUpperReplacer; /// /// %himHer% and %herHim% replacer /// private static string _himHerReplacer; /// /// %himselfHerself% and %herselfHimself% replacer /// private static string _himselfHerselfReplacer; /// /// %hisHer% and %herHis% replacer /// private static string _hisHerReplacer; /// /// %HisHer% and %HerHis% replacer /// private static string _hisHerUpperReplacer; private static string GetAppPath() { return System.AppDomain.CurrentDomain.BaseDirectory; } private static string GetAttributesFolderPath() { return Path.Combine(GetAppPath(), _attributesFolder); } public static string GetAttributePath(string attributeFolder) { return Path.Combine(attributeFolder, _templateName); } /// /// Gets attributes and their content /// /// Returns list of attributes (Task) public static Task> GetAttributesAsync() { return Task.Factory.StartNew(() => { return Directory.EnumerateDirectories(GetAttributesFolderPath(), "*", SearchOption.TopDirectoryOnly) .Select(path => new AttributeModel(path)) .Where(attributeModel => attributeModel.Types.Any()); }); } /// /// Configures replacers based on selected gender /// /// The gender public static void ConfigureReplacers(string gender) { bool isMale = gender.Equals("Male"); _sheHeReplacer = isMale ? "he" : "she"; _sheHeUpperReplacer = isMale ? "He" : "She"; _himHerReplacer = isMale ? "him" : "her"; _himselfHerselfReplacer = isMale ? "himself" : "herself"; _hisHerReplacer = isMale ? "his" : "her"; _hisHerUpperReplacer = isMale ? "His" : "Her"; } /// /// Replaces all known replacers into the given message /// /// The input message /// The first name /// The last name /// Returns updated message public static string ApplyReplacers( string inputMessage, string firstName, string lastName) { return inputMessage .Replace("%UserName%", $"{firstName} {lastName}") .Replace("%LastName%", lastName) .Replace("%FirstName%", firstName) .Replace("%SheHe%", _sheHeUpperReplacer) .Replace("%HeShe%", _sheHeUpperReplacer) .Replace("%sheHe%", _sheHeReplacer) .Replace("%heShe%", _sheHeReplacer) .Replace("%himHer%", _himHerReplacer) .Replace("%herHim%", _himHerReplacer) .Replace("%himselfHerself%", _himselfHerselfReplacer) .Replace("%herselfHimself%", _himselfHerselfReplacer) .Replace("%HisHer%", _hisHerUpperReplacer) .Replace("%HerHis%", _hisHerUpperReplacer) .Replace("%hisHer%", _hisHerReplacer) .Replace("%herHis%", _hisHerReplacer); } } }