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

import com.airlock.iam.plugin.api.config.ConfigurationEnvironment;
import com.airlock.iam.plugin.configeditor.ConfigurationLoadListener;
import com.airlock.iam.plugin.configeditor.data.EnvironmentSelection;
import com.airlock.iam.plugin.configeditor.data.EnvironmentSelectionChangeListener;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;

public class ActivateButtonUpdater implements EnvironmentSelectionChangeListener, ConfigurationLoadListener {
   private final IconWithLabel applyIcon;
   private final ConfigurationEnvironment instanceEnvironment;
   private Optional<EnvironmentSelection> lastEnvironmentSelection = Optional.empty();
   private Optional<Boolean> lastLoadedConfigIsSnippet = Optional.empty();

   @Override
   public void environmentSelectionChanged(EnvironmentSelection environmentSelection) {
      this.lastEnvironmentSelection = Optional.of(environmentSelection);
      this.updateActivationButtonState();
   }

   @Override
   public void newConfigurationLoaded(ConfigurationLoadListener.ConfigurationLoadedEvent event) {
      this.lastLoadedConfigIsSnippet = Optional.of(event.getHandler().getDictionary().isDocumentTypeSnippet());
      this.updateActivationButtonState();
   }

   @Override
   public void failedToLoadConfig(ConfigurationLoadListener.FailedToLoadConfigurationEvent event) {
   }

   private void updateActivationButtonState() {
      if (this.isRegularConfig() && this.activationPossibleInCurrentEnvironment()) {
         this.applyIcon.enable();
      } else {
         this.applyIcon.disable();
      }
   }

   private boolean isRegularConfig() {
      return this.lastLoadedConfigIsSnippet.isPresent() && !this.lastLoadedConfigIsSnippet.get();
   }

   private boolean activationPossibleInCurrentEnvironment() {
      return this.instanceEnvironment.isCommonEnvironment()
         || this.lastEnvironmentSelection.isPresent() && this.isCommonOrInstanceEnvironmentSelected(this.lastEnvironmentSelection.get());
   }

   private boolean isCommonOrInstanceEnvironmentSelected(EnvironmentSelection selection) {
      ConfigurationEnvironment selected = selection.getSelectedEnvironment();
      return selected.isCommonEnvironment() || selected.equals(this.instanceEnvironment);
   }

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

         Object this$applyIcon = this.applyIcon;
         Object other$applyIcon = other.applyIcon;
         if (this$applyIcon == null ? other$applyIcon == null : this$applyIcon.equals(other$applyIcon)) {
            Object this$instanceEnvironment = this.instanceEnvironment;
            Object other$instanceEnvironment = other.instanceEnvironment;
            if (this$instanceEnvironment == null ? other$instanceEnvironment == null : this$instanceEnvironment.equals(other$instanceEnvironment)) {
               Object this$lastEnvironmentSelection = this.lastEnvironmentSelection;
               Object other$lastEnvironmentSelection = other.lastEnvironmentSelection;
               if (this$lastEnvironmentSelection == null
                  ? other$lastEnvironmentSelection == null
                  : this$lastEnvironmentSelection.equals(other$lastEnvironmentSelection)) {
                  Object this$lastLoadedConfigIsSnippet = this.lastLoadedConfigIsSnippet;
                  Object other$lastLoadedConfigIsSnippet = other.lastLoadedConfigIsSnippet;
                  return this$lastLoadedConfigIsSnippet == null
                     ? other$lastLoadedConfigIsSnippet == null
                     : this$lastLoadedConfigIsSnippet.equals(other$lastLoadedConfigIsSnippet);
               } else {
                  return false;
               }
            } else {
               return false;
            }
         } else {
            return false;
         }
      }
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public ActivateButtonUpdater(IconWithLabel applyIcon, ConfigurationEnvironment instanceEnvironment) {
      this.applyIcon = applyIcon;
      this.instanceEnvironment = instanceEnvironment;
   }
}
