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64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
import { FS, Utils } from '../../lib';
import { YouTube } from './youtube';

const MINUTE = 60 * 1000;
const PRENOM_BUMP_TIME = 2 * 60 * MINUTE;

const PRENOMS_FILE = 'config/chat-plugins/otd-prenoms.json';
const DATA_FILE = 'config/chat-plugins/otds.json';

export const prenoms: { [k: string]: [string, AnyObject][] } = JSON.parse(FS(PRENOMS_FILE).readIfExistsSync() || "{}");
export const otdData: OtdData = JSON.parse(FS(DATA_FILE).readIfExistsSync() || "{}");
export const otds = new Map<string, OtdHandler>();

const FINISH_HANDLERS: { [k: string]: (winner: AnyObject) => Promise<void> } = {
	cotw: async winner => {
		const { channel, nominator } = winner;
		const searchResults = await YouTube.searchChannel(channel, 1);
		const result = searchResults?.[0];
		if (result) {
			if (YouTube.data.channels[result]) return;
			void YouTube.getChannelData(`https://www.youtube.com/channel/${result}`);
			const yt = Rooms.get('youtube');
			if (!yt) return;
			yt.sendMods(
				`|c|~|/log The channel with ID ${result} was added to the YouTube channel database.`
			);
			yt.modlog({
				action: `ADDCHANNEL`,
				note: `${result} (${toID(nominator)})`,
				loggedBy: toID(`COTW`),
			});
		}
	},
};

interface OtdSettings {
	id?: string;
	updateOnNom?: boolean;
	keys: string[];
	title: string;
	keyLabels: string[];
	timeLabel: string;
	roomid: RoomID;
}

interface OtdData {
	[k: string]: { settings: OtdSettings, winners: AnyObject[] };
}

function savePrenoms() {
	return FS(PRENOMS_FILE).writeUpdate(() => JSON.stringify(prenoms));
}

function toNominationId(nomination: string) {
	return nomination.toLowerCase().replace(/\s/g, '').replace(/\b&\b/g, '');
}

class OtdHandler {
	id: string;
	name: string;
	room: Room;
	nominations: Map<string, AnyObject>;
	removedNominations: Map<string, AnyObject>;
	voting: boolean;
	timer: NodeJS.Timeout | null;
	keys: string[];
	keyLabels: string[];
	timeLabel: string;
	settings: OtdSettings;
	lastPrenom: number;
	winners: AnyObject[];
	constructor(
		id: string, room: Room, settings: OtdSettings
	) {
		this.id = id;
		this.name = settings.title;
		this.room = room;

		this.nominations = new Map(prenoms[id]);
		this.removedNominations = new Map();

		this.voting = false;
		this.timer = null;

		this.keys = settings.keys;
		this.keyLabels = settings.keyLabels;
		this.timeLabel = settings.timeLabel;
		this.settings = settings;

		this.lastPrenom = 0;

		this.winners = otdData[this.id]?.winners || [];
	}

	static create(room: Room, settings: OtdSettings) {
		const { title, timeLabel } = settings;
		const id = settings.id || toID(title).charAt(0) + 'ot' + timeLabel.charAt(0);
		const handler = new OtdHandler(id, room, settings);
		otds.set(id, handler);
		let needsSave = false;
		for (const winner of handler.winners) {
			if (winner.timestamp) {
				winner.time = winner.timestamp;
				delete winner.timestamp;
				needsSave = true;
			}
		}
		if (needsSave) handler.save();
		return handler;
	}

	static parseOldWinners(content: string, keyLabels: string[], keys: string[]) {
		const data = `${content}`.split("\n");
		const winners = [];
		for (const arg of data) {
			if (!arg || arg === '\r') continue;
			if (arg.startsWith(`${keyLabels[0]}\t`)) {
				continue;
			}
			const entry: AnyObject = {};
			const vals = arg.trim().split("\t");
			for (let i = 0; i < vals.length; i++) {
				entry[keys[i]] = vals[i];
			}
			entry.time = Number(entry.time) || 0;
			winners.push(entry);
		}
		return winners;
	}

	/**
	 * Handles old-format data from the IP and userid refactor
	 */
	convertNominations() {
		for (const value of this.nominations.values()) {
			if (!Array.isArray(value.userids)) value.userids = Object.keys(value.userids);
			if (!Array.isArray(value.ips)) value.ips = Object.keys(value.ips);
		}
		for (const value of this.removedNominations.values()) {
			if (!Array.isArray(value.userids)) value.userids = Object.keys(value.userids);
			if (!Array.isArray(value.ips)) value.ips = Object.keys(value.ips);
		}
	}

	startVote() {
		this.voting = true;
		this.timer = setTimeout(() => this.rollWinner(), 20 * MINUTE);
		this.display();
	}

	finish() {
		this.voting = false;
		this.nominations = new Map();
		this.removedNominations = new Map();
		delete prenoms[this.id];
		void savePrenoms();
		if (this.timer) clearTimeout(this.timer);
	}

	addNomination(user: User, nomination: string) {
		const id = toNominationId(nomination);

		if (this.winners.slice(-30)
			.some(entry => toNominationId(entry[this.keys[0]]) === id)
		) {
			return user.sendTo(this.room, `This ${this.name.toLowerCase()} has already been ${this.id} in the past month.`);
		}
		for (const value of this.removedNominations.values()) {
			if (value.userids.includes(toID(user)) || (!Config.noipchecks && value.ips.includes(user.latestIp))) {
				return user.sendTo(
					this.room,
					`Since your nomination has been removed by staff, you cannot submit another ${this.name.toLowerCase()} until the next round.`
				);
			}
		}

		const prevNom = this.nominations.get(id);
		if (prevNom) {
			if (!(prevNom.userids.includes(toID(user)) || (!Config.noipchecks && prevNom.ips.includes(user.latestIp)))) {
				return user.sendTo(this.room, `This ${this.name.toLowerCase()} has already been nominated.`);
			}
		}

		for (const [key, value] of this.nominations) {
			if (value.userids.includes(toID(user)) || (!Config.noipchecks && value.ips.includes(user.latestIp))) {
				user.sendTo(this.room, `Your previous vote for ${value.nomination} will be removed.`);
				this.nominations.delete(key);
				if (prenoms[this.id]) {
					const idx = prenoms[this.id].findIndex(val => val[0] === key);
					if (idx > -1) {
						prenoms[this.id].splice(idx, 1);
						void savePrenoms();
					}
				}
			}
		}

		const obj: { [k: string]: string } = {};
		obj[user.id] = user.name;

		const nomObj = {
			nomination,
			name: user.name,
			userids: user.previousIDs.concat(user.id),
			ips: user.ips.slice(),
		};

		this.nominations.set(id, nomObj);

		if (!prenoms[this.id]) prenoms[this.id] = [];
		prenoms[this.id].push([id, nomObj]);
		void savePrenoms();

		user.sendTo(this.room, `Your nomination for ${nomination} was successfully submitted.`);

		let updateOnly = !this.voting;
		if (updateOnly) {
			const now = Date.now();
			if (now - this.lastPrenom > PRENOM_BUMP_TIME) {
				updateOnly = false;
				this.lastPrenom = now;
			}
		}

		if (this.settings.updateOnNom) {
			this.display(updateOnly);
		}
	}

	generateNomWindow() {
		let buffer = '';

		if (this.voting) {
			buffer += `<div class="broadcast-blue"><p style="font-weight:bold;text-align:center;font-size:12pt;">Nominations for ${this.name} of the ${this.timeLabel} are in progress! Use <code>/${this.id} nom</code> to nominate a${['A', 'E', 'I', 'O', 'U'].includes(this.name[0]) ? 'n' : ''} ${this.name.toLowerCase()}!</p>`;
			if (this.nominations.size) buffer += `<span style="font-weight:bold;">Nominations:</span>`;
		} else {
			buffer += `<div class="broadcast-blue"><p style="font-weight:bold;text-align:center;font-size:10pt;">Pre-noms for ${this.name} of the ${this.timeLabel}. Use <code>/${this.id} nom</code> to nominate a${['A', 'E', 'I', 'O', 'U'].includes(this.name[0]) ? 'n' : ''} ${this.name.toLowerCase()}:</p>`;
		}

		if (this.winners.length) {
			const winner = this.winners[this.winners.length - 1];

			buffer += `<p>Current winner: <b>${winner[this.keys[0]]}</b> (nominated by ${winner.nominator})</p>`;
		}

		const entries = [];

		for (const value of this.nominations.values()) {
			entries.push(Utils.html`<li><b>${value.nomination}</b> <i>(Submitted by ${value.name})</i></li>`);
		}

		if (entries.length > 20) {
			buffer += `<table><tr><td><ul>${entries.slice(0, Math.ceil(entries.length / 2)).join('')}</ul></td><td><ul>${entries.slice(Math.ceil(entries.length / 2)).join('')}</ul></td></tr></table>`;
		} else {
			buffer += `<ul>${entries.join('')}</ul>`;
		}

		buffer += `</div>`;

		return buffer;
	}

	display(update = false) {
		if (update) {
			this.room.uhtmlchange('otd', this.generateNomWindow());
		} else {
			this.room.add(`|uhtml|otd|${this.generateNomWindow()}`);
		}
	}

	displayTo(connection: Connection) {
		connection.sendTo(this.room, `|uhtml|otd|${this.generateNomWindow()}`);
	}

	rollWinner() {
		const keys = [...this.nominations.keys()];
		if (!keys.length) return false;

		const winner = this.nominations.get(keys[Math.floor(Math.random() * keys.length)]);
		if (!winner) return false; // Should never happen but shuts typescript up.
		const winnerEntry = this.appendWinner(winner.nomination, winner.name);

		const names = [...this.nominations.values()].map(obj => obj.name);

		const columns = names.length > 27 ? 4 : names.length > 18 ? 3 : names.length > 9 ? 2 : 1;
		let content = '';
		for (let i = 0; i < columns; i++) {
			content += `<td>${names.slice(Math.ceil((i / columns) * names.length), Math.ceil(((i + 1) / columns) * names.length)).join('<br />')}</td>`;
		}
		const namesHTML = `<table><tr>${content}</tr></table></p></div>`;

		const finishHandler = FINISH_HANDLERS[this.id];
		if (finishHandler) {
			void finishHandler(winnerEntry);
		}
		this.room.add(
			Utils.html`|uhtml|otd|<div class="broadcast-blue"><p style="font-weight:bold;text-align:center;font-size:12pt;">` +
			`Nominations for ${this.name} of the ${this.timeLabel} are over!</p><p style="tex-align:center;font-size:10pt;">` +
			`Out of ${keys.length} nominations, we randomly selected <strong>${winner.nomination}</strong> as the winner! ` +
			`(Nomination by ${winner.name})</p><p style="font-weight:bold;">Thanks to today's participants:` + namesHTML
		);
		this.room.update();

		this.finish();
		return true;
	}

	removeNomination(name: string) {
		name = toID(name);

		let success = false;
		for (const [key, value] of this.nominations) {
			if (value.userids.includes(name)) {
				this.removedNominations.set(key, value);
				this.nominations.delete(key);
				if (prenoms[this.id]) {
					const idx = prenoms[this.id].findIndex(val => val[0] === key);
					if (idx > -1) {
						prenoms[this.id].splice(idx, 1);
						void savePrenoms();
					}
				}
				success = true;
			}
		}

		if (this.voting) this.display();
		return success;
	}

	forceWinner(winner: string, user: string) {
		this.appendWinner(winner, user);
		this.finish();
	}

	appendWinner(nomination: string, user: string): AnyObject {
		const entry: AnyObject = { time: Date.now(), nominator: user };
		entry[this.keys[0]] = nomination;
		this.winners.push(entry);
		this.save();
		return entry;
	}

	removeWinner(nominationName: string) {
		for (const [i, entry] of this.winners.entries()) {
			if (toID(entry[this.keys[0]]) === toID(nominationName)) {
				const removed = this.winners.splice(i, 1);
				this.save();
				return removed[0];
			}
		}
		throw new Chat.ErrorMessage(`The winner with nomination ${nominationName} could not be found.`);
	}

	setWinnerProperty(properties: { [k: string]: string }) {
		if (!this.winners.length) return;
		for (const i in properties) {
			this.winners[this.winners.length - 1][i] = properties[i];
		}
		return this.save();
	}

	save(destroy = false) {
		if (!destroy) {
			otdData[this.id] = {
				settings: this.settings,
				winners: this.winners,
			};
		}
		FS(DATA_FILE).writeUpdate(() => JSON.stringify(otdData));
	}

	destroy() {
		this.room = null!;
		delete otdData[this.id];
		otds.delete(this.id);
		delete Chat.commands[this.id];
		delete Chat.pages[this.id];
		this.save(true);
	}

	async generateWinnerDisplay() {
		if (!this.winners.length) return false;
		const winner = this.winners[this.winners.length - 1];

		let output = `<div class="broadcast-blue" style="text-align:center;">` +
			Utils.html`<p><span style="font-weight:bold;font-size:11pt">The ${this.name} of the ${this.timeLabel} is ` +
			Utils.html`${winner[this.keys[0]]}${winner.author ? ` by ${winner.author}` : ''}.</span>`;

		if (winner.quote) output += Utils.html`<br /><span style="font-style:italic;">"${winner.quote}"</span>`;
		if (winner.tagline) output += Utils.html`<br />${winner.tagline}`;
		output += `</p><table style="margin:auto;"><tr>`;
		if (winner.image) {
			try {
				const [width, height] = await Chat.fitImage(winner.image, 100, 100);
				output += Utils.html`<td><img src="${winner.image}" width=${width} height=${height}></td>`;
			} catch {}
		}
		output += `<td style="text-align:right;margin:5px;">`;
		if (winner.event) output += Utils.html`<b>Event:</b> ${winner.event}<br />`;
		if (winner.song) {
			output += `<b>Song:</b> `;
			if (winner.link) {
				output += Utils.html`<a href="${winner.link}">${winner.song}</a>`;
			} else {
				output += Utils.escapeHTML(winner.song);
			}
			output += `<br />`;
		} else if (winner.link) {
			output += Utils.html`<b>Link:</b> <a href="${winner.link}">${winner.link}</a><br />`;
		}

		// Batch these together on 2 lines. Order intentional.
		const athleteDetails = [];
		if (winner.sport) athleteDetails.push(Utils.html`<b>Sport:</b> ${winner.sport}`);
		if (winner.team) athleteDetails.push(Utils.html`<b>Team:</b> ${winner.team}`);
		if (winner.age) athleteDetails.push(Utils.html`<b>Age:</b> ${winner.age}`);
		if (winner.country) athleteDetails.push(Utils.html`<b>Nationality:</b> ${winner.country}`);

		if (athleteDetails.length) {
			output += athleteDetails.slice(0, 2).join(' | ') + '<br />';
			if (athleteDetails.length > 2) output += athleteDetails.slice(2).join(' | ') + '<br />';
		}

		output += Utils.html`Nominated by ${winner.nominator}.`;
		output += `</td></tr></table></div>`;

		return output;
	}

	generateWinnerList(context: Chat.PageContext) {
		context.title = `${this.id.toUpperCase()} Winners`;
		let buf = `<div class="pad ladder"><h2>${this.name} of the ${this.timeLabel} Winners</h2>`;

		// Only use specific fields for displaying in winners list.
		const columns: string[] = [];
		const labels = [];

		for (let i = 0; i < this.keys.length; i++) {
			if (i === 0 || ['song', 'event', 'link', 'tagline', 'sport', 'country']
				.includes(this.keys[i]) && !(this.keys[i] === 'link' && this.keys.includes('song'))
			) {
				columns.push(this.keys[i]);
				labels.push(this.keyLabels[i]);
			}
		}
		if (!columns.includes('time')) {
			columns.push('time');
			labels.push('Timestamp');
		}

		let content = ``;

		content += `<tr>${labels.map(label => `<th><h3>${label}</h3></th>`).join('')}</tr>`;
		for (let i = this.winners.length - 1; i >= 0; i--) {
			const entry = columns.map(col => {
				let val = this.winners[i][col];
				if (!val) return '';
				switch (col) {
				case 'time':
					const date = new Date(parseInt(this.winners[i].time));

					const pad = (num: number) => `${num}`.padStart(2, '0');

					return Utils.html`${pad(date.getMonth() + 1)}-${pad(date.getDate())}-${date.getFullYear()}`;
				case 'song':
					if (!this.winners[i].link) return val;
					// falls through
				case 'link':
					return `<a href="${this.winners[i].link}">${val}</a>`;
				case 'book':
					val = `${val}${this.winners[i].author ? ` by ${this.winners[i].author}` : ''}`;
					// falls through
				case columns[0]:
					return `${Utils.escapeHTML(val)}${this.winners[i].nominator ? Utils.html`<br /><span style="font-style:italic;font-size:8pt;">nominated by ${this.winners[i].nominator}</span>` : ''}`;
				default:
					return Utils.escapeHTML(val);
				}
			});
			content += `<tr>${entry.map(val => `<td style="max-width:${600 / columns.length}px;word-wrap:break-word;">${val}</td>`).join('')}</tr>`;
		}
		if (!content) {
			buf += `<p>There have been no ${this.id} winners.</p>`;
		} else {
			buf += `<table>${content}</table>`;
		}
		buf += `</div>`;
		return buf;
	}
}

function selectHandler(message: string) {
	const id = toID(message.substring(1).split(' ')[0]);
	const handler = otds.get(id);
	if (!handler) throw new Error("Invalid type for otd handler.");
	return handler;
}

export const otdCommands: Chat.ChatCommands = {
	start(target, room, user, connection, cmd) {
		this.checkChat();

		const handler = selectHandler(this.message);

		if (!handler.room) return this.errorReply(`The room for this -otd doesn't exist.`);
		if (room !== handler.room) return this.errorReply(`This command can only be used in ${handler.room.title}.`);
		this.checkCan('mute', null, room);

		if (handler.voting) {
			return this.errorReply(
				`The nomination for the ${handler.name} of the ${handler.timeLabel} nomination is already in progress.`
			);
		}
		handler.startVote();

		this.privateModAction(`${user.name} has started nominations for the ${handler.name} of the ${handler.timeLabel}.`);
		this.modlog(`${handler.id.toUpperCase()} START`, null);
	},
	starthelp: [`/-otd start - Starts nominations for the Thing of the Day. Requires: % @ # ~`],

	end(target, room, user) {
		this.checkChat();

		const handler = selectHandler(this.message);

		if (!handler.room) return this.errorReply(`The room for this -otd doesn't exist.`);
		if (room !== handler.room) return this.errorReply(`This command can only be used in ${handler.room.title}.`);
		this.checkCan('mute', null, room);

		if (!handler.voting) {
			return this.errorReply(`There is no ${handler.name} of the ${handler.timeLabel} nomination in progress.`);
		}
		if (!handler.nominations.size) {
			return this.errorReply(`Can't select the ${handler.name} of the ${handler.timeLabel} without nominations.`);
		}
		handler.rollWinner();

		this.privateModAction(`${user.name} has ended nominations for the ${handler.name} of the ${handler.timeLabel}.`);
		this.modlog(`${handler.id.toUpperCase()} END`, null);
	},
	endhelp: [
		`/-otd end - End nominations for the Thing of the Day and set it to a randomly selected nomination. Requires: % @ # ~`,
	],

	nom(target, room, user) {
		this.checkChat(target);
		if (!target) return this.parse('/help otd');

		const handler = selectHandler(this.message);

		if (!handler.room) return this.errorReply(`The room for this -otd doesn't exist.`);
		if (room !== handler.room) return this.errorReply(`This command can only be used in ${handler.room.title}.`);

		if (!toNominationId(target).length || target.length > 75) {
			return this.sendReply(`'${target}' is not a valid ${handler.name.toLowerCase()} name.`);
		}
		handler.addNomination(user, target);
	},
	nomhelp: [`/-otd nom [nomination] - Nominate something for Thing of the Day.`],

	view(target, room, user, connection) {
		this.checkChat();
		if (!this.runBroadcast()) return false;

		const handler = selectHandler(this.message);

		if (!handler.room) return this.errorReply(`The room for this -otd doesn't exist.`);
		if (room !== handler.room) return this.errorReply(`This command can only be used in ${handler.room.title}.`);

		if (this.broadcasting) {
			selectHandler(this.message).display();
		} else {
			selectHandler(this.message).displayTo(connection);
		}
	},
	viewhelp: [`/-otd view - View the current nominations for the Thing of the Day.`],

	remove(target, room, user) {
		this.checkChat();

		const handler = selectHandler(this.message);

		if (!handler.room) return this.errorReply(`The room for this -otd doesn't exist.`);
		if (room !== handler.room) return this.errorReply(`This command can only be used in ${handler.room.title}.`);
		this.checkCan('mute', null, room);

		const userid = toID(target);
		if (!userid) return this.errorReply(`'${target}' is not a valid username.`);

		if (handler.removeNomination(userid)) {
			this.privateModAction(`${user.name} removed ${target}'s nomination for the ${handler.name} of the ${handler.timeLabel}.`);
			this.modlog(`${handler.id.toUpperCase()} REMOVENOM`, userid);
		} else {
			this.sendReply(`User '${target}' has no nomination for the ${handler.name} of the ${handler.timeLabel}.`);
		}
	},
	removehelp: [
		`/-otd remove [username] - Remove a user's nomination for the Thing of the Day.`,
		`Prevents them from voting again until the next round. Requires: % @ # ~`,
	],

	removewinner(target, room, user) {
		const handler = selectHandler(this.message);
		room = this.requireRoom(handler.room.roomid);
		this.checkCan('mute', null, room);

		if (!toID(target)) {
			return this.parse(`/help aotd removewinner`);
		}
		const removed = handler.removeWinner(target);
		this.privateModAction(`${user.name} removed the nomination for ${removed[handler.keys[0]]} from ${removed.nominator}`);
		this.modlog(`${handler.id.toUpperCase()} REMOVEWINNER`, removed.nominator, removed[handler.keys[0]]);
	},
	removewinnerhelp: [
		`/-otd removewinner [nomination] - Remove winners matching the given [nomination] from Thing of the Day.`,
		`Requires: % @ # ~`,
	],

	force(target, room, user) {
		this.checkChat();
		if (!target) return this.parse('/help aotd force');

		const handler = selectHandler(this.message);

		if (!handler.room) return this.errorReply(`The room for this -otd doesn't exist.`);
		if (room !== handler.room) return this.errorReply(`This command can only be used in ${handler.room.title}.`);
		this.checkCan('declare', null, room);

		if (!toNominationId(target).length || target.length > 50) {
			return this.sendReply(`'${target}' is not a valid ${handler.name.toLowerCase()} name.`);
		}
		handler.forceWinner(target, user.name);
		this.privateModAction(`${user.name} forcibly set the ${handler.name} of the ${handler.timeLabel} to ${target}.`);
		this.modlog(`${handler.id.toUpperCase()} FORCE`, user.name, target);
		room.add(`The ${handler.name} of the ${handler.timeLabel} was forcibly set to '${target}'`);
	},
	forcehelp: [
		`/-otd force [nomination] - Forcibly sets the Thing of the Day without a nomination round. Requires: # ~`,
	],

	delay(target, room, user) {
		this.checkChat();

		const handler = selectHandler(this.message);

		if (!handler.room) return this.errorReply(`The room for this -otd doesn't exist.`);
		if (room !== handler.room) return this.errorReply(`This command can only be used in ${handler.room.title}.`);
		this.checkCan('mute', null, room);

		if (!(handler.voting && handler.timer)) {
			return this.errorReply(`There is no ${handler.name} of the ${handler.timeLabel} nomination to disable the timer for.`);
		}
		clearTimeout(handler.timer);

		this.privateModAction(`${user.name} disabled the ${handler.name} of the ${handler.timeLabel} timer.`);
	},
	delayhelp: [
		`/-otd delay - Turns off the automatic 20 minute timer for Thing of the Day voting rounds. Requires: % @ # ~`,
	],

	set(target, room, user) {
		this.checkChat();

		const handler = selectHandler(this.message);

		if (!handler.room) return this.errorReply(`The room for this -otd doesn't exist.`);
		if (room !== handler.room) return this.errorReply(`This command can only be used in ${handler.room.title}.`);
		this.checkCan('mute', null, room);

		const params = target.split(target.includes('|') ? '|' : ',').map(param => param.trim());

		const changelist: { [k: string]: string } = {};

		for (const param of params) {
			let [key, ...values] = param.split(':');
			if (!key || !values.length) return this.errorReply(`Syntax error in '${param}'`);

			key = key.trim();
			const value = values.join(':').trim();

			if (!handler.keys.includes(key)) {
				return this.errorReply(`Invalid key: '${key}'. Valid keys: ${handler.keys.join(', ')}`);
			}

			switch (key) {
			case 'artist':
			case 'film':
			case 'show':
			case 'channel':
			case 'book':
			case 'author':
			case 'athlete':
				if (!toNominationId(value) || value.length > 50) return this.errorReply(`Please enter a valid ${key} name.`);
				break;
			case 'quote':
			case 'tagline':
			case 'match':
			case 'event':
			case 'videogame':
				if (!value.length || value.length > 150) return this.errorReply(`Please enter a valid ${key}.`);
				break;
			case 'sport':
			case 'team':
			case 'song':
			case 'country':
				if (!value.length || value.length > 50) return this.errorReply(`Please enter a valid ${key} name.`);
				break;
			case 'link':
			case 'image':
				if (!/https?:\/\//.test(value)) {
					return this.errorReply(`Please enter a valid URL for the ${key} (starting with http:// or https://)`);
				}
				if (value.length > 200) return this.errorReply("URL too long.");
				break;
			case 'age':
				const num = parseInt(value);
				// let's assume someone isn't over 100 years old? Maybe we should for the memes
				// but i doubt there's any legit athlete over 100.
				if (isNaN(num) || num < 1 || num > 100) return this.errorReply('Please enter a valid number as an age');
				break;
			default:
				// another custom key w/o validation
				if (!toNominationId(value)) {
					return this.errorReply(`No value provided for key ${key}.`);
				}
				break;
			}

			changelist[key] = value;
		}

		const keys = Object.keys(changelist);

		if (keys.length) {
			void handler.setWinnerProperty(changelist);
			this.modlog(handler.id.toUpperCase(), null, `changed ${keys.join(', ')}`);
			return this.privateModAction(`${user.name} changed the following propert${Chat.plural(keys, 'ies', 'y')} of the ${handler.name} of the ${handler.timeLabel}: ${keys.join(', ')}`);
		}
	},
	sethelp: [
		`/-otd set property: value[, property: value] - Set the winner, quote, song, link or image for the current Thing of the Day.`,
		`Requires: % @ # ~`,
	],

	toggleupdate(target, room, user) {
		const otd = selectHandler(this.message);
		room = this.requireRoom(otd.room.roomid);

		this.checkCan('declare', null, room);
		let logMessage = '';

		if (this.meansYes(target)) {
			if (otd.settings.updateOnNom) {
				throw new Chat.ErrorMessage(`This -OTD is already set to update automatically on nomination.`);
			}
			otd.settings.updateOnNom = true;
			logMessage = 'update automatically on nomination';
		} else {
			if (!otd.settings.updateOnNom) {
				throw new Chat.ErrorMessage(`This -OTD is not set to update automatically on nomination.`);
			}
			delete otd.settings.updateOnNom;
			logMessage = 'not update on nomination';
		}
		this.privateModAction(`${user.name} set the ${otd.name} of the ${otd.timeLabel} to ${logMessage}`);
		this.modlog(`OTD TOGGLEUPDATE`, null, logMessage);
		otd.save();
	},

	winners(target, room, user, connection) {
		this.checkChat();

		const handler = selectHandler(this.message);

		if (!handler.room) return this.errorReply(`The room for this -otd doesn't exist.`);
		if (room !== handler.room) return this.errorReply(`This command can only be used in ${handler.room.title}.`);

		return this.parse(`/join view-${handler.id}`);
	},
	winnershelp: [`/-otd winners - Displays a list of previous things of the day.`],

	async ''(target, room) {
		this.checkChat();
		if (!this.runBroadcast()) return false;

		const handler = selectHandler(this.message);
		if (!handler.room) return this.errorReply(`The room for this -otd doesn't exist.`);

		if (room !== handler.room) return this.errorReply(`This command can only be used in ${handler.room.title}.`);

		const text = await handler.generateWinnerDisplay();
		if (!text) return this.errorReply("There is no winner yet.");
		this.sendReplyBox(text);
	},
};

export const pages: Chat.PageTable = {};
export const commands: Chat.ChatCommands = {
	otd: {
		create(target, room, user) {
			room = this.requireRoom();
			if (room.settings.isPrivate) {
				return this.errorReply(`This command is only available in public rooms`);
			}
			const count = [...otds.values()].filter(otd => otd.room.roomid === room.roomid).length;
			if (count > 3) {
				return this.errorReply(`This room already has 3+ -otd's.`);
			}
			this.checkCan('rangeban');

			if (!toID(target)) {
				return this.parse(`/help otd`);
			}
			const [title, time, ...keyLabels] = target.split(',').map(i => i.trim());
			if (!toID(title)) {
				return this.errorReply(`Invalid title.`);
			}
			const timeLabel = toID(time);
			if (!['week', 'day'].includes(timeLabel)) {
				return this.errorReply("Invalid time label - use 'week' or 'month'");
			}
			const id = `${title.charAt(0)}ot${timeLabel.charAt(0)}`;
			const existing = otds.get(id);
			if (existing) {
				this.errorReply(`That -OTD already exists (${existing.name} of the ${existing.timeLabel}, in ${existing.room.title})`);
				return this.errorReply(`Try picking a new title.`);
			}
			const titleIdx = keyLabels.map(toID).indexOf(toID(title));
			if (titleIdx > -1) {
				keyLabels.splice(titleIdx, 1);
			}
			keyLabels.unshift(title);

			const filteredKeys = keyLabels.map(toNominationId).filter(Boolean);
			if (!filteredKeys.length) {
				return this.errorReply(`No valid key labels given.`);
			}
			if (new Set(filteredKeys).size !== keyLabels.length) {
				return this.errorReply(`Invalid keys in set - do not use duplicate key labels.`);
			}
			if (filteredKeys.length < 3) {
				return this.errorReply(`Specify at least 3 key labels.`);
			}
			if (filteredKeys.some(k => k.length < 3 || k.length > 50)) {
				return this.errorReply(`All labels must be more than 3 characters and less than 50 characters long.`);
			}
			const otd = OtdHandler.create(room, {
				keyLabels, keys: filteredKeys, title, timeLabel, roomid: room.roomid,
			});
			const name = `${otd.name} of the ${otd.timeLabel}`;
			this.globalModlog(`OTD CREATE`, null, `${name} - ${filteredKeys.join(', ')}`);
			this.privateGlobalModAction(`${user.name} created the ${name} for ${room.title}`);
			otd.save();
		},
		updateroom(target, room, user) {
			this.checkCan('rangeban');
			const [otdId, roomid] = target.split(',').map(i => toID(i));
			if (!otdId || !roomid) {
				return this.parse('/help otd');
			}
			const otd = otds.get(otdId);
			if (!otd) {
				return this.errorReply(`OTD ${otd} not found.`);
			}
			const targetRoom = Rooms.get(roomid);
			if (!targetRoom) {
				return this.errorReply(`Room ${roomid} not found.`);
			}
			const oldRoom = otd.settings.roomid.slice();
			otd.settings.roomid = targetRoom.roomid;
			otd.room = targetRoom;
			otd.save();
			this.privateGlobalModAction(
				`${user.name} updated the room for the ${otd.name} of the ${otd.timeLabel} from ${oldRoom} to ${targetRoom}`
			);
			this.globalModlog(`OTD UPDATEROOM`, null, `${otd.id} to ${targetRoom} from ${oldRoom}`);
		},
		delete(target, room, user) {
			this.checkCan('rangeban');
			target = toID(target);
			if (!target) {
				return this.parse(`/help otd`);
			}
			const otd = otds.get(target);
			if (!otd) return this.errorReply(`OTD ${target} not found.`);
			otd.destroy();
			this.globalModlog(`OTD DELETE`, null, target);
			this.privateGlobalModAction(`${user.name} deleted the OTD ${otd.name} of the ${otd.timeLabel}`);
		},
	},
	otdhelp: [
		`/otd create [title], [time], [...labels] - Creates a Thing of the Day with the given [name], [time], and [labels]. Requires: ~`,
		`/otd updateroom [otd], [room] - Updates the room for the given [otd] to the new [room]. Requires: ~`,
		`/otd delete [otd] - Removes the given Thing of the Day. Requires: ~`,
	],
};

const otdHelp = [
	`Thing of the Day plugin commands (aotd, fotd, sotd, cotd, botw, motw, anotd):`,
	`- /-otd - View the current Thing of the Day.`,
	`- /-otd start - Starts nominations for the Thing of the Day. Requires: % @ # ~`,
	`- /-otd nom [nomination] - Nominate something for Thing of the Day.`,
	`- /-otd remove [username] - Remove a user's nomination for the Thing of the Day and prevent them from voting again until the next round. Requires: % @ # ~`,
	`- /-otd end - End nominations for the Thing of the Day and set it to a randomly selected nomination. Requires: % @ # ~`,
	`- /-otd removewinner [nomination] - Remove a winner with the given [nomination] from the winners list. Requires: % @ # ~`,
	`- /-otd force [nomination] - Forcibly sets the Thing of the Day without a nomination round. Requires: # ~`,
	`- /-otd delay - Turns off the automatic 20 minute timer for Thing of the Day voting rounds. Requires: % @ # ~`,
	`- /-otd set property: value[, property: value] - Set the winner, quote, song, link or image for the current Thing of the Day. Requires: % @ # ~`,
	`- /-otd winners - Displays a list of previous things of the day.`,
	`- /-otd toggleupdate [on|off] - Changes the Thing of the Day to display on nomination ([on] to update, [off] to turn off updates). Requires: # ~`,
];

for (const otd in otdData) {
	const data = otdData[otd];
	const settings = data.settings;
	const room = Rooms.get(settings.roomid);
	if (!room) {
		Monitor.warn(`Room for -otd ${settings.title} of the ${settings.timeLabel} (${settings.roomid}) not found.`);
		continue;
	}
	OtdHandler.create(room, settings);
}

for (const [k, v] of otds) {
	pages[k] = function () {
		return v.generateWinnerList(this);
	};
	commands[k] = otdCommands;
	commands[`${k}help`] = otdHelp;
}

export const handlers: Chat.Handlers = {
	onRenameRoom(oldID, newID, room) {
		for (const otd in otdData) {
			const data = otdData[otd];
			if (data.settings.roomid === oldID) {
				data.settings.roomid = newID;
				const handler = otds.get(otd);
				handler!.room = room as Room;
				handler!.save();
			}
		}
	},
};

export const punishmentfilter: Chat.PunishmentFilter = (user, punishment) => {
	user = toID(user);
	if (!['NAMELOCK', 'BAN'].includes(punishment.type)) return;
	for (const handler of otds.values()) {
		handler.removeNomination(user);
	}
};