import React from 'react';
import { View, StyleSheet } from 'react-native';
import Animated, { FadeInRight, FadeOutLeft } from 'react-native-reanimated';
import { colors, spacing } from '../../theme/tokens';
import { SafeScreen } from '../../components/layout/SafeScreen';
import { Header } from '../../components/layout/Header';
import { ProgressBar } from '../../components/ui';
import { useQuizStore } from '../../store';
import { t } from '../../i18n';
import { QuizStep1 } from './QuizStep1';
import { QuizStep2 } from './QuizStep2';
import { QuizStep3 } from './QuizStep3';
import { QuizStep4 } from './QuizStep4';
import { QuizStep5 } from './QuizStep5';
import { QuizStep6 } from './QuizStep6';

interface QuizContainerProps {
  onComplete: () => void;
}

export function QuizContainer({ onComplete }: QuizContainerProps) {
  const { step, totalSteps, setStep } = useQuizStore();

  const progress = (step + 1) / totalSteps;

  const handleNext = () => {
    if (step < totalSteps - 1) {
      setStep(step + 1);
    }
  };

  const handleBack = () => {
    if (step > 0) {
      setStep(step - 1);
    }
  };

  const renderStep = () => {
    switch (step) {
      case 0: return <QuizStep1 onNext={handleNext} />;
      case 1: return <QuizStep2 onNext={handleNext} />;
      case 2: return <QuizStep3 onNext={handleNext} />;
      case 3: return <QuizStep4 onNext={handleNext} />;
      case 4: return <QuizStep5 onNext={handleNext} />;
      case 5: return <QuizStep6 onComplete={onComplete} />;
      default: return null;
    }
  };

  return (
    <SafeScreen backgroundColor={colors.background}>
      <Header
        title={`${step + 1} / ${totalSteps}`}
        onBack={step > 0 ? handleBack : undefined}
      />
      <View style={styles.progressWrap}>
        <ProgressBar progress={progress} color={colors.secondary[500]} />
      </View>
      <Animated.View
        key={step}
        entering={FadeInRight.duration(300)}
        exiting={FadeOutLeft.duration(200)}
        style={styles.content}
      >
        {renderStep()}
      </Animated.View>
    </SafeScreen>
  );
}

const styles = StyleSheet.create({
  progressWrap: {
    paddingHorizontal: spacing.xl,
    paddingBottom: spacing.lg,
  },
  content: {
    flex: 1,
  },
});
