namespace FeedbackGeneratorQQ.Utils
{
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FeedbackGeneratorQQ.Models;
public static class AttributesHelper
{
/// <summary>
/// Default attributes root folder
/// </summary>
private const string _attributesFolder = "AttributeTemplates";
/// <summary>
/// Default template name
/// </summary>
private const string _templateName = "Template.xml";
/// <summary>
/// Default template root node
/// </summary>
public const string TemplateRootNode = "Template";
/// <summary>
/// %sheHe% and %heShe% replacer
/// </summary>
private static string _sheHeReplacer;
/// <summary>
/// %SheHe% and %HeShe% replacer
/// </summary>
private static string _sheHeUpperReplacer;
/// <summary>
/// %himHer% and %herHim% replacer
/// </summary>
private static string _himHerReplacer;
/// <summary>
/// %himselfHerself% and %herselfHimself% replacer
/// </summary>
private static string _himselfHerselfReplacer;
/// <summary>
/// %hisHer% and %herHis% replacer
/// </summary>
private static string _hisHerReplacer;
/// <summary>
/// %HisHer% and %HerHis% replacer
/// </summary>
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);
}
/// <summary>
/// Gets attributes and their content
/// </summary>
/// <returns> Returns list of attributes (Task) </returns>
public static Task<IEnumerable<AttributeModel>> GetAttributesAsync()
{
return Task.Factory.StartNew(() =>
{
return Directory.EnumerateDirectories(GetAttributesFolderPath(), "*", SearchOption.TopDirectoryOnly)
.Select(path => new AttributeModel(path))
.Where(attributeModel => attributeModel.Types.Any());
});
}
/// <summary>
/// Configures replacers based on selected gender
/// </summary>
/// <param name="gender"> The gender </param>
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";
}
/// <summary>
/// Replaces all known replacers into the given message
/// </summary>
/// <param name="inputMessage"> The input message </param>
/// <param name="firstName"> The first name </param>
/// <param name="lastName"> The last name </param>
/// <returns> Returns updated message </returns>
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);
}
}
}