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

import com.airlock.iam.common.api.domain.model.persistency.exception.PersisterException;
import com.airlock.iam.common.api.domain.model.user.PersistentUser;
import com.airlock.iam.core.misc.persistency.AuthUserBean;
import com.airlock.iam.core.misc.persistency.ExtendedUserPersister;
import com.airlock.iam.core.misc.persistency.UserIterator;
import com.airlock.iam.core.misc.persistency.UserPersister;
import com.airlock.iam.plugin.api.annotation.property.GroupProperty;
import com.airlock.iam.plugin.api.annotation.property.IntegerProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.concurrent.TimeUnit;

public abstract class AbstractSelfRegUsersTask extends AbstractUserIteratorTask {
   @PluginProperty(description = "User persister plugin used to read and modify user account data.", optional = false)
   @GroupProperty(groupIdentifier = "Basic Settings")
   private ExtendedUserPersister userPersister;
   @PluginProperty(
      description = "The user iterator plugin used to iterate over all users.<br/>Usually this is the same as the UserPersister.",
      optional = false
   )
   @GroupProperty(groupIdentifier = "Basic Settings")
   private UserIterator userIterator;
   @IntegerProperty(
      description = "Number of minutes after registration after which the action is executed if the user did not log in.",
      optional = false,
      minValue = 1,
      exampleValues = {30, 1440, 10080}
   )
   @GroupProperty(groupIdentifier = "Basic Settings")
   private Integer neverLoggedInMinutes;
   protected static final String NEVER_LOGGED_IN_MINUTES = "neverLoggedInMinutes";

   @Override
   protected UserIteratorTaskConfiguration getTaskConfiguration() {
      return new AbstractSelfRegUsersTask.InternalConfiguration(this.userPersister, this.userIterator);
   }

   @Override
   protected final void handleUser(AuthUserBean user, UserIteratorTaskConfiguration config, long now) {
      if (!user.isSelfRegistered()) {
         if (this.getLogger().isDebugEnabled()) {
            this.getLogger()
               .debug("Skipping user structure with uid \"" + user.getName() + "\" in " + this.getTaskName() + " because: user is not self registered");
         }
      } else {
         long selfRegTime = user.getSelfRegistrationDate().getTime();
         long userAge = TimeUnit.MILLISECONDS.toMinutes(now - selfRegTime);
         if (this.checkNeverLoggedIn(user, userAge)) {
            String reason = "User self-registered more than " + this.neverLoggedInMinutes + " minutes ago without ever logging in afterwards.";

            try {
               this.doActionNotLoggedIn(user, reason, config);
            } catch (PersisterException e) {
               this.getLogger()
                  .error(
                     this.getLogType(),
                     "Error in " + this.getTaskName() + " while handling user \"" + user.getName() + "\" (" + e.getMessage() + "). Skipping user. ",
                     e
                  );
            }
         }
      }
   }

   @Override
   protected boolean shouldHandleInvalidUsers() {
      return true;
   }

   private boolean checkNeverLoggedIn(AuthUserBean u, long userAge) {
      return this.neverLoggedInMinutes == null ? false : u.getFirstLogin() == null && userAge > this.neverLoggedInMinutes.intValue();
   }

   protected abstract void doActionNotLoggedIn(PersistentUser var1, String var2, UserIteratorTaskConfiguration var3) throws PersisterException;

   @SuppressFBWarnings(justification = "generated code")
   public AbstractSelfRegUsersTask(ExtendedUserPersister userPersister, UserIterator userIterator, Integer neverLoggedInMinutes) {
      this.userPersister = userPersister;
      this.userIterator = userIterator;
      this.neverLoggedInMinutes = neverLoggedInMinutes;
   }

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

   private static final class InternalConfiguration implements UserIteratorTaskConfiguration {
      private final UserPersister userPersister;
      private final UserIterator userIterator;

      private InternalConfiguration(UserPersister userPersister, UserIterator userIterator) {
         this.userPersister = userPersister;
         this.userIterator = userIterator;
      }

      @Override
      public UserIterator getUserIterator() {
         return this.userIterator;
      }

      @Override
      public UserPersister getUserPersister() {
         return this.userPersister;
      }
   }
}
