import 'package:english_learning/features/learning/modules/exercises/models/review_exercise_model.dart';
import 'package:english_learning/features/learning/modules/exercises/providers/exercise_provider.dart';
import 'package:flutter/material.dart';
import 'package:english_learning/core/utils/styles/theme.dart';
import 'package:english_learning/features/learning/modules/exercises/models/exercise_model.dart';
import 'package:provider/provider.dart';

class MultipleChoiceQuestion extends StatelessWidget {
  final dynamic exercise;
  final bool isReview;

  const MultipleChoiceQuestion({
    super.key,
    required this.exercise,
    this.isReview = false,
  });

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<ExerciseProvider>(context);
    List<String> options = [];
    String? studentAnswer;

    if (exercise is ExerciseModel) {
      final multipleChoice = exercise.choices as MultipleChoice;
      options = [
        multipleChoice.optionA,
        multipleChoice.optionB,
        multipleChoice.optionC,
        multipleChoice.optionD,
        multipleChoice.optionE,
      ];
    } else if (exercise is ReviewExerciseDetail) {
      if (exercise.multipleChoices?.isNotEmpty ?? false) {
        final choices = exercise.multipleChoices!.first;
        options = [
          choices.optionA,
          choices.optionB,
          choices.optionC,
          choices.optionD,
          choices.optionE,
        ];
        studentAnswer = exercise.answerStudent;
      }
    }

    return _buildOptionsList(context, options, provider, studentAnswer);
  }

  Widget _buildOptionsList(
    BuildContext context,
    List<String> options,
    ExerciseProvider provider,
    String? studentAnswer,
  ) {
    return Column(
      children: options.asMap().entries.map((entry) {
        final index = entry.key;
        final option = entry.value;
        final optionLabel = String.fromCharCode(65 + index); // A, B, C, D, E

        bool isSelected = false;
        if (isReview) {
          isSelected = studentAnswer == optionLabel;
        } else {
          isSelected =
              provider.answers[provider.currentExerciseIndex] == optionLabel;
        }

        return GestureDetector(
          onTap: isReview
              ? null
              : () {
                  provider.answerQuestion(
                      provider.currentExerciseIndex, option);
                },
          child: _buildOptionItem(optionLabel, option, isSelected),
        );
      }).toList(),
    );
  }

  Widget _buildOptionItem(String label, String option, bool isSelected) {
    final backgroundColor =
        isSelected ? AppColors.blueColor : AppColors.whiteColor;
    final textColor = isSelected ? AppColors.whiteColor : AppColors.blackColor;

    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            decoration: BoxDecoration(
              color: backgroundColor,
              borderRadius: BorderRadius.circular(25),
              border: Border.all(
                color: isSelected
                    ? Colors.transparent
                    : AppColors.cardDisabledColor,
              ),
            ),
            padding:
                const EdgeInsets.symmetric(horizontal: 14.0, vertical: 10.0),
            child: Text(
              label,
              style: AppTextStyles.blackTextStyle.copyWith(
                fontWeight: FontWeight.w900,
                color: textColor,
              ),
            ),
          ),
          const SizedBox(width: 8),
          Expanded(
            child: Container(
              margin: const EdgeInsets.only(bottom: 8.0),
              decoration: BoxDecoration(
                color: backgroundColor,
                borderRadius: const BorderRadius.only(
                  topRight: Radius.circular(20),
                  bottomRight: Radius.circular(20),
                  bottomLeft: Radius.circular(20),
                ),
                border: Border.all(
                  color: isSelected
                      ? Colors.transparent
                      : AppColors.cardDisabledColor,
                ),
              ),
              padding:
                  const EdgeInsets.symmetric(vertical: 10.0, horizontal: 16.0),
              child: Text(
                option,
                style: AppTextStyles.blackTextStyle.copyWith(
                  color: textColor,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
