From 6fafd3b48040f6af2bda538e1b7389b7843dbb40 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 17 Nov 2018 17:58:25 +0000
Subject: [PATCH] Add support for deleting reminders

---
 RhinoReminds/AIRecogniser.cs   |  9 +++++++++
 RhinoReminds/ClientListener.cs | 28 ++++++++++++++++++++++++++++
 RhinoReminds/ReminderList.cs   | 12 +++++++++---
 3 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/RhinoReminds/AIRecogniser.cs b/RhinoReminds/AIRecogniser.cs
index b28c4d6..519cdca 100644
--- a/RhinoReminds/AIRecogniser.cs
+++ b/RhinoReminds/AIRecogniser.cs
@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text.RegularExpressions;
 using Microsoft.Recognizers.Text;
 using Microsoft.Recognizers.Text.DateTime;
+using Microsoft.Recognizers.Text.Number;
 using SBRL.Geometry;
 
 namespace RhinoReminds
@@ -11,6 +12,14 @@ namespace RhinoReminds
 
 	public static class AIRecogniser
 	{
+		public static IEnumerable<int> RecogniseNumbers(string source)
+		{
+			List<ModelResult> aiResults = NumberRecognizer.RecognizeNumber(source, Culture.English);
+			foreach (ModelResult nextResult in aiResults) {
+				yield return int.Parse(nextResult.Text);
+			}
+		}
+
 		public static (DateTime, TimeSpan) RecogniseDateTimeRange(string source, out string rawString)
 		{
 			List<ModelResult> aiResults = DateTimeRecognizer.RecognizeDateTime(source, Culture.English);
diff --git a/RhinoReminds/ClientListener.cs b/RhinoReminds/ClientListener.cs
index e5870ac..b82bcbf 100644
--- a/RhinoReminds/ClientListener.cs
+++ b/RhinoReminds/ClientListener.cs
@@ -110,6 +110,33 @@ namespace RhinoReminds
 
 			switch (parts[0].ToLower())
 			{
+				case "delete":
+				case "remove":
+					List<int> failed = new List<int>(),
+						succeeded = new List<int>();
+					foreach (int nextId in AIRecogniser.RecogniseNumbers(message.Body)) {
+						Reminder nextReminder = reminderList.GetById(nextId);
+						if (nextReminder.JidObj != message.From.GetBareJid()) {
+							failed.Add(nextId);
+							continue;
+						}
+
+						reminderList.DeleteReminder(nextReminder);
+						succeeded.Add(nextId);
+					}
+
+					if (failed.Count > 0) {
+						string response = string.Join(", ", failed.Select((int nextId) => $"#{nextId}"));
+						response = $"Sorry! I can't delete reminder{(failed.Count != 1 ? "s" : "")} {response}, as you didn't create {(failed.Count != 1 ? "them":"it")}.";
+						sendChatReply(message, response);
+					}
+					if (succeeded.Count > 0) {
+						string response = string.Join(", ", succeeded.Select((int nextId) => $"#{nextId}"));
+						response = $"Deleted reminder{(succeeded.Count != 1 ? "s" : "")} {response} successfully.";
+						sendChatReply(message, response);
+					}
+					break;
+
 				case "list":
 				case "show":
 					if (parts.Select((n) => n.ToLower()).Contains("all")) {
@@ -165,6 +192,7 @@ namespace RhinoReminds
 					Reminder newReminder = reminderList.CreateReminder(message.From, dateTime, reminder);
 					reminderList.Save(ReminderFilePath);
 					break;
+
 				default:
 					sendChatReply(message, "I don't understand that. Try rephrasing it or asking for help.");
 					break;
diff --git a/RhinoReminds/ReminderList.cs b/RhinoReminds/ReminderList.cs
index b2ca0c0..adc401c 100644
--- a/RhinoReminds/ReminderList.cs
+++ b/RhinoReminds/ReminderList.cs
@@ -19,12 +19,14 @@ namespace RhinoReminds
 		public event OnReminderListUpdateHandler OnReminderListUpdate;
 
 
-		public ReminderList() {
+		public ReminderList()
+		{
 
 		}
 
 
-		public Reminder CreateReminder(Jid inJid, DateTime time, string message) {
+		public Reminder CreateReminder(Jid inJid, DateTime time, string message)
+		{
 			Reminder result = new Reminder(nextId++, $"{inJid.Node}@{inJid.Domain}", time, message);
 			Console.WriteLine($"[Rhino/ReminderList] Created reminder {result}");
 			while (Reminders.ContainsKey(time))
@@ -34,7 +36,6 @@ namespace RhinoReminds
 			return result;
 		}
 
-
 		public Reminder GetNextReminder()
 		{
 			if (Reminders.Count == 0)
@@ -43,6 +44,11 @@ namespace RhinoReminds
 			return Reminders.Values[0];
 		}
 
+		public Reminder GetById(int targetId)
+		{
+			return Reminders.First((KeyValuePair<DateTime, Reminder> nextPair) => nextPair.Value.Id == targetId).Value;
+		}
+
 		public void DeleteReminder(Reminder nextReminder)
 		{
 			Reminders.Remove(nextReminder.Time);
