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/student_item.dart';
import 'package:making_school_asignment_app/common/mixins/request_tool_mixin.dart';

import 'class_student_state.dart';

class ClassStudentLogic extends GetxController with RequestToolMixin {
  final ClassStudentState state = ClassStudentState();
  late final EasyRefreshController refreshController;

  @override
  void onInit() {
    super.onInit();
    state.title.value = Get.arguments['title'] ?? '';
    state.classId = Get.arguments['classId'] ?? '';
    final subject = Get.arguments['subject'];
    state.subject = subject == 0 ? null : subject;
    state.page = Get.arguments['page'] ?? '';
    EasyLoading.show(status: 'loading...');
    refreshController = EasyRefreshController();
    getList();
  }

  void getList() async {
    List<StudentItem> res = await getClient().getStudentList(state.classId, state.subject);
    state.studentList.value = res;
    state.studentList.sort((a, b) => a.priorityAnnotate ? -1 : 1);
    EasyLoading.dismiss();
    refreshController.finishRefresh();
  }

  void setJobReadLevel(studentId, priorityAnnotate) async {
    if (state.isClicking) {
      state.isClicking = false;
      await getClient().getAnnotateStudent(state.classId, studentId, priorityAnnotate, state.subject);
      state.isClicking = true;
      getList();
    }
  }

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