package com.airlock.iam.admin.application.service.token.vasco;

import com.airlock.iam.admin.application.configuration.vasco.VascoLetterGenerator;
import com.airlock.iam.admin.infrastructure.exception.VascoOtpValidationHelper;
import com.airlock.iam.base.api.domain.model.exception.FailureException;
import com.airlock.iam.common.api.domain.service.email.EmailServiceException;
import com.airlock.iam.core.misc.impl.token.VascoToken;
import com.airlock.iam.core.misc.tokenservice.VascoTokenService;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;

public class VascoOtpDirectLetterPrint {
   private final VascoLetterGenerator letterGenerator;
   private final VascoTokenService tokenService;
   private final VascoOtpPersistencyService persistencyService;

   public void directPrintLetter(String userId, VascoOtpDeviceId deviceId) {
      VascoToken token = this.tokenService.convertToSpecificToken(this.persistencyService.loadToken(deviceId));
      if (token.getAssignmentForUser(userId) == null) {
         throw VascoOtpValidationHelper.deviceNotAssignedToUserException(userId, deviceId);
      }

      try {
         this.letterGenerator.renderAndSendReport(token, userId);
      } catch (IOException | EmailServiceException e) {
         throw new FailureException(e);
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   VascoOtpDirectLetterPrint(VascoLetterGenerator letterGenerator, VascoTokenService tokenService, VascoOtpPersistencyService persistencyService) {
      this.letterGenerator = letterGenerator;
      this.tokenService = tokenService;
      this.persistencyService = persistencyService;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static VascoOtpDirectLetterPrint.VascoOtpDirectLetterPrintBuilder builder() {
      return new VascoOtpDirectLetterPrint.VascoOtpDirectLetterPrintBuilder();
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class VascoOtpDirectLetterPrintBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private VascoLetterGenerator letterGenerator;
      @SuppressFBWarnings(justification = "generated code")
      private VascoTokenService tokenService;
      @SuppressFBWarnings(justification = "generated code")
      private VascoOtpPersistencyService persistencyService;

      @SuppressFBWarnings(justification = "generated code")
      VascoOtpDirectLetterPrintBuilder() {
      }

      @SuppressFBWarnings(justification = "generated code")
      public VascoOtpDirectLetterPrint.VascoOtpDirectLetterPrintBuilder letterGenerator(VascoLetterGenerator letterGenerator) {
         this.letterGenerator = letterGenerator;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public VascoOtpDirectLetterPrint.VascoOtpDirectLetterPrintBuilder tokenService(VascoTokenService tokenService) {
         this.tokenService = tokenService;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public VascoOtpDirectLetterPrint.VascoOtpDirectLetterPrintBuilder persistencyService(VascoOtpPersistencyService persistencyService) {
         this.persistencyService = persistencyService;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public VascoOtpDirectLetterPrint build() {
         return new VascoOtpDirectLetterPrint(this.letterGenerator, this.tokenService, this.persistencyService);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "VascoOtpDirectLetterPrint.VascoOtpDirectLetterPrintBuilder(letterGenerator="
            + this.letterGenerator
            + ", tokenService="
            + this.tokenService
            + ", persistencyService="
            + this.persistencyService
            + ")";
      }
   }
}
