import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/job/annotated_class.dart';
import 'package:making_school_asignment_app/common/mixins/request_tool_mixin.dart';
import 'package:making_school_asignment_app/common/utils/toast_utils.dart';
import 'package:making_school_asignment_app/page/home_page/children/read_over/read_over_logic.dart';
import 'package:making_school_asignment_app/page/home_page/home_logic.dart';
import 'package:making_school_asignment_app/routes/app_pages.dart';

import 'annotate_class_state.dart';

class AnnotateClassLogic extends GetxController with RequestToolMixin {
  final AnnotateClassState state = AnnotateClassState();
  late final EasyRefreshController refreshController;
  final HomeLogic homeController = Get.find();
  late ReadOverLogic readOverController;

  @override
  void onInit() {
    super.onInit();
    state.preIndex = Get.arguments['tabIndex'] ?? 3;
    if (state.preIndex != 3) {
      // readOverController = Get.find();
    }
    refreshController = EasyRefreshController();
    state.homeworkId.value = Get.arguments['id'] ?? '';
    state.name.value = Get.arguments['name'] ?? '';
    state.grade = Get.arguments['grade'];
    state.subject = Get.arguments['subject'];
    state.completed.value = Get.arguments['completed'] ?? false;

    EasyLoading.show(status: 'loading...');
    getList();
  }

  void getList() async {
    List<AnnotatedClass> data = await getClient().getAnnotatedClassList(state.homeworkId.value);
    state.classList.value = data;
    /*  print('state.classList[0]=${state.classList[0].finishTime}');
    print('state.classList[1]=${state.classList[1].finishTime}');*/
    for (var element in state.classList.value) {
      int commitStudentCount = 0;
      int noCommitStudentCount = 0;
      for (var student in element.students) {
        if (student.state == 0) {
          noCommitStudentCount++;
        } else {
          commitStudentCount++;
        }
      }
      element.commitStudentCount = commitStudentCount;
      element.noCommitStudentCount = noCommitStudentCount;
    }

    EasyLoading.dismiss();
  }

  //一键批阅
  void getAllCorrect(classId) async {
    EasyLoading.show(status: 'loading...');
    try {
      await getClient().getAllCorrect(state.homeworkId.value, classId).then((e) {
        getList();
      });
    } catch (e) {
      EasyLoading.dismiss();
      ToastUtils.showError('操作失败，请重试');
    }
  }

//结束批阅
  void getOverAnnotate(classId) async {
    EasyLoading.show(status: 'loading...');
    try {
      await getClient().overAnnotate(state.homeworkId.value, classId).then((e) {
        getList();
      });
    } catch (e) {
      EasyLoading.dismiss();
      ToastUtils.showError('操作失败，请重试');
    }
  }

  void goQuickDataCheck(item) {
    Get.toNamed(Routes.quickDataCheckPage, arguments: {
      'homeworkId': state.homeworkId.value,
      'classId': item.classId,
      'grade': state.grade,
      'className': item.className
    });
  }

  void gojobReport(item) {
    Get.toNamed(Routes.jobReportPage, arguments: {
      'title': state.name.value,
      'homeworkId': state.homeworkId.value,
      'grade': state.grade,
      'className': item.className
    });
  }

  @override
  void dispose() {
    super.dispose();
    refreshController.dispose();
  }

  /// 跳转到批阅作业页面
  /// [homeworkId]
  /// [homeworkName]
  /// [classId]
  /// [subject]
  void toGoReviewhomework({required homeworkId, required homeworkName, required classId, required subject}) async {
    // 等待 UI 模式切换生效
    await SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky, overlays: <SystemUiOverlay>[]);
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await Future.delayed(const Duration(milliseconds: 300));
      await Get.toNamed(Routes.reviewHomework, arguments: {
        'homeworkId': homeworkId,
        'homeworkName': homeworkName,
        'classId': classId,
        'subject': subject
      })?.then((value) {
        SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge, overlays: [SystemUiOverlay.top]);
      });
    });
  }
}
