All files / web/src/components/tutorial PracticeProblemPlayer.tsx

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

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

import { AbacusReact } from '@soroban/abacus-react'
import { useCallback, useEffect, useState } from 'react'
import { css } from '../../../styled-system/css'
import { hstack, vstack } from '../../../styled-system/patterns'
import type { PracticeStep } from '../../types/tutorial'
import { type GeneratedProblem, generateProblems } from '../../utils/problemGenerator'

interface PracticeProblemPlayerProps {
  practiceStep: PracticeStep
  onComplete?: (results: PracticeResults) => void
  onProblemComplete?: (problemIndex: number, correct: boolean, timeSpent: number) => void
  className?: string
}

export interface PracticeResults {
  totalProblems: number
  correctAnswers: number
  totalTime: number
  averageTime: number
  problemResults: Array<{
    problem: GeneratedProblem
    userAnswer: number
    correct: boolean
    timeSpent: number
  }>
}

export function PracticeProblemPlayer({
  practiceStep,
  onComplete,
  onProblemComplete,
  className,
}: PracticeProblemPlayerProps) {
  const [problems, setProblems] = useState<GeneratedProblem[]>([])
  const [currentProblemIndex, setCurrentProblemIndex] = useState(0)
  const [currentSequenceStep, setCurrentSequenceStep] = useState(0) // Which number in sequence we're adding
  const [userAnswer, setUserAnswer] = useState(0)
  const [isCorrect, setIsCorrect] = useState<boolean | null>(null)
  const [results, setResults] = useState<PracticeResults['problemResults']>([])
  const [startTime, _setStartTime] = useState<number>(Date.now())
  const [problemStartTime, setProblemStartTime] = useState<number>(Date.now())
  const [showExplanation, setShowExplanation] = useState(false)
  const [isGenerating, setIsGenerating] = useState(true)
  const [expectedValue, setExpectedValue] = useState(0) // Expected value at current step

  // Generate problems on mount
  useEffect(() => {
    const generatedProblems = generateProblems(practiceStep)
    setProblems(generatedProblems)
    setIsGenerating(false)
    setProblemStartTime(Date.now())
  }, [practiceStep])

  const currentProblem = problems[currentProblemIndex]

  // Calculate expected value at current step
  const calculateExpectedValue = useCallback((problem: GeneratedProblem, step: number): number => {
    return problem.terms.slice(0, step + 1).reduce((sum, term) => sum + term, 0)
  }, [])

  // Update expected value when problem or step changes
  useEffect(() => {
    if (currentProblem) {
      setExpectedValue(calculateExpectedValue(currentProblem, currentSequenceStep))
    }
  }, [currentProblem, currentSequenceStep, calculateExpectedValue])

  // Complete the practice session
  const completePractice = useCallback(() => {
    const totalTime = Date.now() - startTime
    const correctAnswers = results.filter((r) => r.correct).length

    const practiceResults: PracticeResults = {
      totalProblems: problems.length,
      correctAnswers,
      totalTime,
      averageTime: totalTime / problems.length,
      problemResults: results,
    }

    onComplete?.(practiceResults)
  }, [results, problems.length, startTime, onComplete])

  // Move to next problem
  const nextProblem = useCallback(() => {
    if (currentProblemIndex < problems.length - 1) {
      setCurrentProblemIndex((prev) => prev + 1)
      setCurrentSequenceStep(0)
      setUserAnswer(0)
      setIsCorrect(null)
      setShowExplanation(false)
      setProblemStartTime(Date.now())
    }
  }, [currentProblemIndex, problems.length])

  // Skip current problem (mark as incorrect)
  const skipProblem = useCallback(() => {
    if (currentProblem) {
      const timeSpent = Date.now() - problemStartTime
      const problemResult = {
        problem: currentProblem,
        userAnswer: userAnswer,
        correct: false,
        timeSpent,
      }

      setResults((prev) => [...prev, problemResult])
      onProblemComplete?.(currentProblemIndex, false, timeSpent)
    }

    if (currentProblemIndex < problems.length - 1) {
      nextProblem()
    } else {
      completePractice()
    }
  }, [
    currentProblem,
    currentProblemIndex,
    problems.length,
    userAnswer,
    problemStartTime,
    onProblemComplete,
    nextProblem,
    completePractice,
  ])

  // Reset to start of current problem
  const resetProblem = useCallback(() => {
    setCurrentSequenceStep(0)
    setUserAnswer(0)
    setIsCorrect(null)
    setProblemStartTime(Date.now())
  }, [])

  // Check answer when user changes abacus value
  const handleValueChange = useCallback(
    (newValue: number | bigint) => {
      setUserAnswer(Number(newValue))

      if (currentProblem && newValue === expectedValue) {
        setIsCorrect(true)

        // Check if this was the final step
        if (currentSequenceStep === currentProblem.terms.length - 1) {
          // Problem completed
          const timeSpent = Date.now() - problemStartTime

          const problemResult = {
            problem: currentProblem,
            userAnswer: newValue,
            correct: true,
            timeSpent,
          }

          setResults((prev) => [...prev, problemResult])
          onProblemComplete?.(currentProblemIndex, true, timeSpent)

          // Auto-advance to next problem after delay
          setTimeout(() => {
            if (currentProblemIndex < problems.length - 1) {
              nextProblem()
            } else {
              completePractice()
            }
          }, 1500)
        } else {
          // Move to next step in sequence after short delay
          setTimeout(() => {
            setCurrentSequenceStep((prev) => prev + 1)
            setIsCorrect(null)
          }, 800)
        }
      } else if (currentProblem && newValue !== expectedValue && newValue !== 0) {
        // User has entered a value but it's wrong
        setIsCorrect(false)
      } else {
        setIsCorrect(null)
      }
    },
    [
      currentProblem,
      expectedValue,
      currentSequenceStep,
      currentProblemIndex,
      problems.length,
      problemStartTime,
      onProblemComplete,
      completePractice,
      nextProblem,
    ]
  )

  // Toggle explanation
  const toggleExplanation = useCallback(() => {
    setShowExplanation((prev) => !prev)
  }, [])

  if (isGenerating) {
    return (
      <div
        className={css({
          display: 'flex',
          justifyContent: 'center',
          alignItems: 'center',
          height: '400px',
          textAlign: 'center',
        })}
      >
        <div>
          <div className={css({ fontSize: 'lg', fontWeight: 'medium', mb: 2 })}>
            Generating practice problems...
          </div>
          <div className={css({ fontSize: 'sm', color: 'gray.600' })}>
            Creating {practiceStep.problemCount} problems based on your skill settings
          </div>
        </div>
      </div>
    )
  }

  if (problems.length === 0) {
    return (
      <div
        className={css({
          p: 6,
          bg: 'red.50',
          border: '1px solid',
          borderColor: 'red.200',
          borderRadius: 'lg',
          textAlign: 'center',
        })}
      >
        <h3
          className={css({
            fontSize: 'lg',
            fontWeight: 'bold',
            color: 'red.800',
            mb: 2,
          })}
        >
          No Problems Generated
        </h3>
        <p className={css({ color: 'red.700', mb: 4 })}>
          Unable to generate problems with the current skill and constraint settings.
        </p>
        <p className={css({ fontSize: 'sm', color: 'red.600' })}>
          Try adjusting the skill requirements or number constraints.
        </p>
      </div>
    )
  }

  if (!currentProblem) {
    return <div>No current problem available</div>
  }

  const progress = ((currentProblemIndex + 1) / problems.length) * 100

  return (
    <div
      className={`${css({
        display: 'flex',
        flexDirection: 'column',
        height: '100%',
        minHeight: '600px',
      })} ${className || ''}`}
    >
      {/* Header */}
      <div
        className={css({
          borderBottom: '1px solid',
          borderColor: 'gray.200',
          p: 4,
          bg: 'white',
        })}
      >
        <div
          className={hstack({
            justifyContent: 'space-between',
            alignItems: 'center',
          })}
        >
          <div>
            <h2 className={css({ fontSize: 'xl', fontWeight: 'bold' })}>{practiceStep.title}</h2>
            <p className={css({ fontSize: 'sm', color: 'gray.600' })}>
              Problem {currentProblemIndex + 1} of {problems.length}
            </p>
          </div>

          <div className={hstack({ gap: 2 })}>
            <button
              onClick={toggleExplanation}
              className={css({
                px: 3,
                py: 1,
                fontSize: 'sm',
                border: '1px solid',
                borderColor: 'blue.300',
                borderRadius: 'md',
                bg: showExplanation ? 'blue.100' : 'white',
                color: 'blue.700',
                cursor: 'pointer',
                _hover: { bg: 'blue.50' },
              })}
            >
              Hint
            </button>

            <button
              onClick={resetProblem}
              className={css({
                px: 3,
                py: 1,
                fontSize: 'sm',
                border: '1px solid',
                borderColor: 'orange.300',
                borderRadius: 'md',
                bg: 'white',
                color: 'orange.700',
                cursor: 'pointer',
                _hover: { bg: 'orange.50' },
              })}
            >
              Reset
            </button>

            <button
              onClick={skipProblem}
              className={css({
                px: 3,
                py: 1,
                fontSize: 'sm',
                border: '1px solid',
                borderColor: 'gray.300',
                borderRadius: 'md',
                bg: 'white',
                cursor: 'pointer',
                _hover: { bg: 'gray.50' },
              })}
            >
              Skip
            </button>
          </div>
        </div>

        {/* Progress bar */}
        <div className={css({ mt: 3, bg: 'gray.200', borderRadius: 'full', h: 2 })}>
          <div
            className={css({
              bg: 'green.500',
              h: 'full',
              borderRadius: 'full',
              transition: 'width 0.3s ease',
            })}
            style={{ width: `${progress}%` }}
          />
        </div>
      </div>

      {/* Main content */}
      <div className={css({ flex: 1, p: 6 })}>
        <div className={vstack({ gap: 6, alignItems: 'center' })}>
          {/* Problem statement */}
          <div className={css({ textAlign: 'center', maxW: '600px' })}>
            {/* Problem display - vertical stack */}
            <div className={css({ mb: 4 })}>
              <div
                className={css({
                  display: 'inline-block',
                  textAlign: 'right',
                  fontSize: '2xl',
                  fontWeight: 'bold',
                  fontFamily: 'mono',
                  bg: 'gray.50',
                  p: 4,
                  border: '1px solid',
                  borderColor: 'gray.200',
                  borderRadius: 'md',
                })}
              >
                {currentProblem.terms.map((term, index) => (
                  <div
                    key={index}
                    className={css({
                      py: 1,
                      color: index <= currentSequenceStep ? 'blue.800' : 'gray.400',
                    })}
                  >
                    {term}
                  </div>
                ))}
                <div
                  className={css({
                    borderTop: '2px solid',
                    borderColor: 'gray.800',
                    mt: 2,
                    pt: 2,
                    color: 'green.800',
                  })}
                >
                  {currentProblem.answer}
                </div>
              </div>
            </div>

            {/* Current step */}
            <div className={css({ mb: 3 })}>
              <h2 className={css({ fontSize: '2xl', fontWeight: 'bold', mb: 2 })}>
                {currentSequenceStep === 0
                  ? `Start with 0, then add ${currentProblem.terms[0]}`
                  : `Now add ${currentProblem.terms[currentSequenceStep]}`}
              </h2>
              <p className={css({ fontSize: 'lg', color: 'gray.700' })}>
                Step {currentSequenceStep + 1} of {currentProblem.terms.length}
              </p>
            </div>

            {/* Progress indicator */}
            <div className={css({ mb: 3 })}>
              <div
                className={hstack({
                  gap: 2,
                  justifyContent: 'center',
                  mb: 2,
                  alignItems: 'center',
                })}
              >
                <span className={css({ fontSize: 'sm', color: 'gray.600' })}>Adding:</span>
                {currentProblem.terms.map((term, index) => (
                  <div
                    key={index}
                    className={css({
                      px: 2,
                      py: 1,
                      rounded: 'md',
                      fontSize: 'md',
                      fontWeight: 'bold',
                      fontFamily: 'mono',
                      bg:
                        index < currentSequenceStep
                          ? 'green.100'
                          : index === currentSequenceStep
                            ? 'blue.100'
                            : 'gray.100',
                      color:
                        index < currentSequenceStep
                          ? 'green.800'
                          : index === currentSequenceStep
                            ? 'blue.800'
                            : 'gray.600',
                      border: '1px solid',
                      borderColor: index === currentSequenceStep ? 'blue.300' : 'transparent',
                    })}
                  >
                    {term}
                  </div>
                ))}
              </div>
              <p className={css({ fontSize: 'sm', color: 'gray.600' })}>
                Target for this step: {expectedValue}
              </p>
            </div>

            {/* Difficulty indicator */}
            <div className={css({ mt: 2 })}>
              <span
                className={css({
                  px: 2,
                  py: 1,
                  fontSize: 'xs',
                  fontWeight: 'medium',
                  borderRadius: 'md',
                  bg:
                    currentProblem.difficulty === 'easy'
                      ? 'green.100'
                      : currentProblem.difficulty === 'medium'
                        ? 'yellow.100'
                        : 'red.100',
                  color:
                    currentProblem.difficulty === 'easy'
                      ? 'green.800'
                      : currentProblem.difficulty === 'medium'
                        ? 'yellow.800'
                        : 'red.800',
                })}
              >
                {currentProblem.difficulty.charAt(0).toUpperCase() +
                  currentProblem.difficulty.slice(1)}
              </span>
            </div>
          </div>

          {/* Feedback */}
          {isCorrect === true && (
            <div
              className={css({
                p: 4,
                bg: 'green.50',
                border: '1px solid',
                borderColor: 'green.200',
                borderRadius: 'md',
                color: 'green.700',
                maxW: '600px',
              })}
            >
              {currentSequenceStep === currentProblem.terms.length - 1
                ? `🎉 Problem completed! Final answer: ${currentProblem.answer}`
                : `✅ Correct! Moving to next step...`}
            </div>
          )}

          {isCorrect === false && (
            <div
              className={css({
                p: 4,
                bg: 'red.50',
                border: '1px solid',
                borderColor: 'red.200',
                borderRadius: 'md',
                color: 'red.700',
                maxW: '600px',
              })}
            >
              Not quite right. Current value: {userAnswer}. Target: {expectedValue}. Keep trying!
            </div>
          )}

          {/* Explanation */}
          {showExplanation && (
            <div
              className={css({
                p: 4,
                bg: 'blue.50',
                border: '1px solid',
                borderColor: 'blue.200',
                borderRadius: 'md',
                color: 'blue.700',
                maxW: '600px',
              })}
            >
              <h4 className={css({ fontWeight: 'bold', mb: 2 })}>Hint:</h4>
              <p>{currentProblem.explanation}</p>
              <div className={css({ mt: 2, fontSize: 'sm' })}>
                <strong>Skills used:</strong> {currentProblem.skillsUsed.join(', ')}
              </div>
            </div>
          )}

          {/* Abacus */}
          <div
            className={css({
              bg: 'white',
              border: '2px solid',
              borderColor: 'gray.200',
              borderRadius: 'lg',
              p: 6,
              shadow: 'lg',
            })}
          >
            <AbacusReact
              value={userAnswer}
              columns={3}
              interactive={true}
              animated={true}
              scaleFactor={2.5}
              colorScheme="place-value"
              onValueChange={handleValueChange}
            />
          </div>

          {/* Current progress info */}
          <div
            className={css({
              p: 3,
              bg: 'gray.50',
              border: '1px solid',
              borderColor: 'gray.200',
              borderRadius: 'md',
              fontSize: 'sm',
              color: 'gray.600',
              textAlign: 'center',
            })}
          >
            <div>Current step target: {expectedValue}</div>
            <div>Your current value: {userAnswer}</div>
            <div>Final target: {currentProblem.answer}</div>
          </div>
        </div>
      </div>
    </div>
  )
}