import { useState, useRef, useEffect } from "react"; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { DossierPanel } from "@/components/game/DossierPanel"; import { LoadingOverlay } from "@/components/game/LoadingOverlay"; import { BriefingAudio } from "@/components/game/BriefingAudio"; import { GameBackground } from "@/components/GameBackground"; import { MonthTransition } from "@/components/MonthTransition"; import { IntroDialog } from "../components/game/IntroDialog"; import { useGameStages, OPERATION_NAMES, useLoadingMessages, generateFinalReport, ChoiceID } from "@/components/game/constants"; import { DossierEntry, GameStage } from "@/components/game/types"; import { useToast } from "@/components/ui/use-toast"; import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Separator } from "@/components/ui/separator"; import { ClipboardList } from "lucide-react"; import { Badge } from "@/components/ui/badge"; import { AlertCircle, Lock, Shield } from "lucide-react"; import { playAcceptMissionSound, playDeployStratagemSound, playRecordingSound, playClickSound } from "@/utils/audio"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { TransitionStyle } from "@/components/MonthTransition"; import { ChoiceCard } from "@/components/game/ChoiceCard"; import { FinalMemo } from '../components/game/FinalMemo'; import { StrategyAnimation } from '@/components/game/StrategyAnimation'; import { IntroAudio } from '@/components/game/IntroAudio'; import { Footer } from '../components/Footer'; import { useTranslation } from 'react-i18next'; import { LanguageSwitcher } from '@/components/LanguageSwitcher'; import '@/i18n/config'; import { MetricsDisplay } from "@/components/game/MetricsDisplay"; const Index = () => { const { t } = useTranslation(); const stages = useGameStages(); const operationNameKey = OPERATION_NAMES[Math.floor(Math.random() * OPERATION_NAMES.length)]; const operationName = t(`operations.${operationNameKey}`); const [agentNumber] = useState(Math.floor(Math.random() * 999).toString().padStart(3, '0')); const [currentStage, setCurrentStage] = useState(0); const [gameStarted, setGameStarted] = useState(false); const [showingResult, setShowingResult] = useState(false); const [currentResult, setCurrentResult] = useState(null); const [dossierEntries, setDossierEntries] = useState([]); const { toast } = useToast(); const [showingMonthTransition, setShowingMonthTransition] = useState(false); const [nextStage, setNextStage] = useState(null); const [transitionStyle, setTransitionStyle] = useState(TransitionStyle.NUMBER_CYCLE); const [isLoading, setIsLoading] = useState(false); const [loadingMessage, setLoadingMessage] = useState(''); const [loadingProgress, setLoadingProgress] = useState(0); const audioRef = useRef(null); const [showingInitialTransition, setShowingInitialTransition] = useState(false); const [showIntroDialog, setShowIntroDialog] = useState(true); const [showConfirmDialog, setShowConfirmDialog] = useState(false); const [selectedChoice, setSelectedChoice] = useState(null); const [previousChoices, setPreviousChoices] = useState([]); const [gameComplete, setGameComplete] = useState(false); const [playerChoices, setPlayerChoices] = useState([]); const [gameKey, setGameKey] = useState(0); const loadingMessages = useLoadingMessages(); const [shouldStartAudio, setShouldStartAudio] = useState(false); const handleStartGame = () => { playAcceptMissionSound(); setShowIntroDialog(false); setShowingInitialTransition(true); }; const handleInitialTransitionComplete = () => { setShowingInitialTransition(false); setGameStarted(true); toast({ title: t('mission.welcome.title'), description: t('mission.welcome.description'), }); }; const handleChoice = async (choice: GameStage["choices"][0]) => { if (!choice.choiceId) return; // Skip if no choiceId setPreviousChoices(prev => [...prev, choice.choiceId as ChoiceID]); playDeployStratagemSound(); if (audioRef.current) { audioRef.current.pause(); audioRef.current = null; } setIsLoading(true); setLoadingProgress(0); const messages = loadingMessages.getMessagesForChoice(choice.loadingMessageKey); let totalDuration = 0; for (const message of messages) { totalDuration += message.duration; } let elapsed = 0; for (const message of messages) { setLoadingMessage(message.action); await new Promise(resolve => setTimeout(resolve, message.duration)); elapsed += message.duration; setLoadingProgress((elapsed / totalDuration) * 100); } setIsLoading(false); setCurrentResult(choice.result); setShowingResult(true); const newEntry: DossierEntry = { dateKey: stages[currentStage].monthIndex === 0 ? 'months.january' : stages[currentStage].monthIndex === 1 ? 'months.february' : stages[currentStage].monthIndex === 2 ? 'months.march' : stages[currentStage].monthIndex === 3 ? 'months.april' : stages[currentStage].monthIndex === 4 ? 'months.may' : stages[currentStage].monthIndex === 5 ? 'months.june' : stages[currentStage].monthIndex === 6 ? 'months.july' : stages[currentStage].monthIndex === 7 ? 'months.august' : stages[currentStage].monthIndex === 8 ? 'months.september' : stages[currentStage].monthIndex === 9 ? 'months.october' : stages[currentStage].monthIndex === 10 ? 'months.november' : 'months.december', titleKey: `stages.${currentStage + 1}.choices.${choice.id}.result.title`, insightKeys: Array.from({ length: 4 }, (_, i) => `stages.${currentStage + 1}.choices.${choice.id}.result.insights.${i}`), strategicNoteKey: `stages.${currentStage + 1}.choices.${choice.id}.result.nextStepHint` }; setDossierEntries(prev => [...prev, newEntry]); toast({ title: t('analysis.intelligenceGathered.title'), description: t('analysis.intelligenceGathered.description'), }); if (currentStage === stages.length - 1) { setGameComplete(true); } }; const handleContinue = () => { playDeployStratagemSound(); setShowingResult(false); // Check if this was the last stage if (currentStage >= stages.length - 1) { setGameComplete(true); return; } // Otherwise, continue to next stage setShowingMonthTransition(true); setNextStage(currentStage + 1); }; const handleTransitionComplete = () => { setShowingMonthTransition(false); if (nextStage !== null) { setCurrentStage(nextStage); setNextStage(null); } }; const handleStrategyClick = (choice: GameStage["choices"][0]) => { playClickSound(); setSelectedChoice(choice); setShowConfirmDialog(true); }; const handleConfirmStrategy = async () => { if (!selectedChoice) return; setShowConfirmDialog(false); await handleChoice(selectedChoice); }; const handleRestart = () => { setGameKey(prev => prev + 1); setCurrentStage(0); setGameStarted(false); setShowingResult(false); setCurrentResult(null); setDossierEntries([]); setPreviousChoices([]); setGameComplete(false); setPlayerChoices([]); setShowIntroDialog(true); setShowingInitialTransition(false); setSelectedChoice(null); if (audioRef.current) { audioRef.current.pause(); audioRef.current = null; } }; if (!gameStarted) { if (showingInitialTransition) { return (
); } return (
{showIntroDialog && setShouldStartAudio(true)} />}
{t('mission.topSecret')} {t('mission.classified')}
{t('mission.title', { operationName })}
{t('mission.classified')}
{t('mission.clearanceLevel')}

{t('mission.directorate')}

{t('mission.to', { agentNumber })}

{t('mission.subject', { operationName })}

Date: {new Date().toLocaleDateString('en-GB')}

{t('mission.briefing.part1')}

{t('mission.briefing.part2')}

{t('mission.briefing.warning')}

{t('mission.quote')}

{t('warnings.selfDestruct')}

); } const currentStageData = stages[currentStage]; if (gameComplete) { return ; } if (!currentStageData) { return (
Operation Status Unknown Please restart the mission.
); } if (showingResult && currentResult) { return (
{currentResult.title}
{currentResult.description}

{t('analysis.keyInsights')}

    {currentResult.insights.map((insight, index) => (
  • {insight}
  • ))}

{t('analysis.strategicInsight')} {currentResult.nextStepHint}

); } if (showingMonthTransition && nextStage !== null && stages[nextStage]) { return (
); } return (
{currentStage > 0 && }
{currentStageData.title} {currentStageData.description}
{currentStageData.choices.map((choice, index) => ( handleStrategyClick(choice)} disabled={showingResult || isLoading} optionNumber={index + 1} /> ))}
{selectedChoice?.text} {selectedChoice && ( )} {selectedChoice && ( <> {(selectedChoice.strengthenedBy?.some(choice => previousChoices.includes(choice)) || selectedChoice.weakenedBy?.some(choice => previousChoices.includes(choice))) && (
{selectedChoice.strengthenedBy?.some(choice => previousChoices.includes(choice)) && (
Enhanced by: {selectedChoice.strengthenedBy .filter(choice => previousChoices.includes(choice)) .join(', ')}
)} {selectedChoice.weakenedBy?.some(choice => previousChoices.includes(choice)) && (
Weakened by: {selectedChoice.weakenedBy .filter(choice => previousChoices.includes(choice)) .join(', ')}
)}
)} )}

{t('analysis.strategyOverview')}:

{selectedChoice?.description}

{t('analysis.expertAnalysis')}:

{selectedChoice?.explainer}

{isLoading && }
); }; export default Index;