package com.airlock.iam.servicecontainer.app.application.configuration.radius;

import com.airlock.iam.elog.api.ELogger;
import com.airlock.iam.elog.api.ELoggerFactory;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.license.PluginLicenseRestriction;
import com.airlock.iam.plugin.api.annotation.property.StringProperty;
import com.airlock.iam.servicecontainer.app.application.configuration.RoleTransformationRuleConfig;
import com.airlock.iam.servicecontainer.app.application.service.radius.wrapper.lib.IamRadiusServerAccessRequest;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.InetAddress;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;

@Plugin(
   description = "This target service is associated with an IP.<p>The target service is meant to be used if the IP of the RADIUS client matches the one in the configuration.</p>",
   displayName = "IP-based Target Service"
)
@PluginLicenseRestriction(moduleAffiliation = "RadiusServer")
public final class IPBasedTargetServiceConfig extends TargetServiceConfig {
   private static final ELogger LOG = ELoggerFactory.getLogger(IPBasedTargetServiceConfig.class);
   @StringProperty(description = "IP of the RADIUS client.", optional = false, displayName = "Client IP", exampleValues = "192.168.1.55")
   private String clientIp;

   public IPBasedTargetServiceConfig(List<String> requiredRoles, List<RoleTransformationRuleConfig> roleTransformationRules, String clientIp) {
      super(requiredRoles, roleTransformationRules);
      this.clientIp = clientIp;
   }

   @Override
   public boolean matches(IamRadiusServerAccessRequest request, InetAddress sourceAddress) {
      if (StringUtils.isEmpty(sourceAddress.getHostAddress())) {
         LOG.info("RADIUS client IP not available.");
         return false;
      } else if (Strings.CS.equals(this.clientIp, sourceAddress.getHostAddress())) {
         LOG.info("Configured IP: " + this.clientIp + " matches with the RADIUS client IP.");
         return true;
      } else {
         LOG.debug("No match for RADIUS client with IP: " + sourceAddress.getHostAddress());
         return false;
      }
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public static class IPBasedTargetServiceConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private List<String> requiredRoles;
      @SuppressFBWarnings(justification = "generated code")
      private List<RoleTransformationRuleConfig> roleTransformationRules;
      @SuppressFBWarnings(justification = "generated code")
      private String clientIp;

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

      @SuppressFBWarnings(justification = "generated code")
      public IPBasedTargetServiceConfig.IPBasedTargetServiceConfigBuilder requiredRoles(List<String> requiredRoles) {
         this.requiredRoles = requiredRoles;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public IPBasedTargetServiceConfig.IPBasedTargetServiceConfigBuilder roleTransformationRules(List<RoleTransformationRuleConfig> roleTransformationRules) {
         this.roleTransformationRules = roleTransformationRules;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public IPBasedTargetServiceConfig build() {
         return new IPBasedTargetServiceConfig(this.requiredRoles, this.roleTransformationRules, this.clientIp);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "IPBasedTargetServiceConfig.IPBasedTargetServiceConfigBuilder(requiredRoles="
            + this.requiredRoles
            + ", roleTransformationRules="
            + this.roleTransformationRules
            + ", clientIp="
            + this.clientIp
            + ")";
      }
   }
}
