package com.airlock.iam.oauth2.application.configuration;

import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.license.PluginLicenseRestriction;
import com.airlock.iam.plugin.api.annotation.property.BooleanProperty;
import com.airlock.iam.plugin.api.annotation.property.EnumProperty;
import com.airlock.iam.plugin.framework.domain.model.property.EnumPropertyTranslator;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@Plugin(
   description = "OpenID Connect Session Management settings according to the <a href=\"https://openid.net/specs/openid-connect-session-1_0.html\" target=\"_blank\">OpenID Connect Session Management Specification</a>",
   displayName = "OIDC Session Management"
)
@PluginLicenseRestriction(moduleAffiliation = "OAuthServer")
public final class OpenIDConnectSessionManagementConfig {
   @EnumProperty(
      description = "<p>Specifies the 'SameSite' cookie attribute of the OP User Agent State cookie.</p><p>The correct choice of this property is essential for the correct operation of this feature:<ul><li>For same-domain scenarios, 'Lax' should be chosen. In this case, the cookie will only be accessible when the top-level page (which includes the OP iframe) is in the same domain as the OP iframe)</li><li>For cross-domain scenarios, 'None' must be chosen. In this case, 'Secure' is also automatically appended because modern browsers only accept those cookies on a secure connection</li><li>To rely on browser default behavior (not recommended), 'No SameSite Attribute' can be chosen, resulting in cookies usually interpreted as 'Lax' depending on the browser version.</li></ul></p>",
      defaultValue = "LAX",
      optional = true
   )
   private OpenIDConnectSessionManagementConfig.OpuasCookieSamesitePolicy sameSitePolicy;
   @BooleanProperty(
      description = "When enabled, the OP Iframe writes debug logs to the browser console on every message, allowing to debug the functionality and find the reason for a possible 'error' response. In addition, the origin check endpoint logs a debug log when an origin is not accepted with further information.",
      defaultValue = "false",
      optional = true
   )
   private boolean debug;

   public OpenIDConnectSessionManagementConfig.OpuasCookieSamesitePolicy getSameSitePolicy() {
      return this.sameSitePolicy;
   }

   public boolean isDebug() {
      return this.debug;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   private OpenIDConnectSessionManagementConfig(OpenIDConnectSessionManagementConfig.OpuasCookieSamesitePolicy sameSitePolicy, boolean debug) {
      this.sameSitePolicy = sameSitePolicy;
      this.debug = debug;
   }

   @SuppressFBWarnings(justification = "generated code")
   public OpenIDConnectSessionManagementConfig() {
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class OpenIDConnectSessionManagementConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private OpenIDConnectSessionManagementConfig.OpuasCookieSamesitePolicy sameSitePolicy;
      @SuppressFBWarnings(justification = "generated code")
      private boolean debug;

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

      @SuppressFBWarnings(justification = "generated code")
      public OpenIDConnectSessionManagementConfig.OpenIDConnectSessionManagementConfigBuilder sameSitePolicy(
         OpenIDConnectSessionManagementConfig.OpuasCookieSamesitePolicy sameSitePolicy
      ) {
         this.sameSitePolicy = sameSitePolicy;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OpenIDConnectSessionManagementConfig.OpenIDConnectSessionManagementConfigBuilder debug(boolean debug) {
         this.debug = debug;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OpenIDConnectSessionManagementConfig build() {
         return new OpenIDConnectSessionManagementConfig(this.sameSitePolicy, this.debug);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "OpenIDConnectSessionManagementConfig.OpenIDConnectSessionManagementConfigBuilder(sameSitePolicy="
            + this.sameSitePolicy
            + ", debug="
            + this.debug
            + ")";
      }
   }

   public enum OpuasCookieSamesitePolicy implements EnumPropertyTranslator {
      LAX("Lax"),
      NONE("None"),
      UNDEFINED("No SameSite Attribute");

      private final String displayValue;

      public String getDisplayValue() {
         return this.displayValue;
      }

      @SuppressFBWarnings(justification = "generated code")
      OpuasCookieSamesitePolicy(final String displayValue) {
         this.displayValue = displayValue;
      }
   }
}
