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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f62f94b
 
 
 
 
 
 
 
 
 
 
 
3cdf7b9
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
<script lang="ts">
	import type { Robot } from "../Robot.svelte.js";
	import { robotManager } from "../RobotManager.svelte.js";
	import { settings } from "$lib/runes/settings.svelte";
	import USBCalibrationPanel from "../calibration/USBCalibrationPanel.svelte";

	interface Props {
		robot: Robot;
		workspaceId: string;
	}

	let { robot, workspaceId }: Props = $props();

	const hasConsumer = $derived(robot.hasConsumer);
	const outputDriverCount = $derived(robot.outputDriverCount);
	const consumer = $derived(robot.consumer);
	const producers = $derived(robot.producers);

	// Connection configs - using transport server for communication
	let remoteRobotId = $state(robot.id);
	let connecting = $state(false);
	let error = $state<string | null>(null);

	// USB connection flow state
	let showUSBCalibration = $state(false);
	let pendingUSBConnection: "consumer" | "producer" | null = $state(null);

	// Room management state
	const rooms = $derived(robotManager.rooms);
	const roomsLoading = $derived(robotManager.roomsLoading);
	let selectedRoomId = $state("");
	let newRoomId = $state("");
	let showRoomManagement = $state(true); // Show rooms by default

	// Auto-load rooms when component loads
	$effect(() => {
		if (rooms.length === 0 && !roomsLoading && workspaceId) {
			refreshRooms();
		}
	});

	// Find USB driver for calibration (if any)
	function getUSBDriver(): any {
		// Check consumer first
		if (robot.consumer && 'calibrationState' in robot.consumer) {
			return robot.consumer;
		}
		// Then check producers
		return robot.producers.find(p => 'calibrationState' in p) || null;
	}

	async function connectUSBConsumer() {
		try {
			connecting = true;
			error = null;

			// USB drivers handle their own calibration requirements
			await robot.setConsumer({
				type: "usb",
				baudRate: 1000000
			});
		} catch (err) {
			console.error("Failed to connect USB consumer:", err);
			// Check if it's a calibration error
			if (err instanceof Error && err.message.includes('calibration')) {
				pendingUSBConnection = "consumer";
				showUSBCalibration = true;
				return;
			}
			error = err instanceof Error ? err.message : "Unknown error";
		} finally {
			connecting = false;
		}
	}

	async function connectRemoteConsumer() {
		try {
			connecting = true;
			error = null;
			await robot.setConsumer({
				type: "remote",
				url: settings.transportServerUrl,
				robotId: remoteRobotId,
				workspaceId: workspaceId
			});
		} catch (err) {
			console.error("Failed to connect remote consumer:", err);
			error = err instanceof Error ? err.message : "Unknown error";
		} finally {
			connecting = false;
		}
	}

	async function connectUSBProducer() {
		try {
			connecting = true;
			error = null;

			// USB drivers handle their own calibration requirements
			await robot.addProducer({
				type: "usb",
				baudRate: 1000000
			});
		} catch (err) {
			console.error("Failed to connect USB producer:", err);
			// Check if it's a calibration error
			if (err instanceof Error && err.message.includes('calibration')) {
				pendingUSBConnection = "producer";
				showUSBCalibration = true;
				return;
			}
			error = err instanceof Error ? err.message : "Unknown error";
		} finally {
			connecting = false;
		}
	}

	async function connectRemoteProducer() {
		try {
			connecting = true;
			error = null;
			await robot.addProducer({
				type: "remote",
				url: settings.transportServerUrl,
				robotId: remoteRobotId,
				workspaceId: workspaceId
			});
		} catch (err) {
			console.error("Failed to connect remote producer:", err);
			error = err instanceof Error ? err.message : "Unknown error";
		} finally {
			connecting = false;
		}
	}

	async function disconnectConsumer() {
		try {
			connecting = true;
			error = null;
			await robot.removeConsumer();
		} catch (err) {
			console.error("Failed to disconnect consumer:", err);
			error = err instanceof Error ? err.message : "Unknown error";
		} finally {
			connecting = false;
		}
	}

	async function disconnectProducer(producerId: string) {
		try {
			connecting = true;
			error = null;
			await robot.removeProducer(producerId);
		} catch (err) {
			console.error("Failed to disconnect producer:", err);
			error = err instanceof Error ? err.message : "Unknown error";
		} finally {
			connecting = false;
		}
	}

	// Room management functions
	async function refreshRooms() {
		try {
			await robotManager.refreshRooms(workspaceId);
		} catch (err) {
			console.error("Failed to refresh rooms:", err);
			error = err instanceof Error ? err.message : "Failed to refresh rooms";
		}
	}

	async function createRoom() {
		try {
			connecting = true;
			error = null;
			const result = await robotManager.createRoboticsRoom(workspaceId, newRoomId || undefined);
			if (result.success) {
				newRoomId = "";
				await refreshRooms();
			} else {
				error = result.error || "Failed to create room";
			}
		} catch (err) {
			console.error("Failed to create room:", err);
			error = err instanceof Error ? err.message : "Failed to create room";
		} finally {
			connecting = false;
		}
	}

	async function joinRoomAsConsumer() {
		if (!selectedRoomId) {
			error = "Please select a room";
			return;
		}

		try {
			connecting = true;
			error = null;
			await robotManager.connectConsumerToRoom(workspaceId, robot.id, selectedRoomId);
		} catch (err) {
			console.error("Failed to join room as consumer:", err);
			error = err instanceof Error ? err.message : "Failed to join room as consumer";
		} finally {
			connecting = false;
		}
	}

	async function joinRoomAsProducer() {
		if (!selectedRoomId) {
			error = "Please select a room";
			return;
		}

		try {
			connecting = true;
			error = null;
			await robotManager.connectProducerToRoom(workspaceId, robot.id, selectedRoomId);
		} catch (err) {
			console.error("Failed to join room as producer:", err);
			error = err instanceof Error ? err.message : "Failed to join room as producer";
		} finally {
			connecting = false;
		}
	}

	async function createRoomAndJoinAsProducer() {
		try {
			connecting = true;
			error = null;
			const result = await robotManager.connectProducerAsProducer(
				workspaceId,
				robot.id,
				newRoomId || undefined
			);
			if (result.success) {
				newRoomId = "";
				await refreshRooms();
			} else {
				error = result.error || "Failed to create room and join as producer";
			}
		} catch (err) {
			console.error("Failed to create room and join as producer:", err);
			error = err instanceof Error ? err.message : "Failed to create room and join as producer";
		} finally {
			connecting = false;
		}
	}

	// Handle calibration completion
	async function onCalibrationComplete() {
		showUSBCalibration = false;

		if (pendingUSBConnection === "consumer") {
			await connectUSBConsumer();
		} else if (pendingUSBConnection === "producer") {
			await connectUSBProducer();
		}

		pendingUSBConnection = null;
	}

	function onCalibrationCancel() {
		showUSBCalibration = false;
		pendingUSBConnection = null;
		connecting = false;
	}
</script>

<div class="space-y-4">
	<!-- Connection panel -->
	<div class="space-y-4 rounded-lg border border-slate-600 bg-slate-800 p-4">
		<h3 class="text-lg font-semibold text-slate-100">Connections - {robot.id}</h3>

		<!-- Error display -->
		{#if error}
			<div class="rounded border border-red-500/30 bg-red-900/20 p-2 text-sm text-red-400">
				{error}
			</div>
		{/if}

		<!-- Room Management Section -->
		<div class="space-y-2">
			<div class="flex items-center justify-between">
				<h4 class="text-sm font-medium text-slate-300">Room Management</h4>
				<button
					onclick={() => (showRoomManagement = !showRoomManagement)}
					class="text-xs text-blue-400 hover:text-blue-300"
				>
					{showRoomManagement ? "Hide" : "Show"} Rooms
				</button>
			</div>

			{#if showRoomManagement}
				<div class="space-y-3 rounded border border-slate-600 bg-slate-700/30 p-3">
					<!-- Refresh Rooms -->
					<div class="flex items-center gap-2">
						<button
							onclick={refreshRooms}
							disabled={roomsLoading || connecting}
							class="rounded bg-blue-600 px-3 py-1 text-xs text-white hover:bg-blue-700 disabled:cursor-not-allowed disabled:opacity-50"
						>
							{#if roomsLoading}
								<span class="icon-[mdi--loading] mr-1 size-3 animate-spin"></span>
								Loading...
							{:else}
								<span class="icon-[mdi--refresh] mr-1 size-3"></span>
								Refresh Rooms
							{/if}
						</button>
						<span class="text-xs text-slate-400">
							{rooms.length} room{rooms.length !== 1 ? "s" : ""} available
						</span>
					</div>

					<!-- Available Rooms -->
					<div class="space-y-2">
						<span class="text-xs text-slate-400">Available Rooms:</span>
						<div class="max-h-48 space-y-2 overflow-y-auto">
							<!-- Create New Room Option -->
							<div class="rounded border-2 border-dashed border-green-500/50 bg-green-500/5 p-2">
								<div class="space-y-2">
									<div class="flex items-center gap-2">
										<span class="text-sm font-medium text-green-300">Create New Room</span>
									</div>
									<p class="text-xs text-green-400/70">Create a room for collaboration</p>
									<input
										bind:value={newRoomId}
										placeholder="Room ID (optional)"
										disabled={connecting}
										class="w-full rounded border border-slate-600 bg-slate-700 px-2 py-1 text-xs text-slate-100 disabled:opacity-50"
									/>
									<div class="flex gap-1">
										<button
											onclick={createRoom}
											disabled={connecting}
											class="flex-1 rounded bg-green-600 px-2 py-1 text-xs text-white hover:bg-green-700 disabled:cursor-not-allowed disabled:opacity-50"
										>
											{#if connecting}
												<span class="icon-[mdi--loading] mr-1 size-3 animate-spin"></span>
												Creating...
											{:else}
												Create
											{/if}
										</button>
										<button
											onclick={createRoomAndJoinAsProducer}
											disabled={connecting}
											class="flex-1 rounded bg-green-600 px-2 py-1 text-xs text-white hover:bg-green-700 disabled:cursor-not-allowed disabled:opacity-50"
										>
											{#if connecting}
												<span class="icon-[mdi--loading] mr-1 size-3 animate-spin"></span>
												Creating...
											{:else}
												Create & Join
											{/if}
										</button>
									</div>
								</div>
							</div>

							<!-- Existing Rooms -->
							{#if rooms.length === 0}
								<div class="py-2 text-center text-xs text-slate-500">
									{roomsLoading ? "Loading..." : "No existing rooms available"}
								</div>
							{:else}
								{#each rooms as room}
									<div class="rounded border border-slate-600 bg-slate-700/30 p-2">
										<div class="mb-2">
											<p class="truncate text-sm font-medium text-slate-200">{room.id}</p>
											<p class="text-xs text-slate-400">
												{room.participants?.total || 0} participants
											</p>
										</div>
										<div class="flex gap-1">
											<button
												onclick={() => {
													selectedRoomId = room.id;
													joinRoomAsConsumer();
												}}
												disabled={connecting}
												class="flex-1 rounded bg-purple-600 px-2 py-1 text-xs text-white hover:bg-purple-700 disabled:cursor-not-allowed disabled:opacity-50"
											>
												Join as Consumer
											</button>
											<button
												onclick={() => {
													selectedRoomId = room.id;
													joinRoomAsProducer();
												}}
												disabled={connecting}
												class="flex-1 rounded bg-orange-600 px-2 py-1 text-xs text-white hover:bg-orange-700 disabled:cursor-not-allowed disabled:opacity-50"
											>
												Join as Producer
											</button>
										</div>
									</div>
								{/each}
							{/if}
						</div>
					</div>
				</div>
			{/if}
		</div>

		<!-- Consumer Section (Input) - SINGLE -->
		<div class="space-y-2">
			<h4 class="text-sm font-medium text-slate-300">Consumer (Receive Commands) - Single</h4>
			{#if hasConsumer}
				<div
					class="flex items-center justify-between rounded border border-green-500/30 bg-green-900/20 p-2"
				>
					<div>
						<span class="text-sm text-green-300">{consumer?.name || "Consumer Active"}</span>
						<span class="ml-2 text-xs text-slate-500">
							{consumer?.status.isConnected ? "🟒 Connected" : "πŸ”΄ Disconnected"}
						</span>
					</div>
					<button
						onclick={disconnectConsumer}
						disabled={connecting}
						class="rounded bg-red-600 px-3 py-1 text-xs text-white hover:bg-red-700 disabled:cursor-not-allowed disabled:opacity-50"
					>
						{connecting ? "Disconnecting..." : "Disconnect"}
					</button>
				</div>
			{:else}
				<div class="space-y-2">
					<button
						onclick={connectUSBConsumer}
						disabled={connecting}
						class="w-full rounded bg-blue-600 px-3 py-2 text-sm text-white hover:bg-blue-700 disabled:cursor-not-allowed disabled:opacity-50"
					>
						{connecting ? "Connecting..." : "Connect USB Consumer"}
					</button>
					<div class="space-y-2">
						<div class="flex gap-2">
							<input
								bind:value={settings.transportServerUrl}
								placeholder="Transport server URL (e.g. http://localhost:8000)"
								disabled={connecting}
								class="flex-1 rounded border border-slate-600 bg-slate-700 px-3 py-2 text-sm text-slate-100 disabled:opacity-50"
							/>
							<button
								onclick={connectRemoteConsumer}
								disabled={connecting}
								class="rounded bg-purple-600 px-3 py-2 text-sm whitespace-nowrap text-white hover:bg-purple-700 disabled:cursor-not-allowed disabled:opacity-50"
							>
								{connecting ? "Connecting..." : "Remote Consumer"}
							</button>
						</div>
						<div class="text-xs text-slate-500">
							Remote Consumer: Receive commands from transport server
						</div>
					</div>
				</div>
			{/if}
		</div>

		<!-- Producers Section (Output) - MULTIPLE -->
		<div class="space-y-2">
			<h4 class="text-sm font-medium text-slate-300">
				Producers (Send Commands) - {outputDriverCount} connected
			</h4>
			<div class="space-y-2">
				<button
					onclick={connectUSBProducer}
					disabled={connecting}
					class="w-full rounded bg-green-600 px-3 py-2 text-sm text-white hover:bg-green-700 disabled:cursor-not-allowed disabled:opacity-50"
				>
					{connecting ? "Connecting..." : "Add USB Producer"}
				</button>
				<button
					onclick={connectRemoteProducer}
					disabled={connecting}
					class="w-full rounded bg-orange-600 px-3 py-2 text-sm text-white hover:bg-orange-700 disabled:cursor-not-allowed disabled:opacity-50"
				>
					{connecting ? "Connecting..." : "Add Remote Producer"}
				</button>
				<div class="text-xs text-slate-500">
					Remote Producer: Send commands to transport server. Uses Robot ID: {remoteRobotId}
				</div>
			</div>

			<!-- Connected Producers List -->
			{#each producers as producer}
				<div
					class="flex items-center justify-between rounded border border-slate-600 bg-slate-700/50 p-2"
				>
					<div>
						<span class="text-sm text-slate-300">{producer.name}</span>
						<span class="ml-2 text-xs text-slate-500">
							{producer.status.isConnected ? "🟒 Connected" : "πŸ”΄ Disconnected"}
						</span>
					</div>
					<button
						onclick={() => disconnectProducer(producer.id)}
						disabled={connecting}
						class="rounded bg-red-600 px-2 py-1 text-xs text-white hover:bg-red-700 disabled:cursor-not-allowed disabled:opacity-50"
					>
						{connecting ? "Removing..." : "Remove"}
					</button>
				</div>
			{/each}
		</div>

		<!-- Robot ID Config -->
		<div class="border-t border-slate-600 pt-2">
			<span class="text-xs text-slate-400">Robot ID for Remote Connections:</span>
			<input
				bind:value={remoteRobotId}
				disabled={connecting}
				class="mt-1 w-full rounded border border-slate-600 bg-slate-700 px-3 py-1 text-sm text-slate-100 disabled:opacity-50"
			/>
		</div>
	</div>

	<!-- USB Calibration Modal - Only shown when connecting USB drivers -->
	{#if showUSBCalibration}
		<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
			<div class="m-4 w-full max-w-2xl space-y-4 rounded-lg bg-slate-800 p-6">
				<div class="flex items-center justify-between">
					<h2 class="text-lg font-semibold text-white">
						USB Calibration Required
						{#if pendingUSBConnection}
							<span class="ml-2 text-sm text-slate-400">
								(for {pendingUSBConnection === "consumer" ? "Consumer" : "Producer"})
							</span>
						{/if}
					</h2>
					<button onclick={onCalibrationCancel} class="text-gray-400 hover:text-white"> βœ• </button>
				</div>

				<div class="mb-4 text-sm text-slate-300">
					Before connecting USB drivers, the robot needs to be calibrated to map its physical range
					to software values.
				</div>

				{#if getUSBDriver()}
					<USBCalibrationPanel
						calibrationManager={getUSBDriver()}
						connectionType={pendingUSBConnection || "consumer"}
						{onCalibrationComplete}
						onCancel={onCalibrationCancel}
					/>
				{:else}
					<div class="text-center text-slate-400">
						No USB driver available for calibration
					</div>
				{/if}
			</div>
		</div>
	{/if}
</div>