package com.airlock.iam.login.misc.plugin;

import com.airlock.iam.core.misc.sysmessage.MaintenanceMessageService;
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.lifecycle.AfterConfigActivation;
import com.airlock.iam.plugin.api.annotation.property.GroupProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import com.airlock.iam.plugin.api.annotation.property.StringListProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.List;

@Plugin(
   description = "Configuration of maintenance messages. If a maintenance message is valid and it indicates that the system is not available, the system-not-available page is displayed instead of the requested page and the Airlock session is terminated.",
   displayName = "Maintenance Message Settings"
)
@Groups(groupSequence = {"Basic Settings", "Advanced Settings"}, openGroups = {"Basic Settings", "Advanced Settings"})
public class MaintenanceMessageConfig {
   private static final String LOCATION_PATTERN = "\\w+";
   @PluginProperty(description = "The maintenance message service plugin.", optional = false)
   @GroupProperty(groupIdentifier = "Basic Settings")
   private MaintenanceMessageService messageService;
   @StringListProperty(
      description = "<p>This property specifies all available locations used to display / retrieve maintenance messages. When defined, these identifiers are used to retrieve the messages for the corresponding locations from the session bean. If this property is left empty, the default location (null) is used.</p><p><b>Note:</b> In order to manage the maintenance messages in the Adminapp, make sure to configure the same locations in the Adminapp's maintenance message settings.</p>",
      optional = true,
      exampleElements = {"title", "detail", "footer"},
      unique = true,
      validationRegex = "\\w+"
   )
   @GroupProperty(groupIdentifier = "Advanced Settings")
   private List<String> locations;
   public static final String LOCATIONS_KEY = "locations";
   private List<String> processedLocations;

   public MaintenanceMessageConfig(MaintenanceMessageService messageService, List<String> locations) {
      this.messageService = messageService;
      this.locations = locations;
   }

   public MaintenanceMessageService getMessageService() {
      return this.messageService;
   }

   public List<String> getLocations() {
      return this.processedLocations;
   }

   @AfterConfigActivation
   void prependDefaultLocation() {
      List<String> newList = new ArrayList<>();
      newList.add(null);
      newList.addAll(this.locations);
      this.processedLocations = newList;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public static class MaintenanceMessageConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private MaintenanceMessageService messageService;
      @SuppressFBWarnings(justification = "generated code")
      private List<String> locations;

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

      @SuppressFBWarnings(justification = "generated code")
      public MaintenanceMessageConfig.MaintenanceMessageConfigBuilder messageService(MaintenanceMessageService messageService) {
         this.messageService = messageService;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MaintenanceMessageConfig.MaintenanceMessageConfigBuilder locations(List<String> locations) {
         this.locations = locations;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MaintenanceMessageConfig build() {
         return new MaintenanceMessageConfig(this.messageService, this.locations);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "MaintenanceMessageConfig.MaintenanceMessageConfigBuilder(messageService=" + this.messageService + ", locations=" + this.locations + ")";
      }
   }
}
