import 'package:get/get.dart';
import 'package:vitalapp/architecture/defines.dart';
import 'package:vitalapp/database/entities/defines.dart';
import 'package:vitalapp/managers/interfaces/data_sync.dart';

import 'state.dart';

class PatientSyncController extends FControllerBase {
  final state = PatientSyncState();
  final _dataSyncManager = Get.find<IDataSyncManager>();

  Future<void> syncAll() async {
    // TODO: 先加载多点，需要调整为：进度+逐步加载
    const pageSize = 1000;
    int pageIndex = 1;
    // TODO: 还要包含上传失败的
    final pagedList = await _dataSyncManager.getPatientPagedList(
      pageIndex,
      pageSize: pageSize,
      syncState: OfflineDataSyncState.wait,
    );

    for (var i = 0; i < pagedList.totalCount; i++) {
      final entity = pagedList.data[i];
      _dataSyncManager.syncPatient(entity);
    }
  }

  Future<void> _loadList() async {
    final pagedList =
        await _dataSyncManager.getPatientPagedList(state.pageIndex);
    state.totalCount = pagedList.totalCount;
    state.dataList = pagedList.data;
  }

  @override
  void onReady() {
    super.onReady();
    _loadList();
  }
}
