package com.airlock.iam.common.domain.model.request;

import com.airlock.iam.common.api.domain.model.request.IpAddress;
import com.google.common.net.InetAddresses;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.Inet4Address;
import java.net.InetAddress;
import lombok.NonNull;

public final class IpAddressImpl implements IpAddress {
   @NonNull
   private final InetAddress ip;

   private IpAddressImpl(@NonNull String ip) {
      if (ip == null) {
         throw new NullPointerException("ip is marked non-null but is null");
      }

      this.ip = InetAddresses.forString(ip);
   }

   public static IpAddress ipAddress(String requestIp) {
      return new IpAddressImpl(requestIp);
   }

   @Override
   public String value() {
      return this.ip.getHostAddress();
   }

   @Override
   public byte[] getAddress() {
      return this.ip.getAddress();
   }

   @Override
   public boolean isIPv4() {
      return this.ip instanceof Inet4Address;
   }

   @Override
   public String toString() {
      return this.value();
   }

   @Override
   public boolean isPublicIp() {
      return !this.ip.isSiteLocalAddress();
   }

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

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

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   private InetAddress getIp() {
      return this.ip;
   }
}
