import 'package:flutter/material.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/student_history.dart';
import 'package:making_school_asignment_app/common/job/student_history_params.dart';
import 'package:making_school_asignment_app/common/mixins/request_tool_mixin.dart';

import 'student_work_detail_state.dart';

class StudentWorkDetailLogic extends GetxController with RequestToolMixin, GetSingleTickerProviderStateMixin {
  final StudentWorkDetailState state = StudentWorkDetailState();
  late TabController tabController;
  late final EasyRefreshController refreshController;

  @override
  void onInit() {
    super.onInit();
    state.studentName.value = Get.arguments['studentName'] ?? '';
    state.studentId = Get.arguments['studentId'] ?? -1;
    state.subject = Get.arguments['subject'] ?? -1;
    tabController = TabController(length: 3, vsync: this);
    refreshController = EasyRefreshController();
    EasyLoading.show(status: 'loading...');
    getList();
  }

  void getList() async {
    StudentHistoryParams params = StudentHistoryParams(
      assessType: state.isJob.value ? 0 : 1,
      studentId: state.studentId,
      dateStart: state.dateStart,
      dateEnd: state.dateEnd,
      subject: state.subject,
      pageNumber: state.page,
      pageSize: 10,
    );
    StudentHistory data = await getClient().getStudentHistory(params);
    data.items.items.forEach((e) {
      e.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());
        }
      });
      e.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.studentData.value = data;
    /*  state.kgtLine.value = [{'id':0,'rate':0}];
    state.zgtLine.value = [{'id':0,'rate':0}];
    state.allLine.value = [{'id':0,'rate':0}];*/
    state.kgtLine.value = [];
    state.zgtLine.value = [];
    state.allLine.value = [];
    for (var i = 0; i < state.studentData.value.items.items.length; i++) {
      var element = state.studentData.value.items.items[i];
      int num = 0;
      for (var item in element.kgtList) {
        if (item.state == 0) {
          num = num + 1;
        }
      }
      for (var subject in element.zgtList) {
        if (subject.state == 0) {
          num = num + 1;
        }
      }
      if (num == (element.kgtList.length + element.zgtList.length)) {
        element.allNotDone = true;
      }

      state.kgtLine.add({'id': i + 1, 'rate': (element.kgtCorrectRate / 100 * 5).toStringAsFixed(2)});
      state.zgtLine.add({'id': i + 1, 'rate': (element.zgtCorrectRate / 100 * 5).toStringAsFixed(2)});
      state.allLine.add({'id': i + 1, 'rate': (element.correctRate / 100 * 5).toStringAsFixed(2)});
    }

    EasyLoading.dismiss();
  }

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