import 'package:bootstrap_icons/bootstrap_icons.dart';
import 'package:english_learning/core/utils/styles/theme.dart';
import 'package:english_learning/core/widgets/custom_button.dart';
import 'package:english_learning/features/learning/modules/level/models/level_model.dart';
import 'package:english_learning/features/learning/modules/level/providers/level_provider.dart';
import 'package:english_learning/features/learning/modules/material/screens/material_screen.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class PretestCard extends StatelessWidget {
  final Level pretest;
  final int? score;
  final bool isCompleted;
  final bool isAllowed;

  const PretestCard({
    super.key,
    required this.pretest,
    this.score,
    required this.isCompleted,
    required this.isAllowed,
  });

  @override
  Widget build(BuildContext context) {
    return Consumer<LevelProvider>(builder: (context, levelProvider, _) {
      final isFinished = levelProvider.isPretestFinished(pretest.idLevel);
      final score = levelProvider.getPretestScore(pretest.idLevel);
      // final isAllowed =  levelProvider.isLevelAllowed(pretest.idLevel);
      void navigateToMaterial() {
        if (isFinished) {
          // Mode review untuk pretest yang sudah selesai
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => MaterialScreen(
                levelId: pretest.idLevel,
                isReview: true,
                studentLearningId: pretest.idStudentLearning,
                topicId: pretest.idTopic,
                topicTitle: pretest.nameTopic,
              ),
            ),
          );
        } else {
          // Mode normal untuk pretest yang belum dikerjakan
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => MaterialScreen(
                levelId: pretest.idLevel,
                isReview: false,
                topicId: pretest.idTopic,
                topicTitle: pretest.nameTopic,
              ),
            ),
          );
        }
      }

      return Card(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        elevation: 0,
        child: Container(
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(12),
            gradient: AppColors.gradientTheme,
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        padding: const EdgeInsets.symmetric(
                            vertical: 4, horizontal: 8),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(8),
                          color:
                              isCompleted ? Colors.green : Colors.transparent,
                          border: Border.all(
                            color: AppColors.whiteColor,
                            width: 1.5,
                          ),
                        ),
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text(
                              pretest.nameLevel,
                              style: AppTextStyles.whiteTextStyle.copyWith(
                                fontWeight: FontWeight.w900,
                                fontSize: 16,
                              ),
                            ),
                            if (isCompleted) ...[
                              const SizedBox(width: 4),
                              const Icon(
                                BootstrapIcons.check_all,
                                color: AppColors.whiteColor,
                                size: 20,
                              ),
                            ],
                          ],
                        ),
                      ),
                      const SizedBox(height: 6),
                      Text(
                        'Score $score/100',
                        style: AppTextStyles.whiteTextStyle.copyWith(
                          fontWeight: FontWeight.w900,
                          fontSize: 12,
                        ),
                      ),
                      const SizedBox(height: 16),
                    ],
                  ),
                  Flexible(
                    child: Image.asset(
                      'lib/features/learning/modules/level/assets/images/pretest_level_illustration.png',
                      height: 95,
                      fit: BoxFit.cover,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 13),
              CustomButton(
                text: isFinished ? 'Review' : 'Learn Now',
                textStyle: isFinished
                    ? AppTextStyles.whiteTextStyle.copyWith(
                        fontWeight: FontWeight.w900,
                      )
                    : AppTextStyles.blackButtonTextStyle.copyWith(
                        fontWeight: FontWeight.w900,
                      ),
                width: double.infinity,
                height: 36,
                color: isFinished ? Colors.green : AppColors.yellowButtonColor,
                onPressed: navigateToMaterial,
              ),
            ],
          ),
        ),
      );
    });
  }
}
