import 'package:bootstrap_icons/bootstrap_icons.dart';
import 'package:english_learning/features/learning/modules/exercises/providers/exercise_provider.dart';
import 'package:english_learning/features/learning/modules/exercises/widgets/content/exercise_content.dart';
import 'package:english_learning/features/learning/modules/exercises/widgets/exercise_navigator.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:english_learning/core/utils/styles/theme.dart';

class ReviewScreen extends StatefulWidget {
  final String idStudentLearning;
  final String? topicId;
  final String topicTitle;

  const ReviewScreen({
    super.key,
    required this.idStudentLearning,
    this.topicId,
    required this.topicTitle,
  });

  @override
  State<ReviewScreen> createState() => _ReviewScreenState();
}

class _ReviewScreenState extends State<ReviewScreen> {
  late ScrollController _scrollController;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final provider = context.read<ExerciseProvider>();
      provider.fetchReviewExercises(widget.idStudentLearning);
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  void _scrollToTop() {
    _scrollController.animateTo(
      0,
      duration: const Duration(milliseconds: 300),
      curve: Curves.easeOut,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<ExerciseProvider>(builder: (context, provider, child) {
      if (provider.isLoading) {
        return const Scaffold(
          body: Center(child: CircularProgressIndicator()),
        );
      }

      final currentExercise = provider.currentExercise;
      final hasExercises = provider.reviewExercises.isNotEmpty;

      if (!hasExercises || currentExercise == null) {
        return const Scaffold(
          body: Center(child: Text('No review exercises available')),
        );
      }

      return Scaffold(
        backgroundColor: AppColors.bgSoftColor,
        appBar: AppBar(
          elevation: 0,
          centerTitle: true,
          title: Text(
            '${provider.nameTopic} - ${provider.nameLevel}',
            style: AppTextStyles.whiteTextStyle.copyWith(
              fontSize: 14,
              fontWeight: FontWeight.w900,
            ),
          ),
          flexibleSpace: Container(
            decoration: BoxDecoration(
              gradient: AppColors.gradientTheme,
            ),
          ),
          leading: IconButton(
            icon: const Icon(
              BootstrapIcons.arrow_left,
              color: AppColors.whiteColor,
            ),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ),
        body: SafeArea(
          child: SingleChildScrollView(
            controller: _scrollController,
            padding: const EdgeInsets.symmetric(horizontal: 16.0),
            child: Column(
              children: [
                const SizedBox(height: 16),
                ExerciseContent(
                  key: ValueKey(provider.currentExerciseIndex),
                  exercise: currentExercise,
                  isReview: true,
                ),
                const SizedBox(height: 24),
                ExerciseNavigator(
                  onScrollToTop: _scrollToTop,
                  isReview: true,
                  topicId: widget.topicId ?? '',
                  topicTitle: widget.topicTitle,
                ),
                const SizedBox(height: 32),
              ],
            ),
          ),
        ),
      );
    });
  }
}
