package com.airlock.iam.plugin.configeditor.field;

import com.airlock.iam.plugin.api.annotation.property.RegexProperty;
import com.airlock.iam.plugin.api.exception.InitializeException;
import com.airlock.iam.plugin.configeditor.data.PropertyAdapter;
import com.airlock.iam.plugin.configeditor.rendering.VaadinRegexPropertyRenderer;
import com.airlock.iam.plugin.configeditor.ui.dialog.TestRegexDialog;
import com.airlock.iam.plugin.configeditor.util.GuiResources;
import com.airlock.iam.plugin.framework.misc.processor.RegexPropertyProcessor;
import com.vaadin.event.MouseEvents.ClickEvent;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Image;
import com.vaadin.v7.data.Property;
import com.vaadin.v7.data.Property.ValueChangeEvent;
import com.vaadin.v7.ui.AbstractField;
import com.vaadin.v7.ui.TextField;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class MedusaRegexField extends MedusaField<RegexProperty, String> {
   private static final String TEST_REGEXP_ICON_DESCRIPTION = "Test the regular expression.";
   private final Image testIcon = this.makeIcon(GuiResources.ICON_TEST, "Test the regular expression.", "with-hand", this);

   @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "See ALIAM-15315")
   public MedusaRegexField(PropertyAdapter<RegexProperty> propertyAdapter) {
      super(propertyAdapter);
      this.testIcon.addStyleName("regexTestIcon");
      this.getFieldLayout().addComponent(this.testIcon);
      this.getFieldLayout().setComponentAlignment(this.testIcon, Alignment.MIDDLE_LEFT);
   }

   @Override
   public AbstractField<String> createInnerField() {
      TextField field = new TextField();
      field.setWidth("100%");
      field.setImmediate(true);
      field.setBuffered(false);
      field.setNullRepresentation("");
      return field;
   }

   public void setPropertyDataSource(Property newDataSource) {
      this.getInnerField().setPropertyDataSource(newDataSource);
      this.updateUiAndValidate();
   }

   public Property getPropertyDataSource() {
      return this.getInnerField().getPropertyDataSource();
   }

   @Override
   public void click(ClickEvent event) {
      super.click(event);
      if (event.getSource() == this.testIcon) {
         this.openDialogToTestExpression();
      }
   }

   @Override
   public void valueChange(ValueChangeEvent event) {
      this.propagateNullValueIfFieldIsUndefined();
      super.valueChange(event);
   }

   private void openDialogToTestExpression() {
      RegexProperty regexProperty = (RegexProperty)this.getPropertyAdapter().getFieldAnnotation();
      boolean caseSensitive = false;

      try {
         caseSensitive = RegexPropertyProcessor.isMatchingCaseSensitive(
            this.getPropertyAdapter().getPluginConfiguration().getPluginClass(),
            regexProperty,
            this.getPropertyAdapter().getConfigPropertyName(),
            this.getPropertyAdapter().getPluginConfiguration(),
            this.getPropertyAdapter().getConfigurationEnvironment(),
            this.getPropertyAdapter().getConfigurationContext()
         );
      } catch (InitializeException var4) {
      }

      TestRegexDialog dialog = new TestRegexDialog(
         (TextField)this.getInnerField(),
         this.getPropertyAdapter().getDisplayName(),
         VaadinRegexPropertyRenderer.getRegexpInfos(regexProperty),
         !caseSensitive,
         regexProperty.isMultiLine(),
         regexProperty.useDotAll(),
         regexProperty.matchAgainstWholeInput()
      );
      dialog.addCloseListener(e -> {
         if (dialog.getNewRegex() != null) {
            this.getInnerField().setValue(dialog.getNewRegex());
         }
      });
   }
}
