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
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
<script lang="ts">
	import * as Dialog from "@/components/ui/dialog";
	import { Button } from "@/components/ui/button";
	import * as Card from "@/components/ui/card";
	import * as Alert from "@/components/ui/alert";
	import { Badge } from "@/components/ui/badge";
	import { toast } from "svelte-sonner";
	import { videoManager } from "$lib/elements/video//VideoManager.svelte";
	import type { VideoInstance } from "$lib/elements/video//VideoManager.svelte";

	interface Props {
		workspaceId: string;
		open: boolean;
		video: VideoInstance;
	}

	let { open = $bindable(), video, workspaceId }: Props = $props();

    let isConnecting = $state(false);
    let error = $state<string | null>(null);
    let customRoomId = $state('');
    let hasLoadedRooms = $state(false);

    // Auto-load rooms when modal opens (only once per modal session)
    $effect(() => {
        if (open && !hasLoadedRooms && !videoManager.roomsLoading) {
            refreshRooms();
            hasLoadedRooms = true;
        }
        
        // Reset when modal closes
        if (!open) {
            hasLoadedRooms = false;
            error = null;
        }
    });

    async function refreshRooms() {
        try {
            error = null;
            await videoManager.refreshRooms(workspaceId);
        } catch (err) {
            error = err instanceof Error ? err.message : 'Failed to refresh rooms';
        }
    }

    async function handleStartOutputToRoom(roomId: string) {
        try {
            isConnecting = true;
            error = null;
            const result = await videoManager.startVideoOutputToRoom(workspaceId, video.id, roomId);
            if (result.success) {
                toast.success("Broadcasting Started", {
                    description: `Successfully started broadcasting to room ${roomId}`
                });
            } else {
                error = result.error || 'Failed to start output to room';
            }
        } catch (err) {
            error = err instanceof Error ? err.message : 'Failed to start output to room';
        } finally {
            isConnecting = false;
        }
    }

    async function createRoom() {
        try {
            isConnecting = true;
            error = null;
            const roomId = customRoomId.trim() || video.id;
            const result = await videoManager.createVideoRoom(workspaceId, roomId);
            
            if (result.success) {
                customRoomId = '';
                await refreshRooms();
                toast.success("Room Created", {
                    description: `Successfully created room ${result.roomId}`
                });
            } else {
                error = result.error || 'Failed to create room';
            }
        } catch (err) {
            error = err instanceof Error ? err.message : 'Failed to create room';
        } finally {
            isConnecting = false;
        }
    }

    async function createRoomAndStartOutput() {
        try {
            isConnecting = true;
            error = null;
            const roomId = customRoomId.trim() || video.id;
            const result = await videoManager.startVideoOutputAsProducer(workspaceId, video.id);
            if (result.success) {
                customRoomId = '';
                await refreshRooms();
                toast.success("Room Created & Broadcasting", {
                    description: `Successfully created room and started broadcasting`
                });
            } else {
                error = result.error || 'Failed to create room and start output';
            }
        } catch (err) {
            error = err instanceof Error ? err.message : 'Failed to create room and start output';
        } finally {
            isConnecting = false;
        }
    }

    async function handleStopOutput() {
        try {
            isConnecting = true;
            error = null;
            await videoManager.stopVideoOutput(video.id);
            toast.success("Broadcasting Stopped", {
                description: "Successfully stopped video broadcasting"
            });
        } catch (err) {
            error = err instanceof Error ? err.message : 'Failed to stop broadcasting';
        } finally {
            isConnecting = false;
        }
    }
</script>

<Dialog.Root bind:open>
	<Dialog.Content
		class="max-h-[85vh] max-w-4xl overflow-hidden border-slate-600 bg-slate-900 text-slate-100"
	>
		<Dialog.Header class="pb-3">
			<Dialog.Title class="flex items-center gap-2 text-lg font-bold text-slate-100">
				<span class="icon-[mdi--video] size-5 text-blue-400"></span>
				Video Output - {video?.name || 'No Video Selected'}
			</Dialog.Title>
			<Dialog.Description class="text-sm text-slate-400">
				Broadcast your local camera to remote viewers in rooms
			</Dialog.Description>
		</Dialog.Header>

		<div class="max-h-[calc(85vh-10rem)] overflow-y-auto">
			<div class="space-y-4 pb-4">
				<!-- Error display -->
				{#if error}
					<Alert.Root class="border-red-500/30 bg-red-900/20">
						<span class="icon-[mdi--alert-circle] size-4 text-red-400"></span>
						<Alert.Title class="text-red-300">Broadcasting Error</Alert.Title>
						<Alert.Description class="text-red-400 text-sm">
							{error}
						</Alert.Description>
					</Alert.Root>
				{/if}
			<!-- Current Status Overview -->
			<Card.Root class="border-blue-500/30 bg-blue-900/20">
				<Card.Content class="p-4">
					<div class="flex items-center justify-between">
						<div class="flex items-center gap-2">
							<span class="icon-[mdi--video] size-4 text-blue-400"></span>
							<span class="text-sm font-medium text-blue-300">Current Video Output</span>
						</div>
						{#if video?.hasOutput}
							<Badge variant="default" class="bg-blue-600 text-xs">
								Broadcasting
							</Badge>
						{:else}
							<Badge variant="secondary" class="text-xs text-slate-400">Not Broadcasting</Badge>
						{/if}
					</div>
					{#if video?.hasOutput}
						<div class="mt-2 text-xs text-blue-400/70">
							{#if video.output.roomId}
								Room: {video.output.roomId}
							{:else}
								Broadcasting to server
							{/if}
						</div>
					{/if}
				</Card.Content>
			</Card.Root>

			<!-- Output Requirements -->
			{#if !video?.canOutput}
				<Card.Root class="border-yellow-500/30 bg-yellow-500/5">
					<Card.Header>
						<Card.Title class="flex items-center gap-2 text-base text-yellow-200">
							<span class="icon-[mdi--alert] size-4"></span>
							Requirements
						</Card.Title>
					</Card.Header>
					<Card.Content>
						<div class="space-y-2 text-sm text-yellow-300">
							{#if !video?.hasInput}
								<div class="flex items-center gap-2">
									<span class="icon-[mdi--close-circle] size-3 text-red-400"></span>
									No video input connected
								</div>
							{:else if video.input.type !== 'local-camera'}
								<div class="flex items-center gap-2">
									<span class="icon-[mdi--close-circle] size-3 text-red-400"></span>
									Input must be local camera (cannot re-broadcast remote streams)
								</div>
							{/if}
							<div class="mt-2 text-xs text-yellow-400/70">
								To enable output, first connect to your local camera in the Video Input modal.
							</div>
						</div>
					</Card.Content>
				</Card.Root>
			{/if}

			<!-- Current Output Details -->
			{#if video?.hasOutput}
				<Card.Root class="border-blue-500/30 bg-blue-500/5">
					<Card.Header>
						<Card.Title class="flex items-center gap-2 text-base text-blue-200">
							<span class="icon-[mdi--broadcast] size-4"></span>
							Broadcasting
						</Card.Title>
					</Card.Header>
					<Card.Content>
						<div class="rounded-lg border border-blue-500/30 bg-blue-900/20 p-3">
							<div class="flex items-center justify-between">
								<div>
									<p class="text-sm font-medium text-blue-300">
										Streaming to Server
									</p>
									{#if video.output.roomId}
										<p class="text-xs text-blue-400/70">
											Room ID: {video.output.roomId}
										</p>
									{/if}
									<p class="text-xs text-blue-400/70">
										Source: Local Camera
									</p>
									{#if video.input.stream}
										<p class="text-xs text-blue-400/70">
											Video: {video.input.stream.getVideoTracks().length} tracks
										</p>
										<p class="text-xs text-blue-400/70">
											Audio: {video.input.stream.getAudioTracks().length} tracks
										</p>
									{/if}
								</div>
								<Button
									variant="destructive"
									size="sm"
									onclick={handleStopOutput}
									class="h-7 px-2 text-xs"
								>
									<span class="icon-[mdi--stop] mr-1 size-3"></span>
									Stop
								</Button>
							</div>
						</div>
					</Card.Content>
				</Card.Root>
			{/if}

			<!-- Remote Broadcasting -->
			<Card.Root class="border-orange-500/30 bg-orange-500/5">
				<Card.Header>
					<div class="flex items-center justify-between">
						<div>
							<Card.Title class="flex items-center gap-2 text-base text-orange-200">
								<span class="icon-[mdi--broadcast] size-4"></span>
								Remote Broadcasting (Rooms)
							</Card.Title>
							<Card.Description class="text-xs text-orange-300/70">
								Broadcast your camera feed to remote viewers in rooms
							</Card.Description>
						</div>
						<Button
							variant="ghost"
							size="sm"
							onclick={refreshRooms}
							disabled={videoManager.roomsLoading || isConnecting}
							class="h-7 px-2 text-xs text-orange-300 hover:text-orange-200 hover:bg-orange-500/20"
						>
							{#if videoManager.roomsLoading}
								<span class="icon-[mdi--loading] animate-spin size-3 mr-1"></span>
								Refreshing
							{:else}
								<span class="icon-[mdi--refresh] size-3 mr-1"></span>
								Refresh
							{/if}
						</Button>
					</div>
				</Card.Header>
				<Card.Content class="space-y-4">
					{#if video?.hasOutput}
						<!-- Broadcasting State -->
						<div class="rounded-lg border border-orange-500/30 bg-orange-900/20 p-3">
							<div class="flex items-center justify-between">
								<div>
									<p class="text-sm font-medium text-orange-300">Broadcasting Active</p>
									<p class="text-xs text-orange-400/70">Sending video to remote viewers</p>
								</div>
								<Button
									variant="destructive"
									size="sm"
									onclick={handleStopOutput}
									disabled={isConnecting}
									class="h-7 px-2 text-xs"
								>
									<span class="icon-[mdi--stop] mr-1 size-3"></span>
									{isConnecting ? 'Stopping...' : 'Stop'}
								</Button>
							</div>
						</div>
					{:else if video?.canOutput}
						<!-- Create New Room -->
						<div class="rounded border-2 border-dashed border-green-500/50 bg-green-500/5 p-3">
							<div class="space-y-2">
								<div class="flex items-center gap-2">
									<span class="icon-[mdi--plus-circle] size-4 text-green-400"></span>
									<p class="text-sm font-medium text-green-300">Create New Room</p>
								</div>
								<p class="text-xs text-green-400/70">
									Create a room to broadcast your camera feed
								</p>
								<input
									bind:value={customRoomId}
									placeholder={`Room ID (default: ${video.id})`}
									disabled={isConnecting || video?.hasOutput}
									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
										variant="secondary"
										size="sm"
										onclick={createRoom}
										disabled={isConnecting || video?.hasOutput}
										class="h-6 px-2 text-xs bg-green-600 hover:bg-green-700 disabled:opacity-50"
									>
										Create Only
									</Button>
									<Button
										variant="secondary"
										size="sm"
										onclick={createRoomAndStartOutput}
										disabled={isConnecting || video?.hasOutput}
										class="h-6 px-2 text-xs bg-green-600 hover:bg-green-700 disabled:opacity-50"
									>
										Create & Start
									</Button>
								</div>
							</div>
						</div>

						<!-- Existing Rooms -->
						<div class="space-y-2">
							<div class="flex items-center justify-between">
								<span class="text-xs font-medium text-orange-300">Join Existing Room:</span>
								<span class="text-xs text-slate-400">
									{videoManager.rooms.length} room{videoManager.rooms.length !== 1 ? 's' : ''} available
								</span>
							</div>
							
							<div class="max-h-40 space-y-2 overflow-y-auto">
								{#if videoManager.rooms.length === 0}
									<div class="text-center py-3 text-xs text-slate-400">
										{videoManager.roomsLoading ? 'Loading rooms...' : 'No rooms available. Create one to get started.'}
									</div>
								{:else}
									{#each videoManager.rooms as room}
										<div class="rounded border border-slate-600 bg-slate-800/50 p-2">
											<div class="flex items-start justify-between gap-3">
												<div class="flex-1 min-w-0">
													<p class="text-xs font-medium text-slate-200 truncate">
														{room.id}
													</p>
													<div class="flex gap-3 text-xs text-slate-400">
														<span>{room.participants?.producer ? '🔴 Occupied' : '🟢 Available'}</span>
														<span>👥 {room.participants?.consumers?.length || 0} viewers</span>
													</div>
												</div>
												{#if !room.participants?.producer}
													<Button
														variant="secondary"
														size="sm"
														onclick={() => handleStartOutputToRoom(room.id)}
														disabled={isConnecting || video?.hasOutput}
														class="h-6 px-2 text-xs bg-orange-600 hover:bg-orange-700 shrink-0 disabled:opacity-50"
													>
														<span class="icon-[mdi--broadcast] mr-1 size-3"></span>
														Join as Output
													</Button>
												{:else}
													<Button
														variant="ghost"
														size="sm"
														disabled
														class="text-xs opacity-50 shrink-0"
													>
														Occupied
													</Button>
												{/if}
											</div>
										</div>
									{/each}
								{/if}
							</div>
						</div>

						{#if video?.hasOutput}
							<p class="text-xs text-slate-500">
								Stop current broadcast to join a different room
							</p>
						{/if}
					{:else}
						<!-- Cannot output -->
						<div class="space-y-3">
							<div class="text-center text-sm text-slate-500">
								Connect to local camera first to enable broadcasting
							</div>
						</div>
					{/if}
				</Card.Content>
			</Card.Root>

			<!-- Help Information -->
			<Alert.Root class="border-slate-700 bg-slate-800/30">
				<span class="icon-[mdi--help-circle] size-4 text-slate-400"></span>
				<Alert.Title class="text-slate-300">Video Broadcasting</Alert.Title>
				<Alert.Description class="text-slate-400 text-xs">
					<strong>Requirements:</strong> Local camera input only • <strong>Remote streams:</strong> Cannot be re-broadcasted • Only one output per room
				</Alert.Description>
			</Alert.Root>
			</div>
		</div>
	</Dialog.Content>
</Dialog.Root>