import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/job/student_personal_info.dart';
import 'package:making_school_asignment_app/common/mixins/request_tool_mixin.dart';

import 'student_personal_state.dart';

class StudentPersonalLogic extends GetxController with RequestToolMixin {
  final StudentPersonalState state = StudentPersonalState();

  @override
  void onInit() {
    super.onInit();
    state.studentId = Get.arguments['studentId'];
    state.subject = Get.arguments['subject'] ?? -1;
    state.homeworkId = Get.arguments['homeworkId'];
    getInfo();
  }

  void getInfo() async {
    StudentPersonalInfo data = await getClient().getStudentHomework(state.homeworkId, state.studentId);
    data.kgtList.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());
      }
    });
    data.zgtList.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());
      }
    });
    state.studentInfo.value = data;
  }
}
