using System;
using System.Collections.Generic;
using System.Linq;
namespace RuriLib.Models
{
///
/// Represents a variable.
///
public class CVar
{
///
/// The type of variable.
///
public enum VarType
{
/// Holds a single string as value.
Single,
/// Holds a list of strings as value.
List,
/// Holds a dictionary of strings as value.
Dictionary
}
/// The variable name.
public string Name { get; set; }
/// The dynamic variable value.
public dynamic Value { get; set; }
/// Whether the variable is used for the final capture.
public bool IsCapture { get; set; }
/// The variable type (it determines which type of value is expected).
public VarType Type { get; set; }
/// Whether the variable is hidden and shouldn't be displayed to the user.
public bool Hidden { get; set; }
// Constructors
/// Needed for NoSQL deserialization.
public CVar() { }
///
/// Creates a variable of type Single.
///
/// The variable name
/// The variable value as a single string
/// Whether the variable is marked as Capture
/// Whether the variable is hidden
public CVar(string name, string value, bool isCapture = false, bool hidden = false)
{
Name = name;
Value = value;
IsCapture = isCapture;
Type = VarType.Single;
Hidden = hidden;
}
///
/// Creates a variable of type List.
///
/// The variable name
/// The variable value as a list of strings
/// Whether the variable is marked as Capture
/// Whether the variable is hidden
public CVar(string name, List value, bool isCapture = false, bool hidden = false)
{
Name = name;
Value = value;
IsCapture = isCapture;
Type = VarType.List;
Hidden = hidden;
}
///
/// Creates a variable of type Dictionary.
///
/// The variable name
/// The variable value as a dictionary of strings
/// Whether the variable is marked as Capture
/// Whether the variable is hidden
public CVar(string name, Dictionary value, bool isCapture = false, bool hidden = false)
{
Name = name;
Value = value;
IsCapture = isCapture;
Type = VarType.Dictionary;
Hidden = hidden;
}
///
/// Creates a variable of a given type.
///
/// The variable name
/// The variable type
/// The dynamic variable value
/// Whether the variable is marked as Capture
/// Whether the variable is hidden
public CVar(string name, VarType type, dynamic value, bool isCapture = false, bool hidden = false)
{
Name = name;
Value = value;
IsCapture = isCapture;
Type = type;
Hidden = hidden;
}
///
/// Outputs a string from a variable.
///
/// The formatted value of the value as a single string
public override string ToString()
{
switch (Type)
{
case VarType.Single:
return Value;
case VarType.List:
if (Value.GetType() == typeof(List)) return "[" + string.Join(", ", (List)Value) + "]"; // Coming internally
else if (Value.GetType() == typeof(object[])) return "[" + string.Join(", ", Value) + "]"; // Coming from the DB
else return "";
case VarType.Dictionary:
return "{" + string.Join(", ", ((Dictionary)Value).Select(d => "(" + d.Key + ", " + d.Value + ")")) + "}";
default:
return base.ToString();
}
}
///
/// Gets an item from a List variable.
///
/// The index of the item
/// The item of the List variable's value
public string GetListItem(int index)
{
if (Type != VarType.List) return null;
var list = Value as List;
if (index > list.Count - 1 || index < 0) return null;
return list[index];
}
///
/// Gets an item from a Dictionary Key.
///
/// The key of the dictionary entry
/// The value of the Dictionary variable's entry
public string GetDictValue(string key)
{
var dict = Value as Dictionary;
if (dict.ContainsKey(key)) return dict.First(d => d.Key == key).Value;
else throw new Exception("Key not in dictionary");
}
///
/// Gets an item from a Dictionary Value.
///
/// The value of the dictionary entry
/// The key of the Dictionary variable's entry
public string GetDictKey(string value)
{
var dict = Value as Dictionary;
if (dict.ContainsValue(value)) return dict.First(d => d.Value == value).Key;
else throw new Exception("Value not in dictionary");
}
}
}