package com.airlock.iam.application.parameters.domain.model;

import com.google.common.base.Preconditions;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;

public final class IamInstance {
   private static final Pattern INSTANCE_NAME_PATTERN = Pattern.compile("^[\\w-]+[\\w-.]*$");
   public static final String INSTANCE_PROPERTIES_FILE_NAME = "instance.properties";
   private static final String PROFILES_DIRECTORY = "profiles";
   private static final String LOG4J_DIRECTORY = "log4j";
   public static final String COMMON_INSTANCE = "common";
   private final String name;
   private final Path directory;

   public IamInstance(String name, Path directory) {
      Preconditions.checkArgument(isValidInstanceName(name), "name must match pattern");
      Objects.requireNonNull(directory);
      this.name = name;
      this.directory = directory;
   }

   public static boolean isValidInstanceName(String name) {
      return INSTANCE_NAME_PATTERN.matcher(name).matches();
   }

   public Optional<Path> parametersFile() {
      Path parametersFile = this.directory.resolve("instance.properties");
      return Files.isRegularFile(parametersFile) ? Optional.of(parametersFile) : Optional.empty();
   }

   public Path profilesDirectory() {
      return this.directory.resolve("profiles");
   }

   public Path log4jDirectory() {
      return this.directory.resolve("log4j");
   }

   public boolean isCommon() {
      return "common".equalsIgnoreCase(this.name);
   }

   @SuppressFBWarnings(justification = "generated code")
   public String name() {
      return this.name;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Path directory() {
      return this.directory;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof IamInstance other)) {
         return false;
      } else {
         Object this$name = this.name;
         Object other$name = other.name;
         if (this$name == null ? other$name == null : this$name.equals(other$name)) {
            Object this$directory = this.directory;
            Object other$directory = other.directory;
            return this$directory == null ? other$directory == null : this$directory.equals(other$directory);
         } else {
            return false;
         }
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public int hashCode() {
      int PRIME = 59;
      int result = 1;
      Object $name = this.name;
      result = result * 59 + ($name == null ? 43 : $name.hashCode());
      Object $directory = this.directory;
      return result * 59 + ($directory == null ? 43 : $directory.hashCode());
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "IamInstance(name=" + this.name() + ", directory=" + this.directory() + ")";
   }
}
