package com.airlock.iam.admin.application.configuration.logviewer;

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.property.BooleanProperty;
import com.airlock.iam.plugin.api.annotation.property.GroupProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginListProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.List;
import java.util.Optional;

@Plugin(
   description = "<p>When configured, enables the Log Viewer, which allows viewing messages from log files.</p>\n<p>The available log files as well as their order can be customized here. You may also define a set of filter\nrules, which can be selected by admins to view specific log messages.</p>\n<p>In addition, color schemes can be defined, which highlight specific messages with the selected colors.</p>"
)
@Groups(groupSequence = {"Basic Settings", "Advanced Settings"}, openGroups = "Basic Settings")
public class LogViewer {
   @PluginListProperty(
      description = "<p>The list of log files that will appear in the log viewer.</p>\n<p>The order of the log files in this property is used in the Log Viewer as well.</p>",
      optional = false,
      displayName = "Logfiles",
      minElements = 1
   )
   @GroupProperty(groupIdentifier = "Basic Settings")
   private List<LogFile> logfiles;
   public static final String LOGFILES_DISPLAY_NAME = "Logfiles";
   @PluginListProperty(
      description = "<p>Defines color schemes for the Log Viewer. Messages are matched using regular expressions.</p>\n<p>If the log line matches more than one pattern, the first matching rule is used.\nThe log level is matched first and then the message text. If no scheme is defined, the following\ndefault scheme is used:<p>\n<ul>\n<li>Error messages (log level \"ERROR\") are marked red.</li>\n<li>Warning messages (log level \"WARN\") are marked yellow.</li>\n<li>Successful authentication lines are marked green.</li>\n</ul>",
      displayName = "Color Scheme",
      optional = true
   )
   @GroupProperty(groupIdentifier = "Basic Settings")
   private List<ColoringRule> colorScheme;
   public static final String COLOR_SCHEME_DISPLAY_NAME = "Color Scheme";
   @PluginProperty(
      description = "<p>This property serves two purposes:</p>\n<p>Firstly, it defines the log file that is selected when the Log Viewer is first opened during a browser session.\nWhen reopening the Log Viewer during the same session, the last selected log file is remembered.</p>\n<p>Secondly, it defines the log file that is opened when switching to the Log Viewer from the User Activity Tab.\nIn this case, it will always open the file defined by this property.</p>\n<p>If left undefined, the first log file in the list is used instead.</p>",
      optional = true
   )
   @GroupProperty(groupIdentifier = "Basic Settings")
   private LogFile initiallySelectedLogFile;
   @BooleanProperty(
      description = "<p>If enabled, the difference between the previously activated configuration and the newly activated configuration\ngets logged to the <b>Adminapp log file</b> when a configuration is activated in the Config Editor.</p>\n<p>For example, if the Radius service port gets changed, the following log message is logged:</p>\n<p><tt>\n\t&lt;property name=\"port\"&gt;1812&lt;/property&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property name=\"port\">1813&lt;/property&gt;\n<tt></p>\n<p>Please be aware that enabling this setting has no effect for the current activation, but for future ones.</p>",
      optional = true,
      defaultValue = "false"
   )
   @GroupProperty(groupIdentifier = "Advanced Settings")
   private boolean logConfigChanges;

   public Optional<String> getInitiallySelectedLogFile() {
      return Optional.ofNullable(this.initiallySelectedLogFile).map(LogFile::getNameTranslationKey);
   }

   public List<ColoringRule> getColorScheme() {
      return this.colorScheme;
   }

   public List<LogFile> getLogFiles() {
      return this.logfiles;
   }

   public boolean shouldLogConfigChanges() {
      return this.logConfigChanges;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public LogViewer(List<LogFile> logfiles, List<ColoringRule> colorScheme, LogFile initiallySelectedLogFile, boolean logConfigChanges) {
      this.logfiles = logfiles;
      this.colorScheme = colorScheme;
      this.initiallySelectedLogFile = initiallySelectedLogFile;
      this.logConfigChanges = logConfigChanges;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class LogViewerBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private List<LogFile> logfiles;
      @SuppressFBWarnings(justification = "generated code")
      private List<ColoringRule> colorScheme;
      @SuppressFBWarnings(justification = "generated code")
      private LogFile initiallySelectedLogFile;
      @SuppressFBWarnings(justification = "generated code")
      private boolean logConfigChanges;

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

      @SuppressFBWarnings(justification = "generated code")
      public LogViewer.LogViewerBuilder logfiles(List<LogFile> logfiles) {
         this.logfiles = logfiles;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LogViewer.LogViewerBuilder colorScheme(List<ColoringRule> colorScheme) {
         this.colorScheme = colorScheme;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LogViewer.LogViewerBuilder initiallySelectedLogFile(LogFile initiallySelectedLogFile) {
         this.initiallySelectedLogFile = initiallySelectedLogFile;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LogViewer.LogViewerBuilder logConfigChanges(boolean logConfigChanges) {
         this.logConfigChanges = logConfigChanges;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LogViewer build() {
         return new LogViewer(this.logfiles, this.colorScheme, this.initiallySelectedLogFile, this.logConfigChanges);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "LogViewer.LogViewerBuilder(logfiles="
            + this.logfiles
            + ", colorScheme="
            + this.colorScheme
            + ", initiallySelectedLogFile="
            + this.initiallySelectedLogFile
            + ", logConfigChanges="
            + this.logConfigChanges
            + ")";
      }
   }
}
