package com.airlock.iam.flow.shared.domain.model.devicetoken;

import com.airlock.iam.base.api.domain.model.exception.ViolationException;
import com.airlock.iam.base.api.domain.model.user.UserId;
import com.airlock.iam.base.api.domain.model.validation.GenericError;
import com.airlock.iam.base.api.domain.model.validation.ValidationResult;
import com.airlock.iam.common.domain.model.jwk.ECJsonWebKey;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Map;
import java.util.Optional;

public final class RegisterDeviceTokenRequest {
   private final String username;
   private final Map<String, String> publicJwk;
   private final Optional<String> deviceLabel;
   private final Optional<String> deviceSerial;

   public static RegisterDeviceTokenRequest.RegisterDeviceTokenRequestBuilder builder(String username, Map<String, String> publicKeyJwkAttributes) {
      return new RegisterDeviceTokenRequest(username, publicKeyJwkAttributes, Optional.empty(), Optional.empty()).toBuilder();
   }

   public UserId getUserId() {
      return UserId.userId(this.username);
   }

   public ValidationResult validate() {
      if (this.publicJwk.isEmpty()) {
         return ValidationResult.singleError("publicJwk", GenericError.REQUIRED);
      } else {
         return !this.areKeyAttributesValid() ? ValidationResult.singleError("publicJwk", GenericError.INVALID_VALUE) : ValidationResult.valid();
      }
   }

   public ECJsonWebKey getEcJwk() {
      return ECJsonWebKey.fromMap(this.publicJwk);
   }

   private boolean areKeyAttributesValid() {
      try {
         this.getEcJwk();
         return true;
      } catch (ViolationException e) {
         return false;
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   public RegisterDeviceTokenRequest.RegisterDeviceTokenRequestBuilder toBuilder() {
      return new RegisterDeviceTokenRequest.RegisterDeviceTokenRequestBuilder()
         .username(this.username)
         .publicJwk(this.publicJwk)
         .deviceLabel(this.deviceLabel)
         .deviceSerial(this.deviceSerial);
   }

   @SuppressFBWarnings(justification = "generated code")
   public RegisterDeviceTokenRequest(String username, Map<String, String> publicJwk, Optional<String> deviceLabel, Optional<String> deviceSerial) {
      this.username = username;
      this.publicJwk = publicJwk;
      this.deviceLabel = deviceLabel;
      this.deviceSerial = deviceSerial;
   }

   @SuppressFBWarnings(justification = "generated code")
   public String getUsername() {
      return this.username;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Map<String, String> getPublicJwk() {
      return this.publicJwk;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<String> getDeviceLabel() {
      return this.deviceLabel;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<String> getDeviceSerial() {
      return this.deviceSerial;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class RegisterDeviceTokenRequestBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String username;
      @SuppressFBWarnings(justification = "generated code")
      private Map<String, String> publicJwk;
      @SuppressFBWarnings(justification = "generated code")
      private Optional<String> deviceLabel;
      @SuppressFBWarnings(justification = "generated code")
      private Optional<String> deviceSerial;

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

      @SuppressFBWarnings(justification = "generated code")
      public RegisterDeviceTokenRequest.RegisterDeviceTokenRequestBuilder username(String username) {
         this.username = username;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RegisterDeviceTokenRequest.RegisterDeviceTokenRequestBuilder publicJwk(Map<String, String> publicJwk) {
         this.publicJwk = publicJwk;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RegisterDeviceTokenRequest.RegisterDeviceTokenRequestBuilder deviceLabel(Optional<String> deviceLabel) {
         this.deviceLabel = deviceLabel;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RegisterDeviceTokenRequest.RegisterDeviceTokenRequestBuilder deviceSerial(Optional<String> deviceSerial) {
         this.deviceSerial = deviceSerial;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RegisterDeviceTokenRequest build() {
         return new RegisterDeviceTokenRequest(this.username, this.publicJwk, this.deviceLabel, this.deviceSerial);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "RegisterDeviceTokenRequest.RegisterDeviceTokenRequestBuilder(username="
            + this.username
            + ", publicJwk="
            + this.publicJwk
            + ", deviceLabel="
            + this.deviceLabel
            + ", deviceSerial="
            + this.deviceSerial
            + ")";
      }
   }
}
