import 'package:achievement_view/achievement_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:marking_app/common/model/marking/marking_text_question_tab.dart';
import 'package:marking_app/utils/my_text.dart';

class MarkingQuestionTypeDrawer extends StatefulHookConsumerWidget {
  final double widthPercent;
  final GlobalKey<ScaffoldState> scaffoldKey;
  final List<MarkingTextQuestionTab> currentTabs;
  final MarkingTextQuestionTab? currentTab; // 当前选中tab
  final Function(String tabQuestionNum) call;
  const MarkingQuestionTypeDrawer(
      {required this.widthPercent,
      required this.currentTabs,
      required this.currentTab,
      required this.scaffoldKey,
      required this.call,
      super.key});

  @override
  _MarkingQuestionTypeDrawerState createState() => _MarkingQuestionTypeDrawerState();
}

class _MarkingQuestionTypeDrawerState extends ConsumerState<MarkingQuestionTypeDrawer> {
  @override
  Widget build(BuildContext context) {
    final double _width = MediaQuery.of(context).size.width * widget.widthPercent;
    return SafeArea(
      child: Drawer(
        width: _width,
        child: Container(
          height: double.infinity,
          // color: Colors.red,
          child: ListView.builder(
            itemBuilder: (context, index) {
              MarkingTextQuestionTab theBatchTab = widget.currentTabs[index];
              // bool taskCompleted = !getTagCondition(theBatchTab);
              // bool taskCompleted = theBatchTab.percent == 1 && theBatchTab.total == theBatchTab.finishCount;

              bool taskCompleted = theBatchTab.isFinished;
              if (theBatchTab.isExcess && theBatchTab.finishCount >= theBatchTab.total) {
                // 超量题
                taskCompleted = true;
              }

              return ListTile(
                onTap: () {
                  if (widget.currentTab == theBatchTab) return;

                  if (theBatchTab.total == 0) {
                    AchievementView(
                      elevation: 0.5,
                      duration: Duration(seconds: 1),
                      title: "提示",
                      subTitle: "当前试题Tag已经评阅完成。",
                      color: Theme.of(context).primaryColor,
                    ).show(context);
                    widget.scaffoldKey.currentState?.closeDrawer();
                    return;
                  }
                  widget.call(theBatchTab.questionNum);
                },
                tileColor: theBatchTab == widget.currentTab ? const Color.fromRGBO(46, 91, 255, 0.1) : null,
                leading: Icon(
                    IconData(taskCompleted ? 0xe606 : (theBatchTab.isExcess ? 0xe628 : 0xe627),
                        fontFamily: "AlibabaIcon"),
                    color: taskCompleted ? Theme.of(context).primaryColor : null),
                title: Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    quickText('第${theBatchTab.questionNum}题',
                        color: const Color.fromRGBO(45, 56, 76, 1), size: 19.sp, fontWeight: FontWeight.w500),
                    SizedBox(width: 3.w),
                    Padding(
                        padding: EdgeInsets.only(top: 2.h),
                        child: quickText(
                            // '${theBatchTab.finishCount}/${theBatchTab.isExcess ? (theBatchTab.total > theBatchTab.excessAvgCount ? theBatchTab.total : theBatchTab.excessAvgCount) : '${theBatchTab.total}'}',
                            '${theBatchTab.finishCount}/${theBatchTab.total}',
                            color: const Color.fromRGBO(148, 163, 182, 1),
                            size: 18.sp,
                            fontWeight: FontWeight.w400)),
                    // if (theBatchTab.isExcess) quickText(' 超量题', size: 10.sp, color: const Color.fromRGBO(148, 163, 182, 1))
                  ],
                ),
              );
            },
            itemCount: widget.currentTabs.length,
          ),
        ),
      ),
    );
  }
}
