import {LitElement, html} from './lit-all.min.js';
import * as tfrpc from '/static/tfrpc.js';
import {styles} from './tf-styles.js';
class TfTabConnectionsElement extends LitElement {
	static get properties() {
		return {
			broadcasts: {type: Array},
			identities: {type: Array},
			my_identities: {type: Array},
			connections: {type: Array},
			stored_connections: {type: Array},
			users: {type: Object},
			server_identity: {type: String},
		};
	}
	static styles = styles;
	constructor() {
		super();
		let self = this;
		this.broadcasts = [];
		this.identities = [];
		this.my_identities = [];
		this.connections = [];
		this.stored_connections = [];
		this.users = {};
		tfrpc.rpc.getIdentities().then(function (identities) {
			self.my_identities = identities || [];
		});
		tfrpc.rpc.getAllIdentities().then(function (identities) {
			self.identities = identities || [];
		});
		tfrpc.rpc.getStoredConnections().then(function (connections) {
			self.stored_connections = connections || [];
		});
		tfrpc.rpc.getServerIdentity().then(function (identity) {
			self.server_identity = identity;
		});
	}
	render_connection_summary(connection) {
		if (connection.address && connection.port) {
			return html`
				${connection.address}:${connection.port}
			
`;
		} else if (connection.tunnel) {
			return html`room peer
`;
		} else {
			return JSON.stringify(connection);
		}
	}
	render_room_peers(connection) {
		let self = this;
		let peers = this.broadcasts.filter((x) => x.tunnel?.id == connection);
		if (peers.length) {
			let connections = this.connections.map((x) => x.id);
			return html`${peers
				.filter((x) => connections.indexOf(x.pubkey) == -1)
				.map((x) => html`${self.render_room_peer(x)}`)}`;
		}
	}
	async _tunnel(portal, target) {
		return tfrpc.rpc.createTunnel(portal, target);
	}
	render_room_peer(connection) {
		let self = this;
		return html`
			
				
				 📡
			
		`;
	}
	render_broadcast(connection) {
		return html`
			
				
				
					
					${this.render_connection_summary(connection)}
				
			
		`;
	}
	async forget_stored_connection(connection) {
		await tfrpc.rpc.forgetStoredConnection(connection);
		this.stored_connections = (await tfrpc.rpc.getStoredConnections()) || [];
	}
	render_connection(connection) {
		return html`
			
			
			${connection.tunnel !== undefined
				? '🚇'
				: html`(${connection.host}:${connection.port})`}
			
				${connection.requests.map(
					(x) => html`
						${x.request_number > 0 ? '🟩' : '🟥'} ${x.name}
					`
				)}
			
			
				${this.connections
					.filter((x) => x.tunnel === this.connections.indexOf(connection))
					.map((x) => html`- ${this.render_connection(x)}
 `)}
				${this.render_room_peers(connection.id)}
			
		`;
	}
	render() {
		let self = this;
		return html`
			
				New Connection
				
				
				Broadcasts
				
					${this.broadcasts
						.filter((x) => x.address)
						.map((x) => self.render_broadcast(x))}
				
				Connections
				
					${this.connections
						.filter((x) => x.tunnel === undefined)
						.map(
							(x) => html`
								- ${this.render_connection(x)}
 
							`
						)}
				
				Stored Connections
				
					${this.stored_connections.map(
						(x) => html`
							- 
								
								
								
							
 
						`
					)}
				
				Local Accounts
				
					${this.identities.map(
						(x) =>
							html`- 
								${x == this.server_identity
									? html`🖥 local server`
									: undefined}
								${this.my_identities.indexOf(x) != -1
									? html`😎 you`
									: undefined}
								
							
 `
					)}
				
			 
		`;
	}
}
customElements.define('tf-tab-connections', TfTabConnectionsElement);