/**
   * Returns a Page of DocumentPackages, that last updated in time range, which represents a
   * paginated query response. Important once you have many DocumentPackages.
   *
   * @param status Returned DocumentPackages must have their status set to this value to be included
   *     in the result set
   * @param request Identifying which page of results to return
   * @param from Date range starting from this date included
   * @param to Date range ending of this date included
   * @return List of DocumentPackages that populate the specified page
   */
  public Page<DocumentPackage> getUpdatedPackagesWithinDateRange(
      PackageStatus status, PageRequest request, Date from, Date to) {
    String fromDate = DateHelper.dateToIsoUtcFormat(from);
    String toDate = DateHelper.dateToIsoUtcFormat(to);

    String path =
        template
            .urlFor(UrlTemplate.PACKAGE_LIST_STATUS_DATE_RANGE_PATH)
            .replace("{status}", new PackageStatusConverter(status).toAPIPackageStatus())
            .replace("{from}", Integer.toString(request.getFrom()))
            .replace("{to}", Integer.toString(request.to()))
            .replace("{lastUpdatedStartDate}", fromDate)
            .replace("{lastUpdatedEndDate}", toDate)
            .build();

    try {
      String response = client.get(path);
      Result<Package> results =
          JacksonUtil.deserialize(response, new TypeReference<Result<Package>>() {});
      return convertToPage(results, request);
    } catch (RequestException e) {
      throw new EslServerException("Could not get package list.", e);
    } catch (Exception e) {
      e.printStackTrace();
      throw new EslException("Could not get package list. Exception: " + e.getMessage());
    }
  }
  /**
   * Get the document's metadata from the package.
   *
   * @param documentPackage The DocumentPackage we want to get document from.
   * @param documentId Id of document to get.
   * @return the document's metadata
   */
  public com.silanis.esl.sdk.Document getDocumentMetadata(
      DocumentPackage documentPackage, String documentId) {
    String path =
        template
            .urlFor(UrlTemplate.DOCUMENT_ID_PATH)
            .replace("{packageId}", documentPackage.getId().getId())
            .replace("{documentId}", documentId)
            .build();

    try {
      String response = client.get(path);
      Document apilDocument = Serialization.fromJson(response, Document.class);

      // Wipe out the members not related to the metadata
      apilDocument.setApprovals(new ArrayList<Approval>());
      apilDocument.setFields(new ArrayList<Field>());
      apilDocument.setPages(new ArrayList<com.silanis.esl.api.model.Page>());

      return new DocumentConverter(
              apilDocument, new DocumentPackageConverter(documentPackage).toAPIPackage())
          .toSDKDocument();
    } catch (RequestException e) {
      throw new EslServerException("Could not get the document's metadata.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not get the document's metadata." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Get Journal Entries.
   *
   * @param userId The ID of the user whose e-journal entries are being retrieved.
   * @return all of the user's notary e-journal entries.
   */
  public List<com.silanis.esl.sdk.NotaryJournalEntry> getJournalEntries(String userId) {
    List<com.silanis.esl.sdk.NotaryJournalEntry> result =
        new ArrayList<com.silanis.esl.sdk.NotaryJournalEntry>();
    String path =
        template.urlFor(UrlTemplate.NOTARY_JOURNAL_PATH).replace("{userId}", userId).build();

    try {
      String stringResponse = client.get(path);
      Result<com.silanis.esl.api.model.NotaryJournalEntry> apiResponse =
          JacksonUtil.deserialize(
              stringResponse,
              new TypeReference<Result<com.silanis.esl.api.model.NotaryJournalEntry>>() {});
      for (com.silanis.esl.api.model.NotaryJournalEntry apiNotaryJournalEntry :
          apiResponse.getResults()) {
        result.add(
            new NotaryJournalEntryConverter(apiNotaryJournalEntry).toSDKNotaryJournalEntry());
      }
      return result;

    } catch (RequestException e) {
      throw new EslException("Could not get Journal Entries.", e);
    } catch (Exception e) {
      throw new EslException("Could not get Journal Entries." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Create Fast Track Package.
   *
   * @param packageId The id of the package to start FastTrack
   * @param signers The signers to get the signing url
   * @return The signing url
   */
  public String startFastTrack(PackageId packageId, List<FastTrackSigner> signers) {
    String token = getFastTrackToken(packageId, true);
    String path =
        template.urlFor(UrlTemplate.START_FAST_TRACK_PATH).replace("{token}", token).build();

    List<FastTrackRole> roles = new ArrayList<FastTrackRole>();
    for (FastTrackSigner signer : signers) {
      FastTrackRole role =
          FastTrackRoleBuilder.newRoleWithId(signer.getId())
              .withName(signer.getId())
              .withSigner(signer)
              .build();
      roles.add(role);
    }

    String json = Serialization.toJson(roles);
    try {
      String response = client.post(path, json);
      SigningUrl signingUrl = Serialization.fromJson(response, SigningUrl.class);
      return signingUrl.getUrl();
    } catch (RequestException e) {
      throw new EslException("Could not start fast track.", e);
    } catch (Exception e) {
      throw new EslException("Could not start fast track." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Get Journal Entries in csv format.
   *
   * @param userId The ID of the user whose e-journal entries are being retrieved.
   * @return all of the user's notary e-journal entries in csv format.
   */
  public DownloadedFile getJournalEntriesAsCSV(String userId) {
    String path =
        template.urlFor(UrlTemplate.NOTARY_JOURNAL_CSV_PATH).replace("{userId}", userId).build();

    try {
      return client.getBytes(path);
    } catch (RequestException e) {
      throw new EslException("Could not get Journal Entries in csv.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not get Journal Entries in csv." + " Exception: " + e.getMessage());
    }
  }
 /**
  * Unlock a signer which has been locked out due to too many failed authentication attempts.
  *
  * @param signerId If not null, the id of the signer who's status we are to retrieve
  */
 public void unlockSigner(PackageId packageId, String signerId) {
   String path =
       template
           .urlFor(UrlTemplate.ROLE_UNLOCK_PATH)
           .replace("{packageId}", packageId.getId())
           .replace("{roleId}", signerId)
           .build();
   try {
     client.post(path, null);
   } catch (RequestException e) {
     throw new EslException("Could not unlock signer.", e);
   } catch (Exception e) {
     throw new EslException("Could not unlock signer." + " Exception: " + e.getMessage());
   }
 }
  /**
   * Deletes the specified package.
   *
   * @param packageId The id of the package to be deleted
   */
  public void deletePackage(PackageId packageId) {
    String path =
        template
            .urlFor(UrlTemplate.PACKAGE_ID_PATH)
            .replace("{packageId}", packageId.getId())
            .build();

    try {
      client.delete(path);
    } catch (RequestException e) {
      throw new EslServerException("Unable to delete package.", e);
    } catch (Exception e) {
      e.printStackTrace();
      throw new EslException("Unable to delete package. Exception: " + e.getMessage());
    }
  }
 /**
  * Removes a signer from a package
  *
  * @param packageId The id of the package containing the signer to be deleted
  * @param signerId The role id of the signer to be deleted
  */
 public void removeSigner(PackageId packageId, String signerId) {
   String path =
       template
           .urlFor(UrlTemplate.SIGNER_PATH)
           .replace("{packageId}", packageId.getId())
           .replace("{roleId}", signerId)
           .build();
   try {
     client.delete(path);
     return;
   } catch (RequestException e) {
     throw new EslServerException("Could not delete signer.", e);
   } catch (Exception e) {
     throw new EslException("Could not delete signer." + " Exception: " + e.getMessage());
   }
 }
  private void sendSmsToSigner(PackageId packageId, Role role) {
    String path =
        template
            .urlFor(UrlTemplate.SEND_SMS_TO_SIGNER_PATH)
            .replace("{packageId}", packageId.getId())
            .replace("{roleId}", role.getId())
            .build();

    try {
      client.post(path, null);
    } catch (RequestException e) {
      throw new EslException("Could not send SMS to the signer.", e);
    } catch (Exception e) {
      throw new EslException("Could not send SMS to the signer." + " Exception: " + e.getMessage());
    }
  }
  private void notifySigner(PackageId packageId, String roleId) {
    String path =
        template
            .urlFor(UrlTemplate.NOTIFY_ROLE_PATH)
            .replace("{packageId}", packageId.getId())
            .replace("{roleId}", roleId)
            .build();

    try {
      client.post(path, null);
    } catch (RequestException e) {
      throw new EslServerException("Could not send email notification.", e);
    } catch (Exception e) {
      throw new EslException("Could not send email notification.  " + e.getMessage());
    }
  }
  /**
   * Archive the specified package.
   *
   * @param packageId The id of the package to be archived
   */
  public void archive(PackageId packageId) {
    String path =
        template
            .urlFor(UrlTemplate.PACKAGE_ID_PATH)
            .replace("{packageId}", packageId.getId())
            .build();

    String json = "{\"status\":\"ARCHIVED\"}";
    try {
      client.post(path, json);
    } catch (RequestException e) {
      throw new EslServerException("Unable to archive the package.", e);
    } catch (Exception e) {
      e.printStackTrace();
      throw new EslException("Unable to archive the package. Exception: " + e.getMessage());
    }
  }
  /**
   * Get thank you dialog content.
   *
   * @param packageId The id of the package to get thank you dialog content.
   * @return thank you dialog content
   */
  public String getThankYouDialogContent(PackageId packageId) {
    String path =
        template
            .urlFor(UrlTemplate.THANK_YOU_DIALOG_PATH)
            .replace("{packageId}", packageId.getId())
            .build();

    try {
      String json = client.get(path);
      Properties thankYouDialogContent = Serialization.fromJson(json, Properties.class);
      return thankYouDialogContent.getProperty("body");
    } catch (RequestException e) {
      throw new EslException("Could not get thank you dialog content.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not get thank you dialog content." + " Exception: " + e.getMessage());
    }
  }
  private String getFastTrackUrl(PackageId packageId, Boolean signing) {
    String path =
        template
            .urlFor(UrlTemplate.FAST_TRACK_URL_PATH)
            .replace("{packageId}", packageId.getId())
            .replace("{signing}", signing.toString())
            .build();

    try {
      String json = client.get(path);
      SigningUrl signingUrl = Serialization.fromJson(json, SigningUrl.class);
      return signingUrl.getUrl();
    } catch (RequestException e) {
      throw new EslException("Could not get a fastTrack url.", e);
    } catch (Exception e) {
      throw new EslException("Could not get a fastTrack url." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Upload documents with external content to the package.
   *
   * @param packageId
   */
  public void addDocumentWithExternalContent(
      String packageId, List<com.silanis.esl.sdk.Document> providerDocuments) {
    String path =
        template.urlFor(UrlTemplate.DOCUMENT_PATH).replace("{packageId}", packageId).build();

    List<Document> apiDocuments = new ArrayList<Document>();
    for (com.silanis.esl.sdk.Document document : providerDocuments) {
      apiDocuments.add(new DocumentConverter(document).toAPIDocumentMetadata());
    }
    try {
      String json = Serialization.toJson(apiDocuments);
      client.post(path, json);
    } catch (RequestException e) {
      throw new EslServerException("Could not upload the documents.", e);
    } catch (Exception e) {
      throw new EslException("Could not upload the documents." + " Exception: " + e.getMessage());
    }
  }
  private String getSigningUrl(PackageId packageId, Role role) {

    String path =
        template
            .urlFor(UrlTemplate.SIGNER_URL_PATH)
            .replace("{packageId}", packageId.getId())
            .replace("{roleId}", role.getId())
            .build();

    try {
      String response = client.get(path);
      SigningUrl signingUrl = Serialization.fromJson(response, SigningUrl.class);
      return signingUrl.getUrl();
    } catch (RequestException e) {
      throw new EslException("Could not get a signing url.", e);
    } catch (Exception e) {
      throw new EslException("Could not get a signing url." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Get package support configuration.
   *
   * @param packageId The id of the package to get package support configuration.
   * @return package support configuration
   */
  public SupportConfiguration getConfig(PackageId packageId) {
    String path =
        template
            .urlFor(UrlTemplate.PACKAGE_INFORMATION_CONFIG_PATH)
            .replace("{packageId}", packageId.getId())
            .build();

    try {
      String json = client.get(path);
      com.silanis.esl.api.model.SupportConfiguration apiSupportConfiguration =
          Serialization.fromJson(json, com.silanis.esl.api.model.SupportConfiguration.class);
      return new SupportConfigurationConverter(apiSupportConfiguration).toSDKSupportConfiguration();
    } catch (RequestException e) {
      throw new EslException("Could not get support configuration.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not get support configuration." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Get a signer from the specified package
   *
   * @param packageId The id of the package in which to get the signer
   * @param signerId The id of signer to get
   * @return The signer
   */
  public com.silanis.esl.sdk.Signer getSigner(PackageId packageId, String signerId) {
    String path =
        template
            .urlFor(UrlTemplate.SIGNER_PATH)
            .replace("{packageId}", packageId.getId())
            .replace("{roleId}", signerId)
            .build();

    try {
      String response = client.get(path);
      Role apiRole = Serialization.fromJson(response, Role.class);
      return new SignerConverter(apiRole).toSDKSigner();

    } catch (RequestException e) {
      throw new EslServerException("Could not get signer.", e);
    } catch (Exception e) {
      throw new EslException("Could not get signer." + " Exception: " + e.getMessage());
    }
  }
  public Page<DocumentPackage> getTemplates(PageRequest request) {
    String path =
        template
            .urlFor(UrlTemplate.TEMPLATE_LIST_PATH)
            .replace("{from}", Integer.toString(request.getFrom()))
            .replace("{to}", Integer.toString(request.to()))
            .build();

    try {
      String response = client.get(path);
      Result<Package> results =
          JacksonUtil.deserialize(response, new TypeReference<Result<Package>>() {});

      return convertToPage(results, request);
    } catch (RequestException e) {
      throw new EslServerException("Could not get template list.", e);
    } catch (Exception e) {
      throw new EslException("Could not get template list. Exception: " + e.getMessage());
    }
  }
  /**
   * Updates a signer's information from a package
   *
   * @param packageId The id of the package containing the signer to be updated
   * @param signer The signer with the updated information
   */
  public void updateSigner(PackageId packageId, com.silanis.esl.sdk.Signer signer) {
    Role apiPayload =
        new SignerConverter(signer).toAPIRole(UUID.randomUUID().toString().replace("-", ""));

    String path =
        template
            .urlFor(UrlTemplate.SIGNER_PATH)
            .replace("{packageId}", packageId.getId())
            .replace("{roleId}", signer.getId())
            .build();

    try {
      String json = Serialization.toJson(apiPayload);
      client.put(path, json);
    } catch (RequestException e) {
      throw new EslException("Could not update signer.", e);
    } catch (Exception e) {
      throw new EslException("Could not update signer." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Updates the signer order in a package.
   *
   * @param documentPackage The id of the package to update signer order
   */
  public void orderSigners(DocumentPackage documentPackage) {
    String path =
        template
            .urlFor(UrlTemplate.ROLE_PATH)
            .replace("{packageId}", documentPackage.getId().getId())
            .build();

    List<Role> roles = new ArrayList<Role>();
    for (com.silanis.esl.sdk.Signer signer : documentPackage.getSigners()) {
      roles.add(new SignerConverter(signer).toAPIRole(signer.getId()));
    }

    try {
      String json = Serialization.toJson(roles);
      client.put(path, json);
    } catch (RequestException e) {
      throw new EslServerException("Could not order signers.", e);
    } catch (Exception e) {
      throw new EslException("Could not order signers." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Notifies the specified signer by email, including a custom message.
   *
   * @param packageId The id of the package containing the signer to be notified
   * @param signerEmail The email of the signer to be notified
   * @param message The custom message to be included in the email sent as notification to the
   *     signer
   */
  public void notifySigner(PackageId packageId, String signerEmail, String message) {
    String path =
        template
            .urlFor(UrlTemplate.CUSTOM_NOTIFICATIONS_PATH)
            .replace("{packageId}", packageId.getId())
            .build();

    Map<String, Object> jsonMap = new HashMap<String, Object>();
    jsonMap.put("email", signerEmail);
    jsonMap.put("message", message);

    try {
      String payload = JacksonUtil.serialize(jsonMap);
      client.post(path, payload);
    } catch (RequestException e) {
      throw new EslException("Could not send email notification to signer.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not send email notification to signer. Exception: " + e.getMessage());
    }
  }
  /**
   * Updates the documents signing order
   *
   * @param documentPackage
   */
  public void orderDocuments(DocumentPackage documentPackage) {
    String path =
        template
            .urlFor(UrlTemplate.DOCUMENT_PATH)
            .replace("{packageId}", documentPackage.getId().getId())
            .build();

    List<Document> documents = new ArrayList<Document>();
    for (com.silanis.esl.sdk.Document document : documentPackage.getDocuments()) {
      documents.add(new DocumentConverter(document).toAPIDocumentMetadata());
    }
    try {
      String json = Serialization.toJson(documents);

      client.put(path, json);
    } catch (RequestException e) {
      throw new EslServerException("Could not order the documents.", e);
    } catch (Exception e) {
      throw new EslException("Could not order the documents." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Adds a signer to the specified package
   *
   * @param packageId The id of the package in which the signer will be added
   * @param signer The signer to be added
   * @return The role id of the signer
   */
  public String addSigner(PackageId packageId, com.silanis.esl.sdk.Signer signer) {
    Role apiPayload =
        new SignerConverter(signer).toAPIRole(UUID.randomUUID().toString().replace("-", ""));

    String path =
        template
            .urlFor(UrlTemplate.ADD_SIGNER_PATH)
            .replace("{packageId}", packageId.getId())
            .build();

    try {
      String json = Serialization.toJson(apiPayload);
      String response = client.post(path, json);
      Role apiRole = Serialization.fromJson(response, Role.class);
      return apiRole.getId();

    } catch (RequestException e) {
      throw new EslServerException("Could not add signer.", e);
    } catch (Exception e) {
      throw new EslException("Could not add signer." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Updates the document's metadata from the package.
   *
   * @param documentPackage
   * @param document
   */
  public void updateDocumentMetadata(
      DocumentPackage documentPackage, com.silanis.esl.sdk.Document document) {
    String path =
        template
            .urlFor(UrlTemplate.DOCUMENT_ID_PATH)
            .replace("{packageId}", documentPackage.getId().getId())
            .replace("{documentId}", document.getId().toString())
            .build();

    Document internalDoc = new DocumentConverter(document).toAPIDocumentMetadata();

    try {
      String json = Serialization.toJson(internalDoc);

      client.put(path, json);
    } catch (RequestException e) {
      throw new EslServerException("Could not update the document's metadata.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not update the document's metadata." + " Exception: " + e.getMessage());
    }
  }