package com.airlock.iam.oauth2.domain.service.client;

import com.airlock.iam.oauth2.domain.model.token.IdToken;
import com.nimbusds.jwt.JWT;
import com.nimbusds.jwt.JWTParser;
import com.nimbusds.openid.connect.sdk.LogoutRequest;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.URI;
import java.text.ParseException;
import lombok.NonNull;

public class EndSessionEndpointService {
   @NonNull
   private final URI endpoint;
   private final URI redirectUrl;
   private final IdToken idToken;

   public URI getLogoutUrl() {
      return new LogoutRequest(this.endpoint, this.idTokenHint(this.idToken), this.redirectUrl, null).toURI();
   }

   private JWT idTokenHint(IdToken token) {
      try {
         return token != null ? JWTParser.parse(this.idToken.tokenString()) : null;
      } catch (ParseException e) {
         return null;
      }
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public EndSessionEndpointService(@NonNull URI endpoint, URI redirectUrl, IdToken idToken) {
      if (endpoint == null) {
         throw new NullPointerException("endpoint is marked non-null but is null");
      }

      this.endpoint = endpoint;
      this.redirectUrl = redirectUrl;
      this.idToken = idToken;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class EndSessionEndpointServiceBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private URI endpoint;
      @SuppressFBWarnings(justification = "generated code")
      private URI redirectUrl;
      @SuppressFBWarnings(justification = "generated code")
      private IdToken idToken;

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

      @SuppressFBWarnings(justification = "generated code")
      public EndSessionEndpointService.EndSessionEndpointServiceBuilder endpoint(@NonNull URI endpoint) {
         if (endpoint == null) {
            throw new NullPointerException("endpoint is marked non-null but is null");
         }

         this.endpoint = endpoint;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public EndSessionEndpointService.EndSessionEndpointServiceBuilder redirectUrl(URI redirectUrl) {
         this.redirectUrl = redirectUrl;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public EndSessionEndpointService.EndSessionEndpointServiceBuilder idToken(IdToken idToken) {
         this.idToken = idToken;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public EndSessionEndpointService build() {
         return new EndSessionEndpointService(this.endpoint, this.redirectUrl, this.idToken);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "EndSessionEndpointService.EndSessionEndpointServiceBuilder(endpoint="
            + this.endpoint
            + ", redirectUrl="
            + this.redirectUrl
            + ", idToken="
            + this.idToken
            + ")";
      }
   }
}
