import 'dart:async';

import 'package:fis_common/logger/logger.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:flyinsono/pages/remedical/consultation/controller.dart';
import 'package:flyinsono/pages/remedical/consultation/state.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/rpc.dart';
import 'package:get/get.dart';

/// 扫查图片的控制器
class ScanningImagecontroller extends FControllerBase {
  late ConsultationController controller;
  late FConsultationState state;

  /// 测量图片组
  late List<RemedicalInfoDTO> remedicalItemList = [];

  ScanningImagecontroller(ConsultationController controller) {
    this.controller = controller;
    this.state = controller.state;
  }

  /// 下一页 获取当前检查的图片组
  Future<void> nextPage() async {
    state.imageScannedIndex++;
    final recordInfo = state.imageScanned[state.imageScannedIndex];
    await controller
        .findRemedicalMeasuredInfoAsync(recordInfo.recordCode ?? '');
  }

  /// 上一页 获取当前检查的图片组
  Future<void> lastPage() async {
    state.imageScannedIndex--;
    final recordInfo = state.imageScanned[state.imageScannedIndex];
    await controller
        .findRemedicalMeasuredInfoAsync(recordInfo.recordCode ?? '');
  }

  ///获取所有图像
  Future<PageResult<RemedicalListResult>> getRemedicalListPages() async {
    try {
      final result = await rpc.remedical.getRemedicalListPagesAsync(
        GetRecordsPageRequest(
          token: Store.user.token,
          patientCode: state.patientCode,
        ),
      );
      return result;
    } catch (e) {
      return PageResult<RemedicalListResult>();
    }
  }

  /// 获取扫查图片
  Future<void> getRemedicalListPagesAsync() async {
    try {
      final result = state.patientTab == 0
          ? await getRemedicalListPages()
          : await controller.referPatientInfoController
              .queryReferralRemedicalListPagesAsync();
      state.imageScanned = result.pageData ?? [];

      /// 测量图片获取
      if (state.imageScanned.length > 0) {
        state.imageScannedIndex = 0;
        state.imageScanned[state.imageScannedIndex].remedicalItemList!.forEach(
          (element) {
            remedicalItemList.addAll(element.remedicalList!);
          },
        );
      }
    } catch (e) {
      printError(info: "getRemedicalListPagesAsync exception:" + e.toString());
      logger.e("getRemedicalListPagesAsync exception:", e);
    }
  }

  /// 获取测量图像
  Future<void> findRemedicalMeasuredInfoAsync(String recordCode) async {
    try {
      state.remedicalMeasured = [];
      final result = await rpc.recordInfo.findRemedicalMeasuredInfoAsync(
        FindRemedicalMeasuredInfoRequest(
          token: Store.user.token,
          recordCode: recordCode,
        ),
      );
      state.remedicalMeasured = result;
    } catch (e) {
      state.remedicalMeasured = [];
      printError(
          info: "findRemedicalMeasuredInfoAsync exception:" + e.toString());
      logger.e("findRemedicalMeasuredInfoAsync exception:", e);
    }
  }
}
