package com.airlock.iam.flow.api.application.configuration;

import com.airlock.iam.plugin.api.annotation.config.Groups;
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.GroupProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginListProperty;
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 = "UI-specific maintenance message settings. These settings are not relevant when the system is not available. If the system is not available a separate page is displayed.",
   displayName = "Maintenance Message UI Settings"
)
@PluginLicenseRestriction(moduleAffiliation = "MaintenanceMessages")
@Groups(groupSequence = {"Basic Settings", "Flow-specific Settings"}, openGroups = {"Basic Settings", "Flow-specific Settings"})
public class MaintenanceMessageUiSettings {
   static final String FLOW_SETTINGS = "Flow-specific Settings";
   @PluginListProperty(
      description = "The location filter for which maintenance messages should be displayed. Only maintenance message items whose location property is matched by at least one of the filters will be displayed if the corresponding locations are also configured at REST API Configuration > Maintenance Message Settings. If the location functionality is not used, this property should be empty. More information about filtering can be found in the Loginapp REST API documentation.",
      optional = true
   )
   @GroupProperty(groupIdentifier = "Basic Settings")
   private List<LocationFilterConfig> locationFilters;
   public static final String LOCATION_FILTER_KEY = "locationFilters";
   @BooleanProperty(
      defaultValue = "true",
      description = "If this flag is enabled the maintenance message is only displayed on the first page of the flow. The property has no effect on pages that do not belong to a specific flow.",
      displayName = "Display on first page only",
      optional = true
   )
   @GroupProperty(groupIdentifier = "Flow-specific Settings")
   private boolean firstPageOnly;

   public List<LocationFilterConfig> getLocationFilters() {
      return this.locationFilters;
   }

   public boolean isFirstPageOnly() {
      return this.firstPageOnly;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public MaintenanceMessageUiSettings(List<LocationFilterConfig> locationFilters, boolean firstPageOnly) {
      this.locationFilters = locationFilters;
      this.firstPageOnly = firstPageOnly;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class MaintenanceMessageUiSettingsBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<LocationFilterConfig> locationFilters;
      @SuppressFBWarnings(justification = "generated code")
      private boolean firstPageOnly;

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

      @SuppressFBWarnings(justification = "generated code")
      public MaintenanceMessageUiSettings.MaintenanceMessageUiSettingsBuilder locationFilter(LocationFilterConfig locationFilter) {
         if (this.locationFilters == null) {
            this.locationFilters = new ArrayList<>();
         }

         this.locationFilters.add(locationFilter);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MaintenanceMessageUiSettings.MaintenanceMessageUiSettingsBuilder locationFilters(Collection<? extends LocationFilterConfig> locationFilters) {
         if (locationFilters == null) {
            throw new NullPointerException("locationFilters cannot be null");
         }

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

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

      @SuppressFBWarnings(justification = "generated code")
      public MaintenanceMessageUiSettings.MaintenanceMessageUiSettingsBuilder clearLocationFilters() {
         if (this.locationFilters != null) {
            this.locationFilters.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MaintenanceMessageUiSettings.MaintenanceMessageUiSettingsBuilder firstPageOnly(boolean firstPageOnly) {
         this.firstPageOnly = firstPageOnly;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "MaintenanceMessageUiSettings.MaintenanceMessageUiSettingsBuilder(locationFilters="
            + this.locationFilters
            + ", firstPageOnly="
            + this.firstPageOnly
            + ")";
      }
   }
}
