All files / web/src/arcade-games/know-your-world/components DevCropTool.tsx

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
'use client'

import { useState, useCallback, useEffect } from 'react'
import { css } from '@styled/css'
import { setRuntimeCrop, setCropModeActive } from '../customCrops'

/**
 * Dev-only tool for drawing bounding boxes to get crop coordinates
 * Activated with Ctrl+Shift+B (or Cmd+Shift+B on Mac)
 *
 * Usage:
 * 1. Press Ctrl+Shift+B to activate crop mode
 * 2. Click and drag to draw a bounding box
 * 3. A JSON file is automatically downloaded
 * 4. Save it to: apps/web/src/arcade-games/know-your-world/pending-crop.json
 * 5. Run: npm run apply-crop
 * 6. Press Escape or Ctrl+Shift+B again to deactivate
 */

interface DevCropToolProps {
  svgRef: React.RefObject<SVGSVGElement>
  containerRef: React.RefObject<HTMLDivElement>
  viewBox: string
  mapId: string
  continentId: string
}

interface CropBox {
  startX: number
  startY: number
  endX: number
  endY: number
}

export function DevCropTool({
  svgRef,
  containerRef,
  viewBox,
  mapId,
  continentId,
}: DevCropToolProps) {
  const [isActive, setIsActive] = useState(false)
  const [isDrawing, setIsDrawing] = useState(false)
  const [cropBox, setCropBox] = useState<CropBox | null>(null)
  const [finalBox, setFinalBox] = useState<CropBox | null>(null)
  const [saveStatus, setSaveStatus] = useState<'idle' | 'saving' | 'saved' | 'error'>('idle')

  // Reset crop for current map/continent
  const resetCrop = useCallback(() => {
    console.log(`[DevCropTool] Resetting crop for ${mapId}/${continentId}`)
    setSaveStatus('saving')
    fetch(`/api/dev/save-crop?mapId=${mapId}&continentId=${continentId}`, {
      method: 'DELETE',
    })
      .then((res) => res.json())
      .then((data) => {
        if (data.success) {
          console.log(`✅ Crop reset!`)
          // Update runtime crop immediately for live update
          setRuntimeCrop(mapId, continentId, null)
          setSaveStatus('saved')
          setFinalBox(null)
          setTimeout(() => setSaveStatus('idle'), 2000)
        } else {
          console.error('❌ Failed to reset crop:', data.error)
          setSaveStatus('error')
        }
      })
      .catch((err) => {
        console.error('❌ Failed to reset crop:', err)
        setSaveStatus('error')
      })
  }, [mapId, continentId])

  // Parse viewBox
  const viewBoxParts = viewBox.split(' ').map(Number)
  const viewBoxX = viewBoxParts[0] || 0
  const viewBoxY = viewBoxParts[1] || 0
  const viewBoxWidth = viewBoxParts[2] || 1000
  const viewBoxHeight = viewBoxParts[3] || 1000

  // Convert screen coordinates to SVG coordinates
  // IMPORTANT: Must account for SVG letterboxing due to preserveAspectRatio="xMidYMid meet"
  const screenToSvg = useCallback(
    (screenX: number, screenY: number): { x: number; y: number } | null => {
      const svg = svgRef.current
      const container = containerRef.current
      if (!svg || !container) return null

      const containerRect = container.getBoundingClientRect()
      const svgRect = svg.getBoundingClientRect()

      // With preserveAspectRatio="xMidYMid meet", the SVG uses uniform scaling
      // and centers the content, creating letterboxing
      const scaleX = svgRect.width / viewBoxWidth
      const scaleY = svgRect.height / viewBoxHeight
      const actualScale = Math.min(scaleX, scaleY)

      // Calculate letterbox offsets (content is centered)
      const renderedWidth = viewBoxWidth * actualScale
      const renderedHeight = viewBoxHeight * actualScale
      const offsetX = (svgRect.width - renderedWidth) / 2
      const offsetY = (svgRect.height - renderedHeight) / 2

      // Position relative to SVG element
      const relX = screenX - svgRect.left
      const relY = screenY - svgRect.top

      // Convert to SVG coordinates, accounting for letterbox offset
      return {
        x: viewBoxX + (relX - offsetX) / actualScale,
        y: viewBoxY + (relY - offsetY) / actualScale,
      }
    },
    [svgRef, containerRef, viewBoxX, viewBoxY, viewBoxWidth, viewBoxHeight]
  )

  // Convert SVG coordinates to screen coordinates (for display)
  // IMPORTANT: Must account for SVG letterboxing due to preserveAspectRatio="xMidYMid meet"
  const svgToScreen = useCallback(
    (svgX: number, svgY: number): { x: number; y: number } | null => {
      const svg = svgRef.current
      const container = containerRef.current
      if (!svg || !container) return null

      const containerRect = container.getBoundingClientRect()
      const svgRect = svg.getBoundingClientRect()

      // With preserveAspectRatio="xMidYMid meet", the SVG uses uniform scaling
      const scaleX = svgRect.width / viewBoxWidth
      const scaleY = svgRect.height / viewBoxHeight
      const actualScale = Math.min(scaleX, scaleY)

      // Calculate letterbox offsets (content is centered)
      const renderedWidth = viewBoxWidth * actualScale
      const renderedHeight = viewBoxHeight * actualScale
      const offsetX = (svgRect.width - renderedWidth) / 2
      const offsetY = (svgRect.height - renderedHeight) / 2

      // Convert SVG coordinates to screen, accounting for letterbox offset
      return {
        x: (svgX - viewBoxX) * actualScale + offsetX + (svgRect.left - containerRect.left),
        y: (svgY - viewBoxY) * actualScale + offsetY + (svgRect.top - containerRect.top),
      }
    },
    [svgRef, containerRef, viewBoxX, viewBoxY, viewBoxWidth, viewBoxHeight]
  )

  // Handle keyboard shortcut
  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      // Ctrl+Shift+B (or Cmd+Shift+B on Mac)
      if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key.toLowerCase() === 'b') {
        e.preventDefault()
        const newActive = !isActive
        setIsActive(newActive)
        setCropModeActive(newActive)
        if (isActive) {
          setCropBox(null)
          setFinalBox(null)
        }
      }
      // R to reset crop (when in crop mode)
      if (e.key.toLowerCase() === 'r') {
        console.log('[DevCropTool] R pressed, isActive:', isActive, 'isDrawing:', isDrawing)
        if (isActive && !isDrawing) {
          e.preventDefault()
          resetCrop()
        }
      }
      // Escape to deactivate
      if (e.key === 'Escape' && isActive) {
        setIsActive(false)
        setCropModeActive(false)
        setCropBox(null)
        setFinalBox(null)
        setIsDrawing(false)
      }
    }

    window.addEventListener('keydown', handleKeyDown)
    return () => window.removeEventListener('keydown', handleKeyDown)
  }, [isActive, isDrawing, resetCrop])

  // Handle mouse events for drawing
  const handleMouseDown = useCallback(
    (e: React.MouseEvent) => {
      if (!isActive) return

      const svgCoords = screenToSvg(e.clientX, e.clientY)
      if (!svgCoords) return

      setIsDrawing(true)
      setFinalBox(null)
      setCropBox({
        startX: svgCoords.x,
        startY: svgCoords.y,
        endX: svgCoords.x,
        endY: svgCoords.y,
      })
    },
    [isActive, screenToSvg]
  )

  const handleMouseMove = useCallback(
    (e: React.MouseEvent) => {
      if (!isActive || !isDrawing || !cropBox) return

      const svgCoords = screenToSvg(e.clientX, e.clientY)
      if (!svgCoords) return

      setCropBox((prev) => (prev ? { ...prev, endX: svgCoords.x, endY: svgCoords.y } : null))
    },
    [isActive, isDrawing, cropBox, screenToSvg]
  )

  const handleMouseUp = useCallback(() => {
    if (!isActive || !isDrawing || !cropBox) return

    setIsDrawing(false)

    // Calculate final box (normalize so min is start)
    const minX = Math.min(cropBox.startX, cropBox.endX)
    const maxX = Math.max(cropBox.startX, cropBox.endX)
    const minY = Math.min(cropBox.startY, cropBox.endY)
    const maxY = Math.max(cropBox.startY, cropBox.endY)

    const finalCropBox = {
      startX: minX,
      startY: minY,
      endX: maxX,
      endY: maxY,
    }

    setFinalBox(finalCropBox)
    setCropBox(null)

    // Calculate viewBox string
    const width = maxX - minX
    const height = maxY - minY
    const viewBoxStr = `${minX.toFixed(2)} ${minY.toFixed(2)} ${width.toFixed(2)} ${height.toFixed(2)}`

    console.log(`[DevCropTool] Saving crop for ${mapId}/${continentId}: ${viewBoxStr}`)
    setSaveStatus('saving')

    // POST to API to save directly to customCrops.ts
    fetch('/api/dev/save-crop', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        mapId,
        continentId,
        viewBox: viewBoxStr,
      }),
    })
      .then((res) => res.json())
      .then((data) => {
        if (data.success) {
          console.log(`✅ Crop saved!`)
          console.log('Updated crops:', data.crops)
          // Update runtime crop immediately for live update
          setRuntimeCrop(mapId, continentId, viewBoxStr)
          setSaveStatus('saved')
          setTimeout(() => setSaveStatus('idle'), 2000)
        } else {
          console.error('❌ Failed to save crop:', data.error)
          setSaveStatus('error')
        }
      })
      .catch((err) => {
        console.error('❌ Failed to save crop:', err)
        setSaveStatus('error')
      })
  }, [isActive, isDrawing, cropBox, mapId, continentId])

  // Don't render in production
  if (process.env.NODE_ENV !== 'development') {
    return null
  }

  // Calculate screen coordinates for display
  const currentBox = cropBox || finalBox
  let screenBox: {
    left: number
    top: number
    width: number
    height: number
  } | null = null

  if (currentBox) {
    const minX = Math.min(currentBox.startX, currentBox.endX)
    const maxX = Math.max(currentBox.startX, currentBox.endX)
    const minY = Math.min(currentBox.startY, currentBox.endY)
    const maxY = Math.max(currentBox.startY, currentBox.endY)

    const topLeft = svgToScreen(minX, minY)
    const bottomRight = svgToScreen(maxX, maxY)

    if (topLeft && bottomRight) {
      screenBox = {
        left: topLeft.x,
        top: topLeft.y,
        width: bottomRight.x - topLeft.x,
        height: bottomRight.y - topLeft.y,
      }
    }
  }

  return (
    <>
      {/* Overlay for capturing mouse events when active */}
      {isActive && (
        <div
          data-element="crop-tool-overlay"
          onMouseDown={handleMouseDown}
          onMouseMove={handleMouseMove}
          onMouseUp={handleMouseUp}
          onMouseLeave={handleMouseUp}
          className={css({
            position: 'absolute',
            top: 0,
            left: 0,
            right: 0,
            bottom: 0,
            zIndex: 1000,
            cursor: 'crosshair',
          })}
        />
      )}

      {/* Draw the bounding box */}
      {screenBox && (
        <div
          data-element="crop-box"
          className={css({
            position: 'absolute',
            border: '2px dashed',
            borderColor: finalBox ? 'green.500' : 'red.500',
            bg: finalBox ? 'green.500/20' : 'red.500/20',
            pointerEvents: 'none',
            zIndex: 1001,
          })}
          style={{
            left: `${screenBox.left}px`,
            top: `${screenBox.top}px`,
            width: `${screenBox.width}px`,
            height: `${screenBox.height}px`,
          }}
        >
          {/* Show dimensions */}
          {finalBox && (
            <div
              className={css({
                position: 'absolute',
                bottom: '-24px',
                left: '50%',
                transform: 'translateX(-50%)',
                bg: 'green.700',
                color: 'white',
                px: 2,
                py: 1,
                rounded: 'md',
                fontSize: 'xs',
                fontFamily: 'mono',
                whiteSpace: 'nowrap',
              })}
            >
              {(finalBox.endX - finalBox.startX).toFixed(0)} ×{' '}
              {(finalBox.endY - finalBox.startY).toFixed(0)}
            </div>
          )}
        </div>
      )}

      {/* Status indicator */}
      {isActive && (
        <div
          data-element="crop-tool-status"
          className={css({
            position: 'absolute',
            top: 2,
            left: 2,
            bg: 'red.600',
            color: 'white',
            px: 3,
            py: 2,
            rounded: 'md',
            fontSize: 'sm',
            fontWeight: 'bold',
            zIndex: 1002,
            display: 'flex',
            flexDirection: 'column',
            gap: 1,
          })}
        >
          <div>
            🎯 CROP MODE: {mapId}/{continentId}
          </div>
          <div className={css({ fontSize: 'xs', fontWeight: 'normal' })}>
            Draw a box • R to reset • ESC to exit
          </div>
          {saveStatus === 'saving' && (
            <div
              className={css({
                fontSize: 'xs',
                fontWeight: 'normal',
                color: 'yellow.200',
              })}
            >
              ⏳ Saving...
            </div>
          )}
          {saveStatus === 'saved' && (
            <div
              className={css({
                fontSize: 'xs',
                fontWeight: 'normal',
                color: 'green.200',
              })}
            >
              ✅ Saved! Map updated live.
            </div>
          )}
          {saveStatus === 'error' && (
            <div
              className={css({
                fontSize: 'xs',
                fontWeight: 'normal',
                color: 'red.200',
              })}
            >
              ❌ Error saving. Check console.
            </div>
          )}
        </div>
      )}
    </>
  )
}