import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:english_learning/core/utils/styles/theme.dart';
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';

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

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

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

    final options = [
      {'label': 'A', 'value': '1', 'text': 'True'},
      {'label': 'B', 'value': '0', 'text': 'False'}
    ];

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

  String? _getStudentAnswer() {
    if (isReview && exercise is ReviewExerciseDetail) {
      return exercise.answerStudent;
    }
    return null;
  }

  Widget _buildOptionsList(
    BuildContext context,
    List<Map<String, String>> options,
    ExerciseProvider provider,
    String? studentAnswer,
  ) {
    return Column(
      children: options.map((option) {
        final value = option['value']!;
        final label = option['label']!;
        final text = option['text']!;

        bool isSelected = false;
        bool isCorrect = false;

        if (isReview && exercise is ReviewExerciseDetail) {
          isSelected = studentAnswer == value;
          isCorrect = exercise.isCorrect == 1;
        } else {
          isSelected = provider.answers[provider.currentExerciseIndex] == value;
        }

        return GestureDetector(
          onTap: isReview
              ? null
              : () {
                  provider.answerQuestion(provider.currentExerciseIndex, value);
                },
          child: _buildOptionItem(
            label,
            text,
            isSelected,
            isReview && isSelected,
            isCorrect,
          ),
        );
      }).toList(),
    );
  }

  Widget _buildOptionItem(
    String label,
    String text,
    bool isSelected,
    bool isReviewSelected,
    bool isCorrect,
  ) {
    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(
                text,
                style: AppTextStyles.blackTextStyle.copyWith(
                  color: textColor,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
