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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class ExternalSecretButtonUpdater implements EnvironmentSelectionChangeListener {
   private final IconWithLabel secureExternalStorageManagerIcon;
   private final ConfigurationEnvironment instanceEnvironment;

   @Override
   public void environmentSelectionChanged(EnvironmentSelection selection) {
      boolean enabled = this.enableExternalSecretManager(selection);
      if (enabled) {
         this.secureExternalStorageManagerIcon.enable();
      } else {
         this.secureExternalStorageManagerIcon.disable();
      }
   }

   private boolean enableExternalSecretManager(EnvironmentSelection selection) {
      return !this.isInstanceCommonEnvironmentAndMultiEnvironmentConfig(selection) && selection.getSelectedEnvironment().equals(this.instanceEnvironment);
   }

   private boolean isInstanceCommonEnvironmentAndMultiEnvironmentConfig(EnvironmentSelection selection) {
      return this.instanceEnvironment.isCommonEnvironment() && selection.getInitialEnvironments().size() > 1;
   }

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

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

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

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