package com.airlock.iam.login.app.misc.configuration.oneshot;

import com.airlock.iam.core.application.configuration.waf.AirlockGatewayConfig;
import com.airlock.iam.login.app.misc.oneshot.ErrorBehavior;
import com.airlock.iam.login.app.misc.oneshot.impl.error.AirlockAuthWorkflowErrorBehavior;
import com.airlock.iam.login.app.misc.oneshot.impl.error.CompositeErrorBehavior;
import com.airlock.iam.login.app.misc.oneshot.impl.error.HTTPHeaderErrorBehavior;
import com.airlock.iam.login.app.misc.oneshot.impl.error.HTTPStatusCodeErrorBehavior;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.EnumProperty;
import com.airlock.iam.plugin.api.annotation.property.IntegerProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginListProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.List;

@Plugin(
   description = "Defines how to respond, i.e. what HTTP response to send (status and headers) and how to proceed on the Airlock Gateway (WAF).",
   displayName = "Failure HTTP Response"
)
public final class FailureHttpResponseConfig {
   @IntegerProperty(
      description = "The HTTP status code to set on the response.",
      displayName = "HTTP Status Code",
      exampleValues = {401, 403, 404},
      defaultValue = 401,
      optional = true
   )
   private int httpStatusCode;
   @PluginListProperty(description = "A list of HTTP headers to add to the response.", displayName = "HTTP Headers", minElements = 1, optional = true)
   private List<HTTPHeader> httpHeaders;
   @EnumProperty(description = "Controls the Airlock Gateway (WAF) workflow.", defaultValue = "CONTINUE", optional = true)
   private AirlockAuthWorkflowErrorBehavior.AirlockAuthWorkflowAction workflow;

   public ErrorBehavior create(AirlockGatewayConfig wafConfig) {
      return new CompositeErrorBehavior(
         new HTTPStatusCodeErrorBehavior(this.httpStatusCode),
         new HTTPHeaderErrorBehavior(this.httpHeaders),
         new AirlockAuthWorkflowErrorBehavior(this.workflow, wafConfig.getControlCookieName())
      );
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public FailureHttpResponseConfig(int httpStatusCode, List<HTTPHeader> httpHeaders, AirlockAuthWorkflowErrorBehavior.AirlockAuthWorkflowAction workflow) {
      this.httpStatusCode = httpStatusCode;
      this.httpHeaders = httpHeaders;
      this.workflow = workflow;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class FailureHttpResponseConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private int httpStatusCode;
      @SuppressFBWarnings(justification = "generated code")
      private List<HTTPHeader> httpHeaders;
      @SuppressFBWarnings(justification = "generated code")
      private AirlockAuthWorkflowErrorBehavior.AirlockAuthWorkflowAction workflow;

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

      @SuppressFBWarnings(justification = "generated code")
      public FailureHttpResponseConfig.FailureHttpResponseConfigBuilder httpStatusCode(int httpStatusCode) {
         this.httpStatusCode = httpStatusCode;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public FailureHttpResponseConfig.FailureHttpResponseConfigBuilder httpHeaders(List<HTTPHeader> httpHeaders) {
         this.httpHeaders = httpHeaders;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public FailureHttpResponseConfig.FailureHttpResponseConfigBuilder workflow(AirlockAuthWorkflowErrorBehavior.AirlockAuthWorkflowAction workflow) {
         this.workflow = workflow;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public FailureHttpResponseConfig build() {
         return new FailureHttpResponseConfig(this.httpStatusCode, this.httpHeaders, this.workflow);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "FailureHttpResponseConfig.FailureHttpResponseConfigBuilder(httpStatusCode="
            + this.httpStatusCode
            + ", httpHeaders="
            + this.httpHeaders
            + ", workflow="
            + this.workflow
            + ")";
      }
   }
}
