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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<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';
  import { USBConsumer } from '../drivers/USBConsumer.js';
  import { USBProducer } from '../drivers/USBProducer.js';

  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();
    }
  });

  // Set up calibration completion callback with position sync
  robot.calibrationManager.onCalibrationCompleteWithPositions((finalPositions) => {
    console.log('[ConnectionPanel] Calibration complete, syncing robot to final positions');
    robot.syncToCalibrationPositions(finalPositions);
  });

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

      // Check if calibration is needed
      if (robot.calibrationManager.needsCalibration) {
        pendingUSBConnection = 'consumer';
        showUSBCalibration = true;
        return; // Don't proceed with connection yet
      }

      await robot.setConsumer({
        type: 'usb',
        baudRate: 1000000
      });
    } catch (err) {
      console.error('Failed to connect USB consumer:', err);
      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;

      // Check if calibration is needed
      if (robot.calibrationManager.needsCalibration) {
        pendingUSBConnection = 'producer';
        showUSBCalibration = true;
        return; // Don't proceed with connection yet
      }

      await robot.addProducer({
        type: 'usb',
        baudRate: 1000000
      });
    } catch (err) {
      console.error('Failed to connect USB producer:', err);
      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="bg-slate-800 border border-slate-600 rounded-lg p-4 space-y-4">
    <h3 class="text-lg font-semibold text-slate-100">Connections - {robot.id}</h3>

    <!-- Error display -->
    {#if error}
      <div class="bg-red-900/20 border border-red-500/30 rounded p-2 text-red-400 text-sm">
        {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="bg-slate-700/30 border border-slate-600 rounded p-3 space-y-3">
          <!-- Refresh Rooms -->
          <div class="flex items-center gap-2">
            <button
              onclick={refreshRooms}
              disabled={roomsLoading || connecting}
              class="px-3 py-1 bg-blue-600 hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded text-xs"
            >
              {#if roomsLoading}
                <span class="icon-[mdi--loading] animate-spin size-3 mr-1"></span>
                Loading...
              {:else}
                <span class="icon-[mdi--refresh] size-3 mr-1"></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 px-2 py-1 bg-slate-700 border border-slate-600 rounded text-xs text-slate-100 disabled:opacity-50"
                   />
                   <div class="flex gap-1">
                     <button
                       onclick={createRoom}
                       disabled={connecting}
                       class="flex-1 px-2 py-1 bg-green-600 hover:bg-green-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded text-xs"
                     >
                       {#if connecting}
                         <span class="icon-[mdi--loading] animate-spin size-3 mr-1"></span>
                         Creating...
                       {:else}
                         Create
                       {/if}
                     </button>
                     <button
                       onclick={createRoomAndJoinAsProducer}
                       disabled={connecting}
                       class="flex-1 px-2 py-1 bg-green-600 hover:bg-green-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded text-xs"
                     >
                       {#if connecting}
                         <span class="icon-[mdi--loading] animate-spin size-3 mr-1"></span>
                         Creating...
                       {:else}
                         Create & Join
                       {/if}
                     </button>
                   </div>
                 </div>
               </div>

                             <!-- Existing Rooms -->
               {#if rooms.length === 0}
                 <div class="text-center py-2 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="text-sm font-medium text-slate-200 truncate">{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 px-2 py-1 bg-purple-600 hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded text-xs"
                       >
                         Join as Consumer
                       </button>
                       <button
                         onclick={() => {
                           selectedRoomId = room.id;
                           joinRoomAsProducer();
                         }}
                         disabled={connecting}
                         class="flex-1 px-2 py-1 bg-orange-600 hover:bg-orange-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded text-xs"
                       >
                         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 bg-green-900/20 border border-green-500/30 rounded p-2">
          <div>
            <span class="text-green-300 text-sm">{consumer?.name || 'Consumer Active'}</span>
            <span class="text-xs text-slate-500 ml-2">
              {consumer?.status.isConnected ? '🟒 Connected' : 'πŸ”΄ Disconnected'}
            </span>
          </div>
          <button
            onclick={disconnectConsumer}
            disabled={connecting}
            class="px-3 py-1 bg-red-600 hover:bg-red-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded text-xs"
          >
            {connecting ? 'Disconnecting...' : 'Disconnect'}
          </button>
        </div>
      {:else}
        <div class="space-y-2">
          <button
            onclick={connectUSBConsumer}
            disabled={connecting}
            class="w-full py-2 px-3 bg-blue-600 hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded text-sm"
          >
            {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 px-3 py-2 bg-slate-700 border border-slate-600 rounded text-sm text-slate-100 disabled:opacity-50"
              />
              <button
                onclick={connectRemoteConsumer}
                disabled={connecting}
                class="px-3 py-2 bg-purple-600 hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded text-sm whitespace-nowrap"
              >
                {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 py-2 px-3 bg-green-600 hover:bg-green-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded text-sm"
        >
          {connecting ? 'Connecting...' : 'Add USB Producer'}
        </button>
        <button
          onclick={connectRemoteProducer}
          disabled={connecting}
          class="w-full py-2 px-3 bg-orange-600 hover:bg-orange-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded text-sm"
        >
          {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 bg-slate-700/50 border border-slate-600 rounded p-2">
          <div>
            <span class="text-slate-300 text-sm">{producer.name}</span>
            <span class="text-xs text-slate-500 ml-2">
              {producer.status.isConnected ? '🟒 Connected' : 'πŸ”΄ Disconnected'}
            </span>
          </div>
          <button
            onclick={() => disconnectProducer(producer.id)}
            disabled={connecting}
            class="px-2 py-1 bg-red-600 hover:bg-red-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded text-xs"
          >
            {connecting ? 'Removing...' : 'Remove'}
          </button>
        </div>
      {/each}
    </div>

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

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

        <USBCalibrationPanel 
          calibrationManager={robot.calibrationManager}
          connectionType={pendingUSBConnection || 'consumer'}
          {onCalibrationComplete}
          onCancel={onCalibrationCancel}
        />
      </div>
    </div>
  {/if}
</div>