using System;
using System.Collections.Generic;
using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CAPI.Custom;
namespace CEP.ConsoleTests
{
///
/// Resolves Surface names using the Entities type name
///
///
/// var nameResolver = new SurfaceNameResolver(arrayOfAllExistingSurfaceNames);
/// var typeName = typeof(Quote).Name;
/// var surfaceName = nameResolver.Resolve(typeName);
///
public class SurfaceNameResolver
{
public List SurfaceNames { get; set; } = new List();
public SurfaceNameResolver(IEnumerable surfaceNames)
{
SurfaceNames.AddRange(surfaceNames);
}
public string Resolve(string typeName)
{
if (typeName.IsEmpty()) return string.Empty;
// if the type's name comes from the old classes
// - oPartsAlternates
// - oPartsMaster
// then just remove the prefix 'o'
if (typeName[0] == 'o') return typeName.Substring(1);
// First try matching the pluralized typeName which is most likely the case
var pluralizedName = PluralizationService.CreateService(CultureInfo.CurrentCulture)
.Pluralize(typeName);
var found = SurfaceNames
.FirstOrDefault(p => pluralizedName.Equals(p, StringComparison.OrdinalIgnoreCase))
?? string.Empty;
if (found.IsNotEmpty()) return found;
// Next try matching the typeName
found = SurfaceNames.FirstOrDefault(p => typeName.Equals(p, StringComparison.OrdinalIgnoreCase));
if (found.IsNotEmpty()) return found;
return string.Empty;
}
}
}