import 'package:cherry_toast/cherry_toast.dart';
import 'package:cherry_toast/resources/arrays.dart';
import 'package:flutter/material.dart';
import 'package:initial_folder/models/quiz_model.dart';
import 'package:initial_folder/screens/course/quiz_question_page.dart';
import 'package:initial_folder/services/quiz_service.dart';
import 'package:initial_folder/size_config.dart';
import 'package:initial_folder/theme.dart';
import 'package:initial_folder/widgets/login_regist/default_button.dart';
import 'package:tap_debouncer/tap_debouncer.dart';

class quizPage extends StatefulWidget {
  final String judulQuiz;
  final String lessonId;
  const quizPage({super.key, required this.judulQuiz, required this.lessonId});

  @override
  State<quizPage> createState() => _quizPageState();
}

class _quizPageState extends State<quizPage> {
  QuizModel? quizModel;

  @override
  void initState() {
    super.initState();
    print(widget.judulQuiz);
    print(widget.lessonId);
    getQuiz();
  }

  void getQuiz() {
    quiz_service().get_quiz_info(widget.lessonId).then((value) => {
          setState(() {
            quizModel = value;
            print("ini id apa gatau" + quizModel!.data.first.quizId);
          })
        });
  }

  Widget build(BuildContext context) {
    return quizModel == null
        ? Center(
            child: Scaffold(
            body: Center(
              child: CircularProgressIndicator(
                color: primaryColor,
              ),
            ),
          ))
        : Scaffold(
            appBar: AppBar(
              centerTitle: true,
              backgroundColor: Colors.transparent,
            ),
            body: SingleChildScrollView(
              child: Center(
                child: Column(
                  children: [
                    Container(
                      margin: EdgeInsets.all(25),
                      decoration: BoxDecoration(
                          color: Theme.of(context).colorScheme.primaryContainer,
                          borderRadius: BorderRadius.circular(15),
                          boxShadow: [
                            BoxShadow(
                                color: Theme.of(context).brightness ==
                                        Brightness.dark
                                    ? Colors.black
                                    : Colors.grey,
                                blurRadius: 2,
                                spreadRadius: 1,
                                offset: Offset(0, 3))
                          ]),
                      child: Column(
                        children: [
                          SizedBox(
                            height: getProportionateScreenHeight(71),
                          ),
                          Image.asset(
                            'assets/images/quizLogo-page.png',
                            scale: 0.8,
                          ),
                          SizedBox(
                            height: getProportionateScreenHeight(36),
                          ),
                          Padding(
                            padding: EdgeInsets.symmetric(horizontal: 30),
                            child: Text(
                              widget.judulQuiz,
                              textAlign: TextAlign.center,
                              style: thirdTextStyle.copyWith(
                                  fontWeight: bold,
                                  fontSize: getProportionateScreenWidth(15)),
                            ),
                          ),
                          SizedBox(
                            height: getProportionateScreenHeight(10),
                          ),
                          Text(
                            "Jumlah Pertanyaan : " +
                                quizModel!.total.toString(),
                            textAlign: TextAlign.center,
                            style: thirdTextStyle.copyWith(
                                fontWeight: bold,
                                fontSize: getProportionateScreenWidth(15)),
                          ),
                          SizedBox(
                            height: getProportionateScreenHeight(20),
                          ),
                          Padding(
                              padding: EdgeInsets.all(
                                  getProportionateScreenHeight(15)),
                              child: TapDebouncer(
                                cooldown: const Duration(milliseconds: 1000),
                                onTap: () async => await {
                                  if (quizModel!.total.toString() != "0")
                                    {
                                      Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) =>
                                                  quiz_questionPage(
                                                    judulQuiz: widget.judulQuiz,
                                                    lessonId: widget.lessonId,
                                                    totalQuiz: quizModel!.total,
                                                    quizId: int.parse(quizModel!
                                                        .data.first.quizId),
                                                  )))
                                    }
                                  else
                                    {
                                      CherryToast.error(
                                        animationDuration: Durations.long1,
                                        title: Text(
                                          "Pertanyaan untuk quiz ini \nbelum tersedia",
                                          style: TextStyle(
                                            color: Colors.black,
                                            fontSize: 15,
                                          ),
                                        ),
                                        animationType: AnimationType.fromTop,
                                      ).show(context)
                                    }
                                },
                                builder: (BuildContext context,
                                    TapDebouncerFunc? onTap) {
                                  return DefaultButton(
                                      text: 'Mulai Quiz', press: onTap);
                                },
                              ))
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ),
          );
  }
}
