package com.airlock.iam.login.application.configuration.targetapp;

import com.airlock.iam.flow.shared.application.configuration.valueprovider.RoleProviderConfig;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.StringListProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Plugin(description = "Provides a list of statically configured roles.", displayName = "Static Roles")
public class StaticRolesProviderConfig implements RoleProviderConfig {
   @StringListProperty(
      description = "List of role names. The role can be followed by an idle timeout and a lifetime, each seperated by a colon.",
      exampleElements = {"roleA", "admin:600:3600"},
      optional = false
   )
   private List<String> roles;

   public List<String> getRoles() {
      return this.roles;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public StaticRolesProviderConfig(List<String> roles) {
      this.roles = roles;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class StaticRolesProviderConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<String> roles;

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

      @SuppressFBWarnings(justification = "generated code")
      public StaticRolesProviderConfig.StaticRolesProviderConfigBuilder role(String role) {
         if (this.roles == null) {
            this.roles = new ArrayList<>();
         }

         this.roles.add(role);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public StaticRolesProviderConfig.StaticRolesProviderConfigBuilder roles(Collection<? extends String> roles) {
         if (roles == null) {
            throw new NullPointerException("roles cannot be null");
         }

         if (this.roles == null) {
            this.roles = new ArrayList<>();
         }

         this.roles.addAll(roles);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public StaticRolesProviderConfig.StaticRolesProviderConfigBuilder clearRoles() {
         if (this.roles != null) {
            this.roles.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public StaticRolesProviderConfig build() {
         return new StaticRolesProviderConfig(switch (this.roles == null ? 0 : this.roles.size()) {
            case 0 -> Collections.emptyList();
            case 1 -> Collections.singletonList(this.roles.get(0));
            default -> Collections.unmodifiableList(new ArrayList<>(this.roles));
         });
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "StaticRolesProviderConfig.StaticRolesProviderConfigBuilder(roles=" + this.roles + ")";
      }
   }
}
