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

import com.google.common.io.BaseEncoding;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;

public final class IamTarget {
   private final IamConfigRoot configRoot;
   private final IamInstance instance;
   private final IamProfile profile;

   public IamTarget(IamConfigRoot configRoot, IamInstance instance) {
      this(configRoot, instance, (IamProfile)null);
   }

   public IamTarget(IamConfigRoot configRoot, IamInstance instance, String profileName) {
      this(configRoot, instance, new IamProfile(instance, profileName));
   }

   public IamTarget(IamConfigRoot configRoot, IamInstance instance, IamProfile profile) {
      this.configRoot = configRoot;
      this.instance = instance;
      this.profile = profile;
   }

   public Optional<IamProfile> profile() {
      return Optional.ofNullable(this.profile);
   }

   public String id() {
      String id = this.instance.name() + this.profile().map(p -> "/" + p.name()).orElse("");

      try {
         byte[] hash = MessageDigest.getInstance("MD5").digest(id.getBytes(StandardCharsets.UTF_8));
         return BaseEncoding.base16().lowerCase().encode(hash);
      } catch (NoSuchAlgorithmException e) {
         throw new IllegalStateException(e);
      }
   }

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

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

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "IamTarget(configRoot=" + this.configRoot() + ", instance=" + this.instance() + ", profile=" + this.profile() + ")";
   }
}
