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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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636

import { create } from "zustand"

import { ClapProject, ClapSegment, newClap, parseClap } from "@aitube/clap"
import { getVideoPrompt } from "@aitube/engine"

import { LatentEngineStore } from "./types"
import { resolveSegments } from "../resolvers/resolveSegments"
import { fetchLatentClap } from "./generators/fetchLatentClap"

import { InteractiveSegmenterResult, MPMask } from "@mediapipe/tasks-vision"
import { segmentFrame } from "@/lib/on-device-ai/segmentFrameOnClick"
import { drawSegmentation } from "../utils/canvas/drawSegmentation"
import { getZIndexDepth } from "../utils/data/getZIndexDepth"
import { getSegmentStartAt } from "../utils/data/getSegmentStartAt"
import { getElementsSortedByStartAt } from "../utils/data/getElementsSortedByStartAt"
import { getSegmentEndAt } from "../utils/data/getSegmentEndAt"
import { setZIndexDepthId } from "../utils/data/setZIndexDepth"
import { setSegmentStartAt } from "../utils/data/setSegmentStartAt"
import { setSegmentEndAt } from "../utils/data/setSegmentEndAt"
import { setSegmentId } from "../utils/data/setSegmentId"

export const useLatentEngine = create<LatentEngineStore>((set, get) => ({
  jwtToken: "",

  width: 512,
  height: 288,

  clap: newClap(),
  debug: true,

  headless: false, // false by default

  isLoop: false,
  isStatic: false,
  isLive: false,
  isInteractive: false,

  isLoading: false, // true when a .clap is being downloaded and/or generated
  isLoaded: false, // true if a clap is loaded
  isPlaying: false,
  isPaused: true,

  // our "this is AI.. gasp!" disclaimer
  hasDisclaimer: true,
  hasPresentedDisclaimer: false,

  videoSimulationPromise: undefined,
  videoSimulationPending: false,
  videoSimulationStartedAt: performance.now(),
  videoSimulationEndedAt: performance.now(),
  videoSimulationDurationInMs: 0,
  videoSimulationVideoPlaybackFPS: 0,
  videoSimulationRenderingTimeFPS: 0,

  interfaceSimulationPromise: undefined,
  interfaceSimulationPending: false,
  interfaceSimulationStartedAt: performance.now(),
  interfaceSimulationEndedAt: performance.now(),
  interfaceSimulationDurationInMs: 0,

  entitySimulationPromise: undefined,
  entitySimulationPending: false,
  entitySimulationStartedAt: performance.now(),
  entitySimulationEndedAt: performance.now(),
  entitySimulationDurationInMs: 0,

  renderingIntervalId: undefined,
  renderingIntervalDelayInMs: 150, // 0.2s
  renderingLastRenderAt: performance.now(),

  // for our calculations to be correct
  // those need to match the actual output from the API
  // don't trust the parameters you send to the API,
  // instead check the *actual* values with VLC!!
  videoModelFPS: 24,
  videoModelNumOfFrames: 60, // 80,
  videoModelDurationInSec: 2.584,

  playbackSpeed: 1,

  positionInMs: 0,
  durationInMs: 0,

  // this is the "buffer size"
  videoLayers: [
    {
      id: "video-buffer-0",
      element: null as unknown as JSX.Element,
    },
    {
      id: "video-buffer-1",
      element: null as unknown as JSX.Element,
    },
    /*
    {
      id: "video-buffer-2",
      element: null as unknown as JSX.Element,
    },
    {
      id: "video-buffer-3",
      element: null as unknown as JSX.Element,
    },
    */
  ],
  videoElements: [],

  interfaceLayers: [],

  setJwtToken: (jwtToken: string) => {
    set({
      jwtToken
    })
  },

  setContainerDimension: ({ width, height }: { width: number; height: number }) => {
    set({
      width,
      height
    })
  },

  imagine: async (prompt: string): Promise<void> => {
    set({
      isLoaded: false,
      isLoading: true, 
    })

    let clap: ClapProject | undefined = undefined

    try {
      clap = await fetchLatentClap(prompt)
    } catch (err) {
      console.error(`generateAndLoad failed (${err})`)
      set({
        isLoading: false,
      })
    }

    if (!clap) { return }

    get().open(clap)
  },


  open: async (src?: string | ClapProject | Blob) => {
    const { debug } = get()
    set({
      isLoaded: false,
      isLoading: true, 
    })

    let clap: ClapProject | undefined = undefined

    try {
      clap = await parseClap(src, debug)
    } catch (err) {
      console.error(`failed to open the Clap: ${err}`)
      set({
        isLoading: false,
      })
    }

    if (!clap) { return }
 
    set({
      clap,
      isLoading: false,
      isLoaded: true,
      isLoop: clap.meta.isLoop,
      isStatic: !clap.meta.isInteractive,
      isLive: false,
      isInteractive: clap.meta.isInteractive,
    })
  },

  setVideoElements: (videoElements: HTMLVideoElement[] = []) => { set({ videoElements }) },

  processClickOnSegment: (result: InteractiveSegmenterResult) => {
    console.log(`processClickOnSegment: user clicked on something:`, result)

    const { videoElements, debug } = get()

    if (!result?.categoryMask) {
      if (debug) {
        console.log(`processClickOnSegment: no categoryMask, so we skip the click`)
      }
      return
    }

    try {
      if (debug) {
        console.log(`processClickOnSegment: callling drawSegmentation`)
      }

      const firstVisibleVideo = videoElements.find(element => 
        getZIndexDepth(element) > 0
      )

      const segmentationElements = Array.from(
        document.querySelectorAll('.segmentation-canvas')
      ) as HTMLCanvasElement[]

      const segmentationElement = segmentationElements.at(0)
   
      const canvasMask: HTMLCanvasElement = drawSegmentation({
        mask: result.categoryMask,
        canvas: segmentationElement,
        backgroundImage: firstVisibleVideo,
        fillStyle: "rgba(255, 255, 255, 1.0)"
      })
      // TODO: read the canvas te determine on what the user clicked

      if (debug) {
        console.log(`processClickOnSegment: filtering the original image`)
      }
      // filterImage(imageElement, canvasMask)

      if (debug) {
        console.log("processClickOnSegment: TODO call data.close() to free the memory!")
      }
      result.close()
    } catch (err) {
      console.error(`processClickOnSegment: something failed ${err}`)
    }
  },
  onClickOnSegmentationLayer: (event) => {

    const { videoElements, debug } = get()
    if (debug) {
      console.log("onClickOnSegmentationLayer")
    }

    const firstVisibleVideo = videoElements.find(element => 
      getZIndexDepth(element) > 0
    )
    if (!firstVisibleVideo) { return }

    const box = event.currentTarget.getBoundingClientRect()

    const px = event.clientX
    const py = event.clientY

    const x = px / box.width
    const y = py / box.height
    console.log(`onClickOnSegmentationLayer: user clicked on `, { x, y, px, py, box, videoElements })

    const fn = async () => {
      // todo julian: this should use the visible element instead
      const results: InteractiveSegmenterResult = await segmentFrame(firstVisibleVideo, x, y)
      get().processClickOnSegment(results)
    }
    fn()
  },
  
  togglePlayPause: (): boolean => {
    const { isLoaded, isPlaying, playbackSpeed, renderingIntervalId, videoElements } = get()
    if (!isLoaded) { return false }
    
    const newValue = !isPlaying

    clearInterval(renderingIntervalId)

    const firstVisibleVideo = videoElements.find(element => 
      getZIndexDepth(element) > 0
    )

    // Note Julian: we could also let the background scheduler
    // (runRenderingLoop) do its work of advancing the cursor here

    if (newValue) {
      if (firstVisibleVideo) {
        try {
          firstVisibleVideo.playbackRate = playbackSpeed
          firstVisibleVideo.play()
        } catch (err) {
          console.error(`togglePlayPause: failed to start the video (${err})`)
        }
      }
      set({
        isPlaying: true,
        renderingIntervalId: setTimeout(() => { get().runRenderingLoop() }, 0)
      })
    } else {
      if (firstVisibleVideo) {
        try {
          firstVisibleVideo.playbackRate = playbackSpeed
          firstVisibleVideo.pause()
        } catch (err) {
          console.error(`togglePlayPause: failed to pause the video (${err})`)
        }
      }
      set({ isPlaying: false })
    }

    return newValue
  },


  play: (): boolean => {
    const { isLoaded, isPlaying, renderingIntervalId, renderingIntervalDelayInMs } = get()

    if (!isLoaded) { return false }

    if (isPlaying) { return true }

    clearInterval(renderingIntervalId)
    set({
      isPlaying: true,
      renderingIntervalId: setTimeout(() => { get().runRenderingLoop() }, 0)
    })

    return true
  },

  pause: (): boolean => {
    const { isLoaded, renderingIntervalId } = get()
    if (!isLoaded) { return false }

    clearInterval(renderingIntervalId)

    set({ isPlaying: false })

    return false
  },

  // a slow rendering function (async - might call a third party LLM)
  runVideoSimulationLoop: async () => {
    const {
      isLoaded,
      isPlaying,
      clap,
      playbackSpeed,
      positionInMs,
      videoModelFPS,
      videoModelNumOfFrames,
      videoModelDurationInSec,
      videoElements,
      jwtToken,
    } = get()

    if (!isLoaded || !isPlaying) {
      set({ videoSimulationPending: false })
      return
    }

    set({
      videoSimulationPending: true,
      videoSimulationStartedAt: performance.now(),
    })

    const videosSortedByStartAt = getElementsSortedByStartAt(videoElements)

    // videos whose timestamp is behind the current cursor
    let toRecycle: HTMLVideoElement[] = []
    let toPlay: HTMLVideoElement[] = []
    let toPreload: HTMLVideoElement[] = []
    
    for (let i = 0; i < videosSortedByStartAt.length; i++) {
      const video = videosSortedByStartAt[i]
      
      const segmentStartAt = getSegmentStartAt(video)
      const segmentEndAt = getSegmentEndAt(video)

      // this segment has been spent, it should be discared
      if (segmentEndAt < positionInMs) {
        toRecycle.push(video)
      } else if (segmentStartAt < positionInMs) {
        toPlay.push(video)
        video.play()
        setZIndexDepthId(video, 10)
      } else {
        toPreload.push(video)
        video.pause()
        setZIndexDepthId(video, 0)
      }
    }

    const videoDurationInMs = videoModelDurationInSec * 1000

    // TODO julian: this is an approximation
    // to grab the max number of segments
    const maxBufferDurationInMs = positionInMs + (videoDurationInMs * 4)
    console.log(`DEBUG: `, {
      positionInMs,
      videoModelDurationInSec,
      videoDurationInMs,
      "(videoDurationInMs * 4)":  (videoDurationInMs * 4),
      maxBufferDurationInMs,
      segments: clap.segments
    })
    
    const prefilterSegmentsForPerformanceReasons: ClapSegment[] = clap.segments.filter(s =>
      s.startTimeInMs >= positionInMs &&
      s.startTimeInMs < maxBufferDurationInMs
    )

    console.log(`prefilterSegmentsForPerformanceReasons: `, prefilterSegmentsForPerformanceReasons)

    // this tells us how much time is left
    let remainingTimeInMs = Math.max(0, clap.meta.durationInMs - positionInMs)
    // to avoid interruptions, we should jump to the beginning of the project
    // as soo as we are start playing back the "last" video segment

    // now, we need to recycle spent videos,
    // by discarding their content and replacing it with fresh one
    //
    // yes: I know the code is complex and not intuitive - sorry about that

    const extraPositivePrompt: string[] = ["high quality", "crisp", "detailed"]

    let bufferAheadOfCurrentPositionInMs = positionInMs

    for (let i = 0; i < toRecycle.length; i++) {
      console.log(`got a spent video to recycle`)
      
      // we select the segments in the current shot

      const shotSegmentsToPreload: ClapSegment[] = prefilterSegmentsForPerformanceReasons.filter(s =>
        s.startTimeInMs >= bufferAheadOfCurrentPositionInMs &&
        s.startTimeInMs < (bufferAheadOfCurrentPositionInMs + videoDurationInMs)
      )
      
      bufferAheadOfCurrentPositionInMs += videoDurationInMs

      const prompt = getVideoPrompt(shotSegmentsToPreload, clap.entityIndex, extraPositivePrompt)

      console.log(`video prompt: ${prompt}`)
      // could also be the camera
      // after all, we don't necessarily have a shot,
      // this could also be a gaussian splat
      const shotData = shotSegmentsToPreload.find(s => s.category === "video")

      console.log(`shotData:`, shotData)

      if (!prompt || !shotData) { continue }
  
      const recycled = toRecycle[i]

      recycled.pause()

      setSegmentId(recycled, shotData.id)
      setSegmentStartAt(recycled, shotData.startTimeInMs)
      setSegmentEndAt(recycled, shotData.endTimeInMs)
      setZIndexDepthId(recycled, 0)

      // this is the best compromise for now in term of speed
      const width = 512
      const height = 288

      // this is our magic trick: we let the browser do the token-secured,
      // asynchronous and parallel video generation call for us
      //
      // one issue with this approach is that it hopes the video
      // will be downloaded in time, but it's not an exact science
      //
      // first, generation time varies between 4sec and 7sec,
      // then some people will get 300ms latency due to their ISP,
      // and finally the video itself is a 150~200 Kb payload)
      recycled.src = `/api/resolvers/video?t=${

      // to prevent funny people from using this as a free, open-bar video API
      // we have this system of token with a 24h expiration date
      // we might even make it tighter in the future
        jwtToken
      }&w=${
        width

      }&h=${
        height
      }&p=${
        // let's re-use the best ideas from the Latent Browser:
        // a text uri equals a latent resource
        encodeURIComponent(prompt)
      }`

      toPreload.push(recycled)
    }

    const videoSimulationEndedAt = performance.now()
    const videoSimulationDurationInMs = videoSimulationEndedAt - get().videoSimulationStartedAt 
    const videoSimulationDurationInSec = videoSimulationDurationInMs / 1000

    const videoSimulationVideoPlaybackFPS = videoModelFPS * playbackSpeed
    const videoSimulationRenderingTimeFPS = videoModelNumOfFrames / videoSimulationDurationInSec
    set({
      videoSimulationPending: false,
      videoSimulationEndedAt,
      videoSimulationDurationInMs,
      videoSimulationVideoPlaybackFPS,
      videoSimulationRenderingTimeFPS,
    })
  },


  // a slow rendering function (async - might call a third party LLM)
  runInterfaceSimulationLoop: async () => {
    const {
      isLoaded,
      isPlaying,
      clap,
    } = get()

    if (!isLoaded || !isPlaying) {
      set({ interfaceSimulationPending: false })
      return
    }

    set({
      interfaceSimulationPending: true,
      interfaceSimulationStartedAt: performance.now(),
    })
 
    try {
      if (get().isPlaying) {
        // console.log(`runSimulationLoop: rendering UI layer..`)

        // note: for now we only display one panel at a time,
        // later we can try to see if we should handle more
        // for nice gradient transition,
        const interfaceLayers = await resolveSegments(clap, "interface", 1)

        if (get().isPlaying) {
          set({
            interfaceLayers
          })

          // console.log(`runSimulationLoop: rendered UI layer`)
        }
      }
    } catch (err) {
      console.error(`runInterfaceSimulationLoop failed to render UI layer ${err}`)
    }

    const interfaceSimulationEndedAt = performance.now()
    const interfaceSimulationDurationInMs = interfaceSimulationEndedAt - get().interfaceSimulationStartedAt

    set({
      interfaceSimulationPending: false,
      interfaceSimulationEndedAt,
      interfaceSimulationDurationInMs,
    })
  },


  // a slow rendering function (async - might call a third party LLM)
  runEntitySimulationLoop: async () => {
    const {
      isLoaded,
      isPlaying,
      clap,
    } = get()


    if (!isLoaded || !isPlaying) {
      set({ entitySimulationPending: false })
      return
    }

    set({
      entitySimulationPending: true,
      entitySimulationStartedAt: performance.now(),
    })

    const entitySimulationEndedAt = performance.now()
    const entitySimulationDurationInMs = entitySimulationEndedAt - get().entitySimulationStartedAt 

    set({
      entitySimulationPending: false,
      entitySimulationEndedAt,
      entitySimulationDurationInMs,
    })
  },
  // a fast sync rendering function; whose sole role is to filter the component
  // list to put into the buffer the one that should be displayed
  runRenderingLoop: () => {
    const {
      isLoaded,
      isPlaying,
      renderingIntervalId,
      renderingIntervalDelayInMs,
      renderingLastRenderAt,
      positionInMs,
      videoSimulationPending,
      runVideoSimulationLoop,
      interfaceSimulationPending,
      runInterfaceSimulationLoop,
      entitySimulationPending,
      runEntitySimulationLoop,
    } = get()
    if (!isLoaded || !isPlaying) { return }

    // TODO julian: don't do this here, this is inneficient
    const videoElements = Array.from(
      document.querySelectorAll('.video-buffer')
    ) as HTMLVideoElement[]
 
    const newRenderingLastRenderAt = performance.now()
    const elapsedInMs = newRenderingLastRenderAt - renderingLastRenderAt

    // let's move inside the Clap file timeline
    const newPositionInMs = positionInMs + elapsedInMs

    clearInterval(renderingIntervalId)

    set({
      isPlaying: true,
      renderingLastRenderAt: newRenderingLastRenderAt,
      positionInMs: newPositionInMs,

      videoElements: videoElements,
      // TODO: use requestAnimationFrame somehow
      // https://developers.google.com/mediapipe/solutions/vision/image_segmenter/web_js
      renderingIntervalId: setTimeout(() => { get().runRenderingLoop() }, renderingIntervalDelayInMs)
    })

    // note that having this second set() also helps us to make sure previously values are properly stored
    // in the state when the simulation loop runs
    if (!videoSimulationPending) {
      set({ videoSimulationPromise: runVideoSimulationLoop() }) // <-- note: this is a fire-and-forget operation!
    }
    if (!interfaceSimulationPending) {
      set({ interfaceSimulationPromise: runInterfaceSimulationLoop() }) // <-- note: this is a fire-and-forget operation!
    }
    if (!entitySimulationPending) {
      set({ entitySimulationPromise: runEntitySimulationLoop() }) // <-- note: this is a fire-and-forget operation!
    }
  },

  jumpTo: (positionInMs: number) => {
    set({ positionInMs })
  },
  jumpToStart: () => {
    set({ positionInMs: 0 })
  },
}))