All files / web/src/app/vision-training/train/components DigitCapturePanel.tsx

0% Statements 0/676
0% Branches 0/1
0% Functions 0/1
0% Lines 0/676

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
'use client'

import { useCallback, useEffect, useRef, useState } from 'react'
import { css } from '../../../../../styled-system/css'
import { CameraCapture, type CameraSource } from '@/components/vision/CameraCapture'
import type { CalibrationGrid } from '@/types/vision'

interface CapturedColumn {
  id: string
  imageUrl: string
  timestamp: number
}

interface DigitCapturePanelProps {
  /** The digit to capture */
  digit: number
  /** Called when capture is successful with count of new images */
  onCaptureSuccess: (capturedCount: number) => void
  /** Number of physical abacus columns (default 4) */
  columnCount?: number
}

/** Minimum time between auto-captures in ms */
const AUTO_CAPTURE_INTERVAL = 600

/**
 * Capture panel for a specific digit.
 * Shows instructions, camera feed, and live preview of captured columns.
 *
 * Layout: Fixed header + flexible camera + fixed footer (no scrolling)
 */
export function DigitCapturePanel({
  digit,
  onCaptureSuccess,
  columnCount = 4,
}: DigitCapturePanelProps) {
  // Camera state
  const [isPhoneConnected, setIsPhoneConnected] = useState(false)
  const [cameraSource, setCameraSource] = useState<CameraSource>('local')
  const [calibration, setCalibration] = useState<CalibrationGrid | null>(null)
  const [markersVisible, setMarkersVisible] = useState(false)

  // Capture state
  const [isCapturing, setIsCapturing] = useState(false)
  const [captureStatus, setCaptureStatus] = useState<{
    success: boolean
    message: string
  } | null>(null)

  // Recently captured columns (for live preview)
  const [recentCaptures, setRecentCaptures] = useState<CapturedColumn[]>([])
  const [sessionCaptureCount, setSessionCaptureCount] = useState(0)

  // Auto-capture state
  const [autoCapture, setAutoCapture] = useState(false)
  const [autoCaptureCount, setAutoCaptureCount] = useState(0)
  const lastAutoCaptureTimeRef = useRef<number>(0)
  const isAutoCapturingRef = useRef(false)
  const markersVisibleRef = useRef(false)
  const captureTrainingDataRef = useRef<(() => Promise<void>) | null>(null)

  const captureElementRef = useRef<HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | null>(
    null
  )

  // Handle capture element from camera (may be video, image, or rectified canvas)
  const handleCapture = useCallback(
    (element: HTMLImageElement | HTMLVideoElement | HTMLCanvasElement) => {
      const prevElement = captureElementRef.current
      const prevType = prevElement?.constructor?.name ?? 'null'
      const newType = element?.constructor?.name ?? 'null'
      const dimensions =
        element instanceof HTMLCanvasElement
          ? `${element.width}x${element.height}`
          : element instanceof HTMLVideoElement
            ? `${element.videoWidth}x${element.videoHeight}`
            : element instanceof HTMLImageElement
              ? `${element.naturalWidth}x${element.naturalHeight}`
              : ''

      console.log(
        `[DigitCapturePanel] handleCapture: ${prevType} → ${newType}`,
        dimensions,
        element === prevElement ? '(same element)' : '(NEW element)'
      )
      captureElementRef.current = element
    },
    []
  )

  // Perform capture
  const captureTrainingData = useCallback(async () => {
    const element = captureElementRef.current
    console.log(
      '[DigitCapturePanel] captureTrainingData - element:',
      element?.constructor?.name,
      element instanceof HTMLCanvasElement ? `${element.width}x${element.height}` : ''
    )

    if (!element) {
      console.log('[DigitCapturePanel] captureTrainingData - NO ELEMENT!')
      setCaptureStatus({
        success: false,
        message: 'No camera frame available',
      })
      return
    }

    // Check if this is a rectified canvas (already perspective-corrected)
    const isRectifiedCanvas = element instanceof HTMLCanvasElement
    console.log('[DigitCapturePanel] isRectifiedCanvas:', isRectifiedCanvas)

    // Validate element readiness
    if (element instanceof HTMLVideoElement && element.readyState < 2) {
      setCaptureStatus({ success: false, message: 'Camera not ready' })
      return
    }
    if (element instanceof HTMLImageElement && (!element.complete || element.naturalWidth === 0)) {
      setCaptureStatus({ success: false, message: 'Camera frame not ready' })
      return
    }
    if (isRectifiedCanvas && (element.width === 0 || element.height === 0)) {
      setCaptureStatus({ success: false, message: 'Canvas not ready' })
      return
    }

    setIsCapturing(true)
    setCaptureStatus(null)

    try {
      // Import frame processor dynamically
      const { processImageFrame } = await import('@/lib/vision/frameProcessor')
      const { imageDataToBase64Png } = await import('@/lib/vision/trainingData')

      // Convert element to image for processing
      let imageElement: HTMLImageElement

      if (isRectifiedCanvas) {
        // Canvas is already rectified - convert to image
        imageElement = new Image()
        imageElement.src = element.toDataURL('image/jpeg')
        await new Promise((resolve, reject) => {
          imageElement.onload = resolve
          imageElement.onerror = reject
        })
      } else if (element instanceof HTMLVideoElement) {
        // Raw video - capture to canvas first
        const canvas = document.createElement('canvas')
        canvas.width = element.videoWidth
        canvas.height = element.videoHeight
        const ctx = canvas.getContext('2d')
        if (!ctx) throw new Error('Failed to create canvas context')
        ctx.drawImage(element, 0, 0)

        imageElement = new Image()
        imageElement.src = canvas.toDataURL('image/jpeg')
        await new Promise((resolve, reject) => {
          imageElement.onload = resolve
          imageElement.onerror = reject
        })
      } else {
        imageElement = element
      }

      // Slice image into columns
      // When it's a rectified canvas, pass null for calibration - image is already corrected,
      // just slice into equal columns. When it's raw video/image, use calibration for perspective.
      console.log(
        '[DigitCapturePanel] Calling processImageFrame, imageElement size:',
        imageElement.width || imageElement.naturalWidth,
        'x',
        imageElement.height || imageElement.naturalHeight
      )
      const columnImages = processImageFrame(
        imageElement,
        isRectifiedCanvas ? null : calibration,
        columnCount
      )
      console.log('[DigitCapturePanel] processImageFrame returned', columnImages.length, 'columns')
      if (columnImages.length === 0) {
        throw new Error('Failed to slice image into columns')
      }

      // Convert to base64
      const columns = columnImages.map((imgData: ImageData, index: number) => ({
        columnIndex: index,
        imageData: imageDataToBase64Png(imgData),
      }))

      // Send to collect API - we send the digit for each column
      const response = await fetch('/api/vision-training/collect', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          columns,
          correctAnswer: parseInt(String(digit).repeat(columnCount), 10) || digit,
          playerId: 'training-wizard',
          sessionId: `digit-capture-${digit}`,
        }),
      })

      const result = await response.json()

      if (result.success) {
        const savedCount = result.savedCount || columnCount
        setSessionCaptureCount((c) => c + savedCount)
        setCaptureStatus({
          success: true,
          message: `+${savedCount} saved`,
        })

        // Add to recent captures preview
        const newCaptures: CapturedColumn[] = columns.map(
          (col: { columnIndex: number; imageData: string }, idx: number) => ({
            id: `${Date.now()}-${idx}`,
            imageUrl: `data:image/png;base64,${col.imageData}`,
            timestamp: Date.now(),
          })
        )
        setRecentCaptures((prev) => [...newCaptures, ...prev].slice(0, 8))

        onCaptureSuccess(savedCount)
      } else {
        throw new Error(result.error || 'Failed to save')
      }
    } catch (error) {
      console.error('[DigitCapturePanel] Error:', error)
      setCaptureStatus({
        success: false,
        message: error instanceof Error ? error.message : 'Failed to capture',
      })
    } finally {
      setIsCapturing(false)
    }
  }, [digit, columnCount, calibration, onCaptureSuccess])

  // Keep refs in sync for auto-capture (avoids interval restarts)
  useEffect(() => {
    console.log('[DigitCapturePanel] markersVisible changed:', markersVisible, {
      captureElementType: captureElementRef.current?.constructor?.name ?? 'null',
    })
    markersVisibleRef.current = markersVisible
  }, [markersVisible])

  useEffect(() => {
    captureTrainingDataRef.current = captureTrainingData
  }, [captureTrainingData])

  // Auto-capture effect - only restarts when autoCapture toggles
  useEffect(() => {
    if (!autoCapture) {
      console.log('[AutoCapture] Disabled')
      isAutoCapturingRef.current = false
      return
    }

    console.log('[AutoCapture] Enabled, starting polling loop')

    // Set up polling interval
    const intervalId = setInterval(async () => {
      // Skip if markers not currently visible - use ref to avoid restarts
      if (!markersVisibleRef.current) {
        // Only log occasionally to avoid spam
        if (Date.now() % 2000 < 100) {
          console.log('[AutoCapture] Waiting for markers (currently not visible)...')
        }
        return
      }

      // Skip if already capturing
      if (isAutoCapturingRef.current) {
        return // Skip silently - we're already in progress
      }

      // Validate capture element exists and has dimensions
      const element = captureElementRef.current
      if (!element) {
        console.log('[AutoCapture] No capture element available yet (ref is null)')
        return
      }

      // Check element has valid dimensions
      if (element instanceof HTMLCanvasElement) {
        if (element.width === 0 || element.height === 0) {
          console.log('[AutoCapture] Canvas has zero dimensions, skipping')
          return
        }
      } else if (element instanceof HTMLVideoElement) {
        if (element.videoWidth === 0 || element.videoHeight === 0) {
          console.log('[AutoCapture] Video has zero dimensions, skipping')
          return
        }
      }

      // Throttle captures
      const now = Date.now()
      const timeSinceLastCapture = now - lastAutoCaptureTimeRef.current
      if (timeSinceLastCapture < AUTO_CAPTURE_INTERVAL) {
        return // Don't log this - too spammy
      }

      // Perform capture using ref
      const captureFn = captureTrainingDataRef.current
      if (!captureFn) {
        console.log('[AutoCapture] No capture function available')
        return
      }

      console.log('[AutoCapture] Starting capture...', {
        elementType: element.constructor.name,
        dimensions:
          element instanceof HTMLCanvasElement
            ? `${element.width}x${element.height}`
            : element instanceof HTMLVideoElement
              ? `${element.videoWidth}x${element.videoHeight}`
              : 'unknown',
      })
      isAutoCapturingRef.current = true
      lastAutoCaptureTimeRef.current = now

      try {
        await captureFn()
        setAutoCaptureCount((c) => {
          console.log('[AutoCapture] Capture complete, count:', c + 1)
          return c + 1
        })
      } catch (err) {
        console.error('[AutoCapture] Capture failed:', err)
      } finally {
        isAutoCapturingRef.current = false
      }
    }, 100) // Poll frequently, but actual captures are throttled

    return () => {
      console.log('[AutoCapture] Cleanup - clearing interval')
      clearInterval(intervalId)
      isAutoCapturingRef.current = false
    }
  }, [autoCapture]) // Only restart when autoCapture toggles

  // Reset auto-capture count when digit changes
  useEffect(() => {
    setAutoCaptureCount(0)
  }, [digit])

  // Toggle auto-capture mode
  const toggleAutoCapture = useCallback(() => {
    setAutoCapture((prev) => !prev)
    if (!autoCapture) {
      // Starting auto-capture - reset count
      setAutoCaptureCount(0)
    }
  }, [autoCapture])

  // Handle keyboard shortcut
  const handleKeyDown = useCallback(
    (e: KeyboardEvent) => {
      if ((e.key === ' ' || e.key === 'Enter') && !isCapturing) {
        e.preventDefault()
        captureTrainingData()
      }
    },
    [captureTrainingData, isCapturing]
  )

  const containerRef = useRef<HTMLDivElement>(null)
  const canCapture = cameraSource === 'local' || isPhoneConnected

  return (
    <div
      ref={containerRef}
      data-component="digit-capture-panel"
      data-digit={digit}
      className={css({
        display: 'flex',
        flexDirection: 'column',
        height: '100%',
        overflow: 'hidden', // No scrolling in capture panel
      })}
      // biome-ignore lint/a11y/noNoninteractiveTabindex: needed for programmatic focus to receive keyboard events
      tabIndex={0}
      onKeyDown={(e) => handleKeyDown(e.nativeEvent)}
    >
      {/* ═══════════════════════════════════════════════════════════════════
          COMPACT INSTRUCTION HEADER
          ═══════════════════════════════════════════════════════════════════ */}
      <div
        data-element="capture-instruction-header"
        className={css({
          flexShrink: 0,
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          gap: 3,
          py: 2,
          px: 3,
          bg: 'blue.900/30',
          borderBottom: '1px solid',
          borderColor: 'blue.800/50',
        })}
      >
        <span
          data-element="instruction-text"
          className={css({ fontSize: 'sm', color: 'gray.400' })}
        >
          Set abacus to all
        </span>
        <span
          data-element="target-digit-badge"
          data-digit={digit}
          className={css({
            fontSize: '2xl',
            fontWeight: 'bold',
            fontFamily: 'mono',
            color: 'blue.300',
            px: 3,
            py: 1,
            bg: 'blue.900/50',
            borderRadius: 'lg',
            border: '2px solid',
            borderColor: 'blue.600',
          })}
        >
          {digit}
        </span>
        {sessionCaptureCount > 0 && (
          <span
            data-element="session-capture-count"
            className={css({ fontSize: 'sm', color: 'green.400' })}
          >
            +{sessionCaptureCount}
          </span>
        )}
        {calibration && (
          <span
            data-element="calibration-indicator"
            className={css({ fontSize: 'xs', color: 'green.500' })}
            title="Markers detected"
          >

          </span>
        )}
      </div>

      {/* ═══════════════════════════════════════════════════════════════════
          CAMERA FEED - Takes all available space, contains footer overlay
          ═══════════════════════════════════════════════════════════════════ */}
      <div
        data-element="camera-feed-container"
        className={css({
          position: 'relative',
          flex: 1,
          minHeight: 0,
          display: 'flex',
          flexDirection: 'column',
        })}
      >
        <CameraCapture
          initialSource="local"
          onCapture={handleCapture}
          onSourceChange={setCameraSource}
          onPhoneConnected={setIsPhoneConnected}
          compact
          enableMarkerDetection
          columnCount={columnCount}
          onCalibrationChange={setCalibration}
          onMarkersVisible={setMarkersVisible}
          showRectifiedView
        />

        {/* Footer - absolutely positioned so it doesn't affect width */}
        {canCapture && (
          <div
            data-element="capture-footer"
            className={css({
              position: 'absolute',
              bottom: 0,
              left: 0,
              right: 0,
              zIndex: 10,
              borderTop: '1px solid',
              borderColor: 'gray.800',
              bg: 'gray.900/95',
              backdropFilter: 'blur(4px)',
            })}
          >
            {/* Recent captures strip */}
            {recentCaptures.length > 0 && (
              <div
                data-element="recent-captures-strip"
                className={css({
                  display: 'flex',
                  alignItems: 'center',
                  gap: 2,
                  px: 3,
                  py: 2,
                  borderBottom: '1px solid',
                  borderColor: 'gray.800',
                })}
              >
                <span
                  data-element="recent-captures-label"
                  className={css({
                    fontSize: 'xs',
                    color: 'gray.500',
                    flexShrink: 0,
                  })}
                >
                  Just captured:
                </span>
                <div
                  data-element="recent-captures-thumbnails"
                  className={css({
                    display: 'flex',
                    gap: 1,
                    overflow: 'hidden',
                  })}
                >
                  {recentCaptures.map((capture) => (
                    <div
                      key={capture.id}
                      data-element="capture-thumbnail"
                      data-capture-id={capture.id}
                      className={css({
                        flexShrink: 0,
                        width: '32px',
                        height: '32px',
                        borderRadius: 'sm',
                        overflow: 'hidden',
                        border: '1px solid',
                        borderColor: 'green.600',
                      })}
                    >
                      <img
                        src={capture.imageUrl}
                        alt={`Captured ${digit}`}
                        className={css({
                          width: '100%',
                          height: '100%',
                          objectFit: 'cover',
                        })}
                      />
                    </div>
                  ))}
                </div>
              </div>
            )}

            {/* Capture button row */}
            <div
              data-element="capture-button-row"
              className={css({
                p: 3,
                display: 'flex',
                gap: 2,
                alignItems: 'center',
              })}
            >
              {/* Manual capture button */}
              <button
                type="button"
                data-action="capture"
                data-capturing={isCapturing}
                onClick={captureTrainingData}
                disabled={isCapturing || autoCapture}
                className={css({
                  flex: 1,
                  py: 3,
                  bg: autoCapture ? 'gray.700' : isCapturing ? 'gray.700' : 'green.600',
                  color: autoCapture ? 'gray.500' : 'white',
                  borderRadius: 'lg',
                  borderTopRightRadius: 0,
                  borderBottomRightRadius: 0,
                  border: 'none',
                  cursor: isCapturing || autoCapture ? 'not-allowed' : 'pointer',
                  fontWeight: 'bold',
                  fontSize: 'md',
                  transition: 'all 0.15s ease',
                  _hover: isCapturing || autoCapture ? {} : { bg: 'green.500' },
                })}
              >
                {isCapturing ? '⏳...' : `📸 Capture`}
              </button>

              {/* Auto-capture toggle button */}
              <button
                type="button"
                data-action="toggle-auto-capture"
                data-active={autoCapture}
                data-paused={autoCapture && !markersVisible}
                onClick={toggleAutoCapture}
                className={css({
                  py: 3,
                  px: 4,
                  bg: autoCapture ? (markersVisible ? 'blue.600' : 'yellow.700') : 'gray.700',
                  color: 'white',
                  borderRadius: 'lg',
                  borderTopLeftRadius: 0,
                  borderBottomLeftRadius: 0,
                  border: 'none',
                  cursor: 'pointer',
                  fontWeight: 'bold',
                  fontSize: 'md',
                  transition: 'all 0.15s ease',
                  animation:
                    autoCapture && markersVisible ? 'pulse 1.5s ease-in-out infinite' : 'none',
                  _hover: {
                    bg: autoCapture ? (markersVisible ? 'blue.500' : 'yellow.600') : 'gray.600',
                  },
                })}
                title={
                  autoCapture
                    ? markersVisible
                      ? 'Click to stop auto-capture'
                      : 'Waiting for markers...'
                    : 'Enable auto-capture'
                }
              >
                {autoCapture ? (
                  markersVisible ? (
                    <>🔄 {autoCaptureCount > 0 ? autoCaptureCount : 'Auto'}</>
                  ) : (
                    <>⏸️ No markers</>
                  )
                ) : (
                  <>🔁 Auto</>
                )}
              </button>
            </div>

            {/* Status indicator */}
            {(captureStatus || (autoCapture && markersVisible)) && (
              <div
                data-element="capture-status-bar"
                className={css({
                  px: 3,
                  pb: 2,
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  gap: 2,
                })}
              >
                {autoCapture && markersVisible && (
                  <span
                    data-element="auto-capture-indicator"
                    className={css({
                      fontSize: 'sm',
                      color: 'blue.400',
                    })}
                  >
                    Auto-capturing every {AUTO_CAPTURE_INTERVAL}ms
                  </span>
                )}
                {captureStatus && !autoCapture && (
                  <span
                    data-element="capture-status"
                    data-success={captureStatus.success}
                    className={css({
                      fontSize: 'sm',
                      color: captureStatus.success ? 'green.400' : 'red.400',
                    })}
                  >
                    {captureStatus.success ? '✓' : '✗'} {captureStatus.message}
                  </span>
                )}
              </div>
            )}
          </div>
        )}
      </div>
    </div>
  )
}

export default DigitCapturePanel