using System;
using System.Collections.Generic;
using System.Linq;
namespace xAPI.Registry
{
///
/// Version: 2.1.1
///
public abstract class xAPI_Definition
{
public readonly string Context;
public readonly string Key;
public readonly Dictionary Names;
public readonly Dictionary Descriptions;
public xAPI_Definition(string context, string key, Dictionary names,
Dictionary descriptions)
{
Context = context;
Key = key;
Names = names;
Descriptions = descriptions;
}
///
/// Checks if this definition matches keyPath.
///
/// Can be expressed as [contextKey].(verbs|activity|extensions.(activity|context|result)).[definitionKey].
/// Is true if key path matches with this item.
public bool MatchesPath(string keyPath, char pathSeperator = '.')
{
var path = GetPath().Substring(1).Replace('/', pathSeperator);
return keyPath == path;
}
public string GetName(string language)
{
if (!Names.ContainsKey(language))
{
throw new ArgumentException($"There is no name for the language {language}.");
}
return Names[language];
}
public string GetDescription(string language)
{
if (!Descriptions.ContainsKey(language))
{
throw new ArgumentException($"There is no description for the language {language}.");
}
return Descriptions[language];
}
public KeyValuePair GetNameDescription(string language)
{
var namesContainLang = Names.ContainsKey(language);
var descsContainLang = Descriptions.ContainsKey(language);
if (!namesContainLang)
{
if (!descsContainLang)
{
throw new ArgumentException($"There is no name and no description for the language {language}.");
}
throw new ArgumentException($"There is no name for the language {language}.");
}
else if (!descsContainLang)
{
throw new ArgumentException($"There is no description for the language {language}.");
}
return new KeyValuePair(Names[language], Descriptions[language]);
}
public string[] GetLanguages() => Names.Keys.Union(Descriptions.Keys).ToArray();
public abstract string GetPath();
public string CreateValidId(string uri)
{
var path = GetPath();
// remove slash at end of uri
if (uri[uri.Length - 1] == '/')
{
uri = uri.Substring(0, uri.Length - 1);
}
// remove slash at start of path
if (path[0] == '/')
{
path = path.Substring(1);
}
return $"{uri}/{path}";
}
private static string DictToString(Dictionary dict) =>
dict == null ? string.Empty : string.Join(",", dict);
public override string ToString() =>
$"[{GetType().Name}: name=[{DictToString(Names)}], description=[{DictToString(Descriptions)}]]";
}
}