import * as tfrpc from '/tfrpc.js';

tfrpc.register(async function get_private_key(id) {
	return bip39Words(await ssb.getPrivateKey(id));
});
tfrpc.register(async function create_id(id) {
	return await ssb.createIdentity();
});
tfrpc.register(async function add_id(id) {
	return await ssb.addIdentity(bip39Bytes(id));
});
tfrpc.register(async function delete_id(id) {
	return await ssb.deleteIdentity(id);
});
tfrpc.register(async function reload() {
	await main();
});

async function main() {
	let ids = await ssb.getIdentities();
	await app.setDocument(
		`<body style="color: #fff">
		<script>const handler = {};</script>
		<script type="module">
			import * as tfrpc from '/static/tfrpc.js';
			handler.export_id = async function export_id(event) {
				let id = event.srcElement.dataset.id;
				let element = document.createElement('textarea');
				element.value = await tfrpc.rpc.get_private_key(id);
				element.style = 'width: 100%; read-only: true';
				element.readOnly = true;
				event.srcElement.parentElement.appendChild(element);
				event.srcElement.onclick = event => handler.hide_id(event, element);
			}
			handler.add_id = async function add_id(event) {
				let id = document.getElementById('add_id').value;
				try {
					let new_id = await tfrpc.rpc.add_id(id);
					alert('Successfully imported: ' + new_id);
					await tfrpc.rpc.reload();
				} catch (e) {
					alert('Error importing identity: ' + e);
				}
			}
			handler.create_id = async function create_id(event) {
				try {
					let id = await tfrpc.rpc.create_id();
					alert('Successfully created: ' + id);
					await tfrpc.rpc.reload();
				} catch (e) {
					alert('Error creating identity: ' + e);
				}
			}
			handler.hide_id = function hide_id(event, element) {
				element.parentNode.removeChild(element);
				event.srcElement.onclick = handler.export_id;
			}
			handler.delete_id = async function delete_id(event) {
				let id = event.srcElement.dataset.id;
				try {
					if (prompt('Are you sure you want to delete "' + id + '"?  It cannot be recovered without the exported phrase.\\n\\nEnter the word "DELETE" to confirm you wish to delete it.') === 'DELETE') {
						if (await tfrpc.rpc.delete_id(id)) {
							alert('Successfully deleted ID: ' + id);
						}
						await tfrpc.rpc.reload();
					}
				} catch (e) {
					alert('Error deleting ID: ' + e);
				}
			}
		</script>
		<h1>SSB Identity Management</h1>
		<h2>Create a new identity</h2>
		<button id="create_id" onclick="handler.create_id()">Create Identity</button>
		<h2>Import an SSB Identity from 12 BIP39 English Words</h2>
		<textarea id="add_id" style="width: 100%" rows="4"></textarea><button id="add" onclick="handler.add_id(event)">Import Identity</button>
		<h2>Identities</h2>
		<ul>` +
			ids
				.map(
					(id) => `<li>
			<button onclick="handler.export_id(event)" data-id="${id}">Export Identity</button>
			<button onclick="handler.delete_id(event)" data-id="${id}">Delete Identity</button>
			${id}
		</li>`
				)
				.join('\n') +
			`	</ul>
	</body>`
	);
}

main();
