import 'package:fis_jsonrpc/rpc.dart';
import 'package:vitalapp/managers/interfaces/contract.dart';
import 'package:vitalapp/rpc.dart';
import 'package:vitalapp/store/store.dart';

class ContractManager implements IContractManager {
  @override
  Future<String?> createContractRecordAsync(
      CreateContractRecordRequest request) async {
    try {
      request.token = Store.user.token;
      final result =
          await rpc.contractRecord.createContractRecordAsync(request);
      return result;
    } catch (e) {
      // logger.e("PatientManager create patient error.", e);
      return null;
    }
  }

  @override
  Future<PageCollection<ContractRecordDTO>?>
      getContractRecordPageByPatientCodeAsync(String patientCode) async {
    try {
      final result = await rpc.contractRecord
          .getContractRecordPageByPatientCodeAsync(
              GetContractRecordPageByPatientCodeRequest(
                  patientCode: patientCode,
                  token: Store.user.token,
                  pageSize: 20,
                  pageIndex: 1));
      return result;
    } catch (e) {
      return null;
    }
  }

  @override
  Future<PageCollection<ContractRecordDTO>?>
      getContractRecordPageAsync() async {
    try {
      final result = await rpc.contractRecord.getContractRecordPageAsync(
          ContractRecordPageRequest(
              token: Store.user.token, pageSize: 20, pageIndex: 1));
      return result;
    } catch (e) {
      return null;
    }
  }

  @override
  Future<ContractRecordDTO?> getContractRecordDetailAsync(
      String patientCode) async {
    try {
      var request = GetContractRecordRequest(
        code: patientCode,
        token: Store.user.token,
      );
      final result =
          await rpc.contractRecord.getContractRecordDetailAsync(request);
      return result;
    } catch (e) {
      return null;
    }
  }

  @override
  Future<bool> updateContractRecordStateAsync(
      String? code, ContractStateEnum? contractState, String? reason) async {
    try {
      var request = UpdateContractRecordStateRequest(
        code: code,
        contractState: contractState,
        reason: reason,
        token: Store.user.token,
      );
      final result =
          await rpc.contractRecord.updateContractRecordStateAsync(request);
      return result;
    } catch (e) {}
    return false;
  }

  @override
  Future<bool> removeContractRecordAsync(String code) async {
    try {
      var request = RemoveContractRecordRequest(
        code: code,
        token: Store.user.token,
      );
      final result =
          await rpc.contractRecord.removeContractRecordAsync(request);
      return result;
    } catch (e) {}
    return false;
  }

  @override
  Future<String> renewalContractRecordAsync(String code) async {
    try {
      var request = RenewalContractRecordRequest(
        token: Store.user.token,
        code: code,
        serviceStartDate: DateTime.now(),
        serviceEndDate: DateTime.now().add(const Duration(days: 365)),
      );
      final result =
          await rpc.contractRecord.renewalContractRecordAsync(request);
      return result;
    } catch (e) {}
    return "";
  }
}
