var UndeckedClipboard = new class UndeckedClipboard {
	constructor() {}

	hasKeyInClipboard() {
		return localStorage.getItem('clipboard') != undefined && localStorage.getItem('clipboard').length > 0;
	}

	copyKey(originKeyX: number, originKeyY: number) {
		localStorage.setItem('clipboard', `key_copy_${PageHandler.currentPageID}.${originKeyX}.${originKeyY}`);
		UndeckedNotification('Key has been copied to clipboard');
	}

	copyGhostKey(originKeyX: number, originKeyY: number) {
		localStorage.setItem('clipboard', `key_ghost_${PageHandler.currentPageID}.${originKeyX}.${originKeyY}`);
		UndeckedNotification('Key has been copied to clipboard as a ghost');
	}

	cutKey(originKeyX: number, originKeyY: number) {
		//TODO: Implement something in the front end to show that the item is being cut right now
		localStorage.setItem('clipboard', `key_cut_${PageHandler.currentPageID}.${originKeyX}.${originKeyY}`);
		UndeckedNotification('Key has been cut to clipboard');
	}

	pasteKey(destinationKeyX: number, destinationKeyY: number) {
		if (this.hasKeyInClipboard()) {
			var clipboard = this.decodeClipboard();

			if (clipboard.elementType == 'key') {
				socket.emit(
					'page',
					'operation',
					clipboard.operationType,
					clipboard.id,
					clipboard.x,
					clipboard.y,
					PageHandler.currentPageID,
					destinationKeyX,
					destinationKeyY
				);

				if (clipboard.operationType == 'cut') localStorage.setItem('clipboard', '');
			}
		}
	}

	decodeClipboard(): {
		elementType: 'key';
		operationType: 'cut' | 'copy' | 'ghost';
		id: string;
		x?: number;
		y?: number;
	} {
		var clipboard = {
			elementType: null,
			operationType: null,
			id: null,
			x: null,
			y: null
		};

		if (this.hasKeyInClipboard()) {
			var raw = localStorage.getItem('clipboard').split('_');
			clipboard.elementType = raw[0];
			clipboard.operationType = raw[1];
			var args = raw[2].split('.');
			clipboard.id = args[0];
			clipboard.x = args[1];
			clipboard.y = args[2];
			return clipboard;
		} else return clipboard;
	}
}();
