import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';
import 'package:making_school_asignment_app/common/job/homework_details.dart';
import 'package:making_school_asignment_app/common/utils/utils.dart';
import 'package:making_school_asignment_app/page/global_widget/MyEmptyWidget.dart';
import 'package:making_school_asignment_app/page/global_widget/my_text.dart';
import 'package:making_school_asignment_app/routes/app_pages.dart';

class KnowledgePoint extends StatefulWidget {
  final RxList<Knows> knowsList;
  final HomeworkDetails data;
  final String className;
  final String homeworkId;
  final int subject;
  const KnowledgePoint(
      {super.key, required this.knowsList, required this.data, required this.className, required this.homeworkId, required this.subject});

  @override
  State<KnowledgePoint> createState() => _KnowledgePointState();
}

class _KnowledgePointState extends State<KnowledgePoint> {
  void goQuickCheckPersonalPath(studentId) {
    if (studentId != null) {
      Get.toNamed(Routes.studentPersonalPage, arguments: {'studentId': studentId, 'homeworkId': widget.homeworkId, 'subject': widget.subject});
    }
  }

  void showPeopleListDialog({
    required BuildContext context,
    required String title,
    required List<Students> arr,
  }) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            // insetPadding: EdgeInsets.symmetric(vertical: 20.r,horizontal: 20.r),
            backgroundColor: const Color(0xFFFFFFFF),
            contentPadding: EdgeInsets.all(20.r),
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(15.r))),
            content: SizedBox(
              width: MediaQuery.of(context).size.width * 0.7,
              height: MediaQuery.of(context).size.height * 0.7,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Center(
                    child: Text(
                      title,
                      style: TextStyle(fontSize: 15.sp, color: const Color(0xFF3C3C3C), fontWeight: FontWeight.w500),
                    ),
                  ),
                  SizedBox(
                    height: 10.r,
                  ),
                  Row(
                    children: [
                      Expanded(
                          flex: 2,
                          child: Center(
                              child: Text(
                            '姓名',
                            style: TextStyle(fontSize: 12.sp, color: const Color(0xFF6A6A6A)),
                          ))),
                      SizedBox(
                        width: 10.r,
                      ),
                      Expanded(
                          flex: 1,
                          child: Center(
                              child: Text(
                            '班级',
                            style: TextStyle(fontSize: 12.sp, color: const Color(0xFF6A6A6A)),
                          ))),
                      SizedBox(
                        width: 10.r,
                      ),
                      Expanded(
                          flex: 2,
                          child: Center(
                              child: Text(
                            '掌握度',
                            style: TextStyle(fontSize: 12.sp, color: const Color(0xFF6A6A6A)),
                          ))),
                      SizedBox(
                        width: 10.r,
                      ),
                      Expanded(
                          flex: 3,
                          child: Center(
                              child: Text(
                            '掌握情况',
                            style: TextStyle(fontSize: 12.sp, color: const Color(0xFF6A6A6A)),
                          ))),
                    ],
                  ),
                  SizedBox(
                    height: 5.r,
                  ),
                  arr.isNotEmpty
                      ? Expanded(
                          child: ListView.builder(
                            shrinkWrap: true,
                            itemBuilder: (context, index) {
                              var item = arr[index];
                              return Container(
                                padding: EdgeInsets.symmetric(vertical: 5.r),
                                color: const Color(0xFFF0F0F0),
                                margin: EdgeInsets.only(bottom: 2.r),
                                child: Row(
                                  children: [
                                    Expanded(
                                        flex: 2,
                                        child: InkWell(
                                            onTap: () {
                                              goQuickCheckPersonalPath(item.studentId);
                                            },
                                            child: Center(
                                                child: Text(
                                              item.studentName!,
                                              style: TextStyle(fontSize: 12.sp, color: const Color(0xFF323232)),
                                            )))),
                                    SizedBox(
                                      width: 10.r,
                                    ),
                                    Expanded(
                                        flex: 1,
                                        child: Center(
                                            child: Text(
                                          widget.className == '' ? '全部' : widget.className,
                                          style: TextStyle(fontSize: 12.sp, color: Color(0xFF323232)),
                                        ))),
                                    SizedBox(
                                      width: 10.r,
                                    ),
                                    Expanded(
                                        flex: 2,
                                        child: Center(
                                            child: Text(
                                          '${item.okRate!.toStringAsFixed(0)}%',
                                          style: TextStyle(fontSize: 12.sp, color: Color(0xFF323232)),
                                        ))),
                                    SizedBox(
                                      width: 10.r,
                                    ),
                                    Expanded(
                                      flex: 3,
                                      child: Padding(
                                        padding: EdgeInsets.symmetric(vertical: 2.r, horizontal: 5.r),
                                        child: SingleChildScrollView(
                                          child: Wrap(
                                              direction: Axis.horizontal,
                                              alignment: WrapAlignment.start,
                                              spacing: 5,
                                              runSpacing: 3,
                                              children: List.generate(item.queDtls!.length, (index) {
                                                Dtls kgInfo = item.queDtls![index];
                                                return Container(
                                                  width: 20.r,
                                                  height: 20.r,
                                                  decoration: BoxDecoration(
                                                      color: kgInfo.state == 0
                                                          ? const Color(0xFFD3D3D3)
                                                          : kgInfo.state == 1
                                                              ? Colors.white
                                                              : kgInfo.state == 2
                                                                  ? const Color(0xFFFF7474)
                                                                  : Theme.of(context).primaryColor,
                                                      borderRadius: BorderRadius.all(Radius.circular(10.r))),
                                                  child: Center(
                                                      child: Text(
                                                    kgInfo.questionNo.toString(),
                                                    style: TextStyle(fontSize: 8.sp, color: kgInfo.state == 1 ? Color(0xFF525252) : Colors.white),
                                                  )),
                                                );
                                              })),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            },
                            itemCount: arr.length,
                          ),
                        )
                      : const MyEmptyWidget()
                ],
              ),
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.only(top: 10.h),
        padding: EdgeInsets.symmetric(vertical: 16.h, horizontal: 12.w),
        constraints: BoxConstraints(maxHeight: 320.h),
        decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10.r)),
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.only(bottom: 24.h),
              child: quickText('知识点掌握情况', color: const Color.fromRGBO(92, 92, 92, 1), size: 14.sp, fontWeight: FontWeight.bold),
            ),
            Expanded(
              child: widget.knowsList.isNotEmpty
                  ? ListView(
                      children: widget.knowsList.value.map((item) {
                      return Container(
                        margin: EdgeInsets.only(bottom: 15.h, left: 15.r, right: 15.r),
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Expanded(
                                  flex: 10,
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      quickText(item.knowledgeName, size: 12.sp, color: const Color.fromRGBO(152, 152, 152, 1)),
                                      quickText('${item.okRate!.toStringAsFixed(0)}%', size: 12.sp, color: const Color.fromRGBO(64, 64, 64, 1)),
                                    ],
                                  ),
                                ),
                                SizedBox(width: 10.w),
                                const Expanded(flex: 1, child: SizedBox()),
                              ],
                            ),
                            SizedBox(height: 3.h),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Expanded(
                                    flex: 10,
                                    child: LinearPercentIndicator(
                                      padding: EdgeInsets.zero,
                                      animation: true,
                                      lineHeight: 10.h,
                                      animationDuration: 2500,
                                      percent: item.okRate! / 100,
                                      progressColor: Theme.of(context).primaryColor,
                                      backgroundColor: const Color.fromRGBO(219, 224, 243, 1),
                                      barRadius: Radius.circular(10.r),
                                    )),
                                SizedBox(width: 10.w),
                                InkWell(
                                  onTap: () {
                                    List<Questions> ques = widget.data.questions
                                        .where((w) => w.knows.indexWhere((k) => k.knowledgeId == item.knowledgeId) > -1)
                                        .toList();
                                    List<Students> array2 = [...widget.data.students];
                                    for (var stu in array2) {
                                      stu.queDtls = widget.data.dtls
                                          .where((w) =>
                                              w.studentId == stu.studentId &&
                                              ques.indexWhere((q) => w.templateId == q.templateId && w.questionNo == q.questionNo) > -1)
                                          .toList();
                                      int okCount = stu.queDtls!.where((w) => w.state == 3).length;
                                      int ttlCount = stu.queDtls!.length;
                                      stu.okRate = Utils.calcRate(okCount, ttlCount);
                                      stu.queDtls!.sort((a, b) {
                                        try {
                                          if (RegExp(r'^\d*\.?\d+$').hasMatch(a.questionNo) && RegExp(r'^\d*\.?\d+$').hasMatch(b.questionNo)) {
                                            return num.parse(a.questionNo).compareTo(num.parse(b.questionNo));
                                          } else {
                                            throw Exception();
                                          }
                                        } catch (e) {
                                          return a.questionNo.toLowerCase().compareTo(b.questionNo.toLowerCase());
                                        }
                                      });
                                    }

                                    showPeopleListDialog(
                                      context: context,
                                      title: item.knowledgeName,
                                      arr: array2,
                                    );
                                  },
                                  child: Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      quickText('查看', size: 12.sp, color: const Color.fromRGBO(239, 135, 20, 1)),
                                      Icon(Icons.arrow_forward_ios, size: 11.sp, color: const Color.fromRGBO(239, 135, 20, 1)),
                                    ],
                                  ),
                                ),
                              ],
                            )
                          ],
                        ),
                      );
                    }).toList())
                  : MyEmptyWidget(imgWidth: 100.r, imgHeight: 100.r, font: 8.sp),
            ),
          ],
        ));
  }
}
