import 'dart:convert';

import 'package:fis_jsonrpc/rpc.dart';
import 'package:vitalapp/architecture/network_connectivity.dart';
import 'package:vitalapp/database/db.dart';
import 'package:vitalapp/managers/adapters/offline/patient.dart';
import 'package:vitalapp/managers/interfaces/models/patient_model_dto.dart';
import 'package:vitalapp/managers/interfaces/patient.dart';
import 'package:vitalapp/rpc.dart';
import 'package:fis_common/logger/logger.dart';
import 'package:vitalapp/store/store.dart';

class PatientManager implements IPatientManager {
  @override
  Future<String?> create(CreatePatientRequest request) async {
    try {
      request.token = Store.user.token;
      request.code = request.cardNo;
      final result = await rpc.patient.createPatientAsync(request);
      return result;
    } catch (e) {
      logger.e("PatientManager create patient error.", e);
      return null;
    }
  }

  @override
  Future<PatientDTO?> getDetail(String code) async {
    try {
      final request = GetPatientRequest(code: code, token: Store.user.token);
      final result = await rpc.patient.getPatientDetailAsync(request);
      return result;
    } catch (e) {
      logger.e("PatientManager query patient detail error.", e);
      return null;
    }
  }

  @override
  Future<PageCollection<PatientModelDTO>?> getPagedList(
      PatientPageRequest request) async {
    PageCollection<PatientModelDTO>? listPatientDto =
        PageCollection<PatientModelDTO>();
    try {
      request.token = Store.user.token;
      final result = await rpc.patient.getPatientPageAsync(request);
      listPatientDto.dataCount = result.dataCount;
      listPatientDto.pageData = [];
      for (var element in result.pageData!) {
        var patientModel = PatientModelDTO.fromJson(element.toJson());
        var isNotUploadedPatient =
            await db.repositories.patient.isNotUploadedPatient(element.code!);
        if (isNotUploadedPatient) {
          patientModel.isExistLocalData = true;
        } else {
          final localCount = await db.repositories.diagnosis
              .getNotUploadedCountByPatientCode(element.code!);
          if (localCount > 0) {
            patientModel.isExistLocalData = true;
          }
        }
        listPatientDto.pageData?.add(patientModel);
      }
      return listPatientDto;
    } catch (e) {
      logger.e("PatientManager query patient paged list error.", e);
      return null;
    }
  }

  @override
  Future<bool> updatePatientAsync(UpdatePatientRequest request) async {
    try {
      final result = await rpc.patient.updatePatientAsync(request);
      return result;
    } catch (e) {
      return false;
    }
  }

  @override
  Future<bool> setCrowdLabelsAsync(
      String patientCode, List<String> crowLabels) async {
    try {
      final request = SetCrowdLabelsRequest(
          code: patientCode, crowdLabels: crowLabels, token: Store.user.token);
      final result = rpc.patient.setCrowdLabelsAsync(request);
      return result;
    } catch (e) {
      return false;
    }
  }

  @override
  Future<String> createPatientExtensionAsync(
      CreatePatientExtensionRequest request) async {
    try {
      final result =
          await rpc.patientExtension.createPatientExtensionAsync(request);
      return result;
    } catch (e) {
      return '';
    }
  }

  @override
  Future<PatientExtensionDTO?> getPatientExtensionDetailByCodeAndKeyAsync(
      GetPatientExtensionByCodeAndKeyRequest request) async {
    try {
      final result = await rpc.patientExtension
          .getPatientExtensionDetailByCodeAndKeyAsync(request);
      return result;
    } catch (e) {
      return null;
    }
  }

  @override

  ///更新居民健康信息
  Future<bool> updatePatientExtensionAsync(
      UpdatePatientExtensionRequest request) async {
    try {
      final result =
          await rpc.patientExtension.updatePatientExtensionAsync(request);
      return result;
    } catch (e) {}
    return false;
  }
}
