package com.airlock.iam.plugin.configeditor.ui.components;

import com.airlock.iam.plugin.api.config.ConfigurationEnvironment;
import com.airlock.iam.plugin.configeditor.data.EnvironmentSelection;
import com.airlock.iam.plugin.configeditor.data.EnvironmentSelectionChangeListener;
import com.airlock.iam.plugin.configeditor.util.ValidationType;
import com.vaadin.v7.ui.ComboBox;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.HashMap;
import java.util.Map;

public class ValidationSelectionUpdater implements EnvironmentSelectionChangeListener {
   private final ComboBox validationEnvironment;
   private final ConfigurationEnvironment instanceEnvironment;
   private final ValidationType initialType;
   private final Map<ConfigurationEnvironment, ValidationType> previousSelection = new HashMap<>();

   @Override
   public void environmentSelectionChanged(EnvironmentSelection selection) {
      this.validationEnvironment.setValue(this.getPreviouslySelected(selection.getSelectedEnvironment()));
   }

   public void saveCurrentValidationSelection(ComboBox validationEnv, ConfigurationEnvironment environment) {
      this.previousSelection.put(environment, (ValidationType)validationEnv.getValue());
   }

   private ValidationType getPreviouslySelected(ConfigurationEnvironment environment) {
      return this.previousSelection.getOrDefault(environment, this.getInitialTyp(environment));
   }

   private ValidationType getInitialTyp(ConfigurationEnvironment environment) {
      return ValidationType.STRICT.equals(this.initialType) && !environment.equals(this.instanceEnvironment) ? ValidationType.DEFAULT : this.initialType;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof ValidationSelectionUpdater other)) {
         return false;
      } else {
         if (!other.canEqual(this)) {
            return false;
         }

         Object this$validationEnvironment = this.validationEnvironment;
         Object other$validationEnvironment = other.validationEnvironment;
         if (this$validationEnvironment == null ? other$validationEnvironment == null : this$validationEnvironment.equals(other$validationEnvironment)) {
            Object this$instanceEnvironment = this.instanceEnvironment;
            Object other$instanceEnvironment = other.instanceEnvironment;
            if (this$instanceEnvironment == null ? other$instanceEnvironment == null : this$instanceEnvironment.equals(other$instanceEnvironment)) {
               Object this$initialType = this.initialType;
               Object other$initialType = other.initialType;
               if (this$initialType == null ? other$initialType == null : this$initialType.equals(other$initialType)) {
                  Object this$previousSelection = this.previousSelection;
                  Object other$previousSelection = other.previousSelection;
                  return this$previousSelection == null ? other$previousSelection == null : this$previousSelection.equals(other$previousSelection);
               } else {
                  return false;
               }
            } else {
               return false;
            }
         } else {
            return false;
         }
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   protected boolean canEqual(Object other) {
      return other instanceof ValidationSelectionUpdater;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public ValidationSelectionUpdater(ComboBox validationEnvironment, ConfigurationEnvironment instanceEnvironment, ValidationType initialType) {
      this.validationEnvironment = validationEnvironment;
      this.instanceEnvironment = instanceEnvironment;
      this.initialType = initialType;
   }
}
