using RuriLib.Models;
using RuriLib.ViewModels;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Media;
namespace RuriLib.Models
{
///
/// Represents a set of keys that can be checked in a OR/AND fashion and modifies the status of the BotData if successful.
///
public class KeyChain : ViewModelBase
{
/// The returned status upon a successful check of the keys. If no KeyChain was valid, the original status won't be changed.
public enum KeychainType
{
/// Sets a SUCCESS status in the bot.
Success,
/// Sets a FAIL status in the bot.
Failure,
/// Sets a BAN status in the bot.
Ban,
/// Sets a RETRY status in the bot.
Retry,
/// Sets a CUSTOM status in the bot.
Custom
}
/// The mode in which the keys should be checked.
public enum KeychainMode
{
/// Trigger the KeyChain if ANY Key is valid.
OR,
/// Trigger the KeyChain if ALL the Keys are valid.
AND
}
private KeychainType type = KeychainType.Success;
/// The type of the KeyChain.
public KeychainType Type { get { return type; } set { type = value; OnPropertyChanged(); } }
private KeychainMode mode = KeychainMode.OR;
/// The mode of the KeyChain.
public KeychainMode Mode { get { return mode; } set { mode = value; OnPropertyChanged(); } }
private string customType = "CUSTOM";
/// The type of the KeyChain in case Custom is selected.
public string CustomType { get { return customType; } set { customType = value; OnPropertyChanged(); } }
/// The collection of Keys in the KeyChain.
public ObservableCollection Keys { get; set; } = new ObservableCollection();
///
/// Checks all the Keys in the KeyChain.
///
/// The BotData used for variable replacement
/// Whether the KeyChain was triggered or not
public bool CheckKeys(BotData data)
{
switch (Mode)
{
case KeychainMode.OR:
foreach (var key in Keys)
{
if (key.CheckKey(data))
{
data.Log(new LogEntry(string.Format("Found 'OR' Key {0} {1} {2}",
BlockBase.TruncatePretty(BlockBase.ReplaceValues(key.LeftTerm, data), 20),
key.Condition.ToString(),
BlockBase.ReplaceValues(key.RightTerm, data)),
Colors.White));
return true;
}
}
return false;
case KeychainMode.AND:
foreach (var key in Keys)
{
if (!key.CheckKey(data))
return false;
else
{
data.Log(new LogEntry(string.Format("Found 'AND' Key {0} {1} {2}",
BlockBase.TruncatePretty(BlockBase.ReplaceValues(key.LeftTerm, data), 20),
key.Condition.ToString(),
BlockBase.ReplaceValues(key.RightTerm, data)),
Colors.White));
}
}
return true;
}
return false;
}
}
}