import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:marking_app/common/mixin/common.dart';
import 'package:marking_app/common/model/common/base_structure_result.dart';
import 'package:marking_app/common/model/enum/marking_list_type.dart';
import 'package:marking_app/common/model/marking/marking_statistics.dart';
import 'package:marking_app/utils/request/rest_client.dart';

class UseMarkingStatistics with CommonMixin {
  final List<String> typeNames = ['阅卷', '仲裁', '异常'];
  List<MarkingListType> theTypes;
  ValueNotifier<MarkingListType> currentType; // 设置主页
  ValueNotifier<Map<MarkingListType, int>> statistics; // 统计数据

  UseMarkingStatistics._({required this.theTypes, required this.statistics, required this.currentType});

  factory UseMarkingStatistics.use({required int pageType}) {
    List<MarkingListType> _theTypes = MarkingListType.values;

    return UseMarkingStatistics._(
      theTypes: _theTypes.toList(),
      currentType: useState(_theTypes[pageType]),
      statistics: useState({}),
    );
  }

  getData() async {
    RestClient client = await getClient();
    Map<MarkingListType, int> map = {};
    BaseStructureResult<List<MarkingStatistics>> result = await client.getMarkingStatistics();
    if (result.success && result.data != null) {
      // [ { 0,"正常"}, { 1,"仲裁"},{ 2,"异常"}]
      List<MarkingListType> _theTypes = MarkingListType.values;
      result.data!.forEach((e) => map[_theTypes[e.navld]] = e.total);
    }
    statistics.value = map;
  }
}
