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

import com.airlock.iam.plugin.api.annotation.property.BooleanProperty;
import com.airlock.iam.plugin.configeditor.data.PropertyAdapter;
import com.vaadin.event.MouseEvents.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.v7.data.Property;
import com.vaadin.v7.ui.AbstractField;
import com.vaadin.v7.ui.CheckBox;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class MedusaCheckBoxField extends MedusaField<BooleanProperty, Boolean> {
   @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "See ALIAM-15315")
   public MedusaCheckBoxField(PropertyAdapter<BooleanProperty> propertyAdapter) {
      super(propertyAdapter);
      this.getFieldLayout().setWidthUndefined();
   }

   @Override
   public AbstractField<Boolean> createInnerField() {
      CheckBox field = new CheckBox();
      field.setImmediate(true);
      field.setBuffered(false);
      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);
      this.setPropertyDataSource(this.getPropertyDataSource());
   }

   @Override
   protected void addUndefinePropertyButton() {
      if (!this.getConfigurableProperty().isMandatory()) {
         this.getFieldLayout().addComponent(this.getUndefinePropertyIcon());
         this.getFieldLayout().setExpandRatio(this.getUndefinePropertyIcon(), 40.0F);
      }
   }

   @Override
   protected Component initContent() {
      this.addUndefinePropertyButton();
      return super.initContent();
   }
}
