import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/job/annotated_class.dart';
import 'package:making_school_asignment_app/common/utils/anti_shake_throttling.dart';
import 'package:making_school_asignment_app/common/utils/enum_untils.dart';
import 'package:making_school_asignment_app/page/global_widget/my_text.dart';
import 'package:making_school_asignment_app/page/global_widget/show_student_list.dart';
import 'package:making_school_asignment_app/page/home_page/children/annotate_class/annotate_class_logic.dart';
import 'package:making_school_asignment_app/page/home_page/children/annotate_class/widget/item_btn.dart';
import 'package:making_school_asignment_app/page/home_page/widget/progress_bar.dart';
import 'package:making_school_asignment_app/routes/app_pages.dart';
import 'package:percent_indicator/percent_indicator.dart';

class AnnotateItem extends StatefulWidget {
  final String homeworkId;
  final AnnotatedClass item;
  final double font;
  final String name;
  final AnnotateClassLogic logic;
  const AnnotateItem(
      {super.key,
      required this.homeworkId,
      required this.item,
      required this.font,
      required this.name,
      required this.logic});

  @override
  State<AnnotateItem> createState() => _AnnotateItemState();
}

class _AnnotateItemState extends State<AnnotateItem> {
  Future<bool> confirmDialog(String text) async {
    return await showDialog(
      context: context,
      builder: (context) => AlertDialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.0), // 设置圆角大小
        ),
        actionsPadding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 10.r),
        content: SizedBox(
          width: 200.r,
          child: Text(
            text,
            style: TextStyle(fontSize: 14.sp, color: const Color(0xFF505E6E)),
          ),
        ),
        actionsAlignment: MainAxisAlignment.center,
        actions: <Widget>[
          InkWell(
            child: Container(
                width: 97.r,
                height: 27.r,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(20.r)),
                  color: Theme.of(context).primaryColor,
                ),
                child: const Center(
                    child: Text(
                  '确定',
                  style: TextStyle(color: Colors.white),
                ))),
            onTap: () {
              // 执行操作
              Get.back(result: true);
            },
          ),
          InkWell(
            onTap: () {
              Get.back(result: false);
            },
            child: Container(
                width: 97.r,
                height: 27.r,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(20.r)),
                  color: const Color(0xFFF4F4F4),
                ),
                child: const Center(
                    child: Text(
                  '取消',
                  style: TextStyle(color: Color(0xFF666666)),
                ))),
          ),
        ],
      ),
    );
  }

  void showStudentList(context, List<AnnotatedStudents> students, [bool submitted = false]) async {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return ShowStudentList(
          title: '${widget.item.className} ${submitted ? '已提交' : '未提交'}作业学生',
          studentList: students,
          homeworkId: widget.homeworkId,
          subject: widget.logic.state.subject,
        );
      },
    );
    EasyLoading.dismiss();
  }

  @override
  Widget build(BuildContext context) {
    AnnotatedClass itemData = widget.item;
    return Container(
      padding: EdgeInsets.only(top: 10.r),
      margin: EdgeInsets.only(bottom: 10.r),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadiusDirectional.circular(4.r),
        boxShadow: const [BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.15), blurRadius: 10)],
      ),
      child: Column(
        children: [
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 10.r),
            child: Row(
              children: [
                Text(
                  '${EnumUtils.formatGrade(widget.item.grade)}${widget.item.className}',
                  style: TextStyle(fontSize: widget.font, color: const Color(0xFF000000)),
                ),
                const Spacer(),
                InkWell(
                  onTap: () {
                    EasyLoading.show(status: 'loading...');
                    showStudentList(context, widget.item.commitStudent!, true);
                  },
                  child: Text(
                    '已交：${widget.item.commitStudentCount}',
                    style: TextStyle(fontSize: widget.font - 2.sp, color: Theme.of(context).primaryColor),
                  ),
                ),
                SizedBox(
                  width: 20.r,
                ),
                InkWell(
                  onTap: () {
                    EasyLoading.show(status: 'loading...');
                    showStudentList(context, widget.item.noCommitStudent!, true);
                  },
                  child: Text(
                    '未交：${widget.item.noCommitStudentCount}',
                    style: TextStyle(fontSize: widget.font - 2.sp, color: const Color(0xFFFF5656)),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(
            height: 10.r,
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 8.r),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: widget.item.finishTime != null
                  ? [
                      Expanded(
                        flex: 4,
                        child: ItemBtn(
                          title:
                              "收藏夹${widget.item.homeworkFavs.isNotEmpty ? '(${widget.item.homeworkFavs.length})' : ''}",
                          font: widget.font - 2.sp,
                          clickFunction: () {
                            Get.toNamed(Routes.favStudentPage, arguments: {
                              'homeworkName': widget.name,
                              'classId': widget.item.classId,
                              'homeworkId': widget.logic.state.homeworkId.value,
                              'grade': widget.item.grade
                            });
                          },
                        ),
                      ),
                      const Expanded(flex: 1, child: SizedBox()),
                      const Expanded(flex: 4, child: SizedBox()),
                      const Expanded(flex: 1, child: SizedBox()),
                      const Expanded(flex: 4, child: SizedBox()),
                    ]
                  : [
                      Expanded(
                        flex: 4,
                        child: ItemBtn(
                          title: "一键批阅",
                          font: widget.font - 2.sp,
                          clickFunction: () async {
                            var confim = await confirmDialog('一键批阅后，默认学生答题结果全部正确， 是否进行此操作？');
                            if (confim) {
                              widget.logic.getAllCorrect(widget.item.classId);
                            }
                          },
                          bgColor: Theme.of(context).primaryColor,
                        ),
                      ),
                      const Expanded(
                        flex: 1,
                        child: Text(''),
                      ),
                      Expanded(
                        flex: 4,
                        child: ItemBtn(
                          title: "数据快查",
                          font: widget.font - 2.sp,
                          clickFunction: () {
                            widget.logic.goQuickDataCheck(widget.item);
                          },
                        ),
                      ),
                      const Expanded(
                        flex: 1,
                        child: Text(''),
                      ),
                      Expanded(
                        flex: 4,
                        child: ItemBtn(
                          title:
                              "收藏夹${widget.item.homeworkFavs.isNotEmpty ? '(${widget.item.homeworkFavs.length})' : ''}",
                          font: widget.font - 2.sp,
                          clickFunction: () {
                            Get.toNamed(Routes.favStudentPage, arguments: {
                              'homeworkName': widget.name,
                              'classId': widget.item.classId,
                              'homeworkId': widget.logic.state.homeworkId.value,
                              'grade': widget.item.grade
                            });
                          },
                        ),
                      ),
                    ],
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 10.r, left: 14.r, right: 14.r),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                quickText('批阅进度', size: widget.font - 2.sp, color: const Color(0xFF8E8E8E)),
                quickText('${widget.item.annotateRate}%', size: widget.font + 10.sp, color: const Color(0xFF464646))
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 0.r, left: 14.r, right: 14.r, bottom: 10.r),
            child: LinearPercentIndicator(
              padding: EdgeInsets.zero,
              animation: true,
              lineHeight: 8.h,
              animationDuration: 2500,

              percent: widget.item.annotateRate / 100,
              linearGradient: LinearGradient(
                tileMode: TileMode.mirror,
                stops: const [0.0, 1.0],
                colors: [Theme.of(context).primaryColor.withOpacity(0.1), Theme.of(context).primaryColor],
              ),
              // linearStrokeCap: LinearStrokeCap.butt,
              // progressColor: Theme.of(context).primaryColor,
              backgroundColor: const Color.fromRGBO(232, 232, 232, 1),
              barRadius: Radius.circular(10.r),
            ),
          ),
          ProgressBar(
              title: '客观题正确率：',
              color: Theme.of(context).primaryColor,
              percent: widget.item.kgtCorrectRate / 100,
              marginEdg: EdgeInsets.zero,
              padingEdg: EdgeInsets.only(top: 8.h, left: 14.r, right: 14.r),
              fontSize: widget.font - 2.sp),
          ProgressBar(
              title: '主观题正确率：',
              color: Theme.of(context).primaryColor,
              percent: widget.item.zgtCorrectRate / 100,
              padingEdg: EdgeInsets.symmetric(horizontal: 10.r),
              marginEdg: EdgeInsets.only(top: 8.h),
              fontSize: widget.font - 2.sp),
          ProgressBar(
              title: '总正确率：',
              color: Theme.of(context).primaryColor,
              percent: widget.item.correctRate / 100,
              padingEdg: EdgeInsets.symmetric(horizontal: 10.r),
              marginEdg: EdgeInsets.only(top: 8.h),
              fontSize: widget.font - 2.sp),
          Container(
            margin: EdgeInsets.only(top: 10.r),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.only(bottomLeft: Radius.circular(4.r), bottomRight: Radius.circular(4.r)),
              color: Colors.white,
              boxShadow: const [
                BoxShadow(
                  color: Color.fromRGBO(0, 0, 0, 0.15),
                  offset: Offset(0, -0.0001), //阴影y轴偏移量
                  blurRadius: 4, //阴影模糊程度
                  spreadRadius: 0, //阴影扩散程度
                )
              ],
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: widget.item.finishTime != null
                  ? [
                      Expanded(
                        child: InkWell(
                          onTap: () {
                            widget.logic.goQuickDataCheck(widget.item);
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(vertical: 10.r),
                            decoration: BoxDecoration(
                                border: Border(
                                    right: BorderSide(width: 1.r, color: const Color.fromRGBO(221, 221, 221, 1)))),
                            alignment: Alignment.center,
                            child: quickText('数据快查', color: const Color.fromRGBO(118, 118, 118, 1), size: widget.font),
                          ),
                        ),
                      ),
                      // Container(width: 1.w, height: 30.h, color: const Color.fromRGBO(221, 221, 221, 1)),
                      Expanded(
                          child: InkWell(
                        onTap: () {
                          widget.logic.gojobReport(widget.item);
                        },
                        child: Container(
                          alignment: Alignment.center,
                          child: quickText('查看报告', color: const Color.fromRGBO(118, 118, 118, 1), size: widget.font),
                        ),
                      )),
                    ]
                  : [
                      Expanded(
                        child: InkWell(
                          onTap: () => easyThrottle('TO_GO_REVIEWHOMEWORK', () {
                            /// 跳转到批阅作业页面
                            widget.logic.toGoReviewhomework(
                              homeworkId: widget.homeworkId,
                              homeworkName: widget.name,
                              classId: itemData.classId,
                              subject: widget.logic.state.subject,
                            );
                          }),
                          child: Container(
                            padding: EdgeInsets.symmetric(vertical: 10.r),
                            decoration: BoxDecoration(
                                border: Border(
                                    right: BorderSide(width: 1.r, color: const Color.fromRGBO(221, 221, 221, 1)))),
                            alignment: Alignment.center,
                            child: quickText('批阅', color: const Color.fromRGBO(118, 118, 118, 1), size: widget.font),
                          ),
                        ),
                      ),
                      // Container(width: 1.w, height: 30.h, color: const Color.fromRGBO(221, 221, 221, 1)),
                      Expanded(
                          child: InkWell(
                        onTap: () async {
                          var confim = await confirmDialog('当前批阅任务未完成，请确认需要结束此任务？');
                          if (confim) {
                            widget.logic.getOverAnnotate(widget.item.classId);
                          }
                        },
                        child: Container(
                          alignment: Alignment.center,
                          child: quickText('结束批阅', color: const Color.fromRGBO(118, 118, 118, 1), size: widget.font),
                        ),
                      )),
                    ],
            ),
          )
        ],
      ),
    );
  }
}
