import 'dart:convert';

import 'package:fis_jsonrpc/rpc.dart';
import 'package:vitalapp/managers/interfaces/follow_up.dart';
import 'package:vitalapp/rpc.dart';
import 'package:vitalapp/store/store.dart';
import 'package:fis_common/logger/logger.dart';

class FollowUpManager implements IFollowUpManager {
  @override
  Future<String> createFollowUp(CreateFollowUpRequest examRequest) async {
    try {
      examRequest.token = Store.user.token;
      final examCode = await rpc.followUp.createFollowUpAsync(
        examRequest,
      );
      return examCode;
    } catch (e) {
      logger.e("DoctorManager sync user info error.", e);
      return '';
    }
  }

  @override
  Future<bool?> updateFollowUp(UpdateFollowUpRequest followRequest) async {
    try {
      followRequest.token = Store.user.token;
      final result = await rpc.followUp.updateFollowUpAsync(
        followRequest,
      );
      print(jsonEncode(followRequest.toJson()));
      return result;
    } catch (e) {
      logger.e("FollowUpManager sync user info error.", e);
      return null;
    }
  }

  @override
  Future<List<FollowUpDTO>?> getFollowUpPage(
      FollowUpPageRequest followUpPageRequest) async {
    try {
      followUpPageRequest.token = Store.user.token;
      final result = await rpc.followUp.getFollowUpPageAsync(
        followUpPageRequest,
      );
      return result.pageData;
    } catch (e) {
      logger.e("DoctorManager sync user info error.", e);
      return null;
    }
  }

  @override
  Future<List<FollowUpRecordDTO>?> getFollowUpRecordList(
      List<String> keys, String patientCode) async {
    try {
      var request = GetFollowUpRecordListRequest(
          keys: keys, patientCode: patientCode, token: Store.user.token);
      final result = await rpc.followUp.getFollowUpRecordListAsync(request);
      return result;
    } catch (e) {
      return null;
    }
  }

  @override
  Future<List<FollowUpRecordDTO>?> getFollowUpRecordListByYearAsync(
      GetFollowUpRecordListByYearRequest
          followUpRecordListByYearRequest) async {
    try {
      followUpRecordListByYearRequest.token = Store.user.token;
      final result = await rpc.followUp
          .getFollowUpRecordListByYearAsync(followUpRecordListByYearRequest);
      print(followUpRecordListByYearRequest.toJson());
      return result;
    } catch (e) {
      return null;
    }
  }
}
