package cz.metacentrum.perun.wui.registrar.widgets.items;

import com.google.gwt.user.client.ui.Widget;
import cz.metacentrum.perun.wui.json.Events;
import cz.metacentrum.perun.wui.model.beans.ApplicationFormItemData;
import cz.metacentrum.perun.wui.registrar.widgets.items.validators.PerunFormItemValidator;
import cz.metacentrum.perun.wui.widgets.boxes.ExtendedTextBox;
import org.gwtbootstrap3.client.ui.constants.ValidationState;
import org.gwtbootstrap3.client.ui.html.Paragraph;
import org.gwtbootstrap3.client.ui.html.Span;

/**
 * Represents TextField with value prefilled from federation. It can be visible or hidden and it Should be disable.
 *
 * @author Ondrej Velisek <ondrejvelisek@gmail.com>
 */
public class FromFederation extends PerunFormItemEditable {

	private Widget widget;

	public FromFederation(ApplicationFormItemData item, String lang, boolean onlyPreview) {
		super(item, lang, onlyPreview);
	}

	@Override
	protected Widget initWidget() {

		widget = new ExtendedTextBox();
		getBox().setEnabled(false);
		return widget;
	}

	@Override
	public void validate(Events<Boolean> events) {
		events.onLoadingStart();
		events.onFinished(true);
	}

	@Override
	public boolean validateLocal() {
		return true;
	}

	@Override
	public PerunFormItemValidator.Result getLastValidationResult() {
		return PerunFormItemValidator.Result.OK;
	}

	@Override
	public boolean focus() {
		return false;
	}

	@Override
	protected Widget initWidgetOnlyPreview() {
		widget = new Paragraph();
		getPreview().addStyleName("form-control");
		setStatus(getTranslation().federation(), ValidationState.NONE);
		return widget;
	}


	@Override
	public void setValidationTriggers() {
		// no validation
	}

	@Override
	public String getValue() {
		if (isOnlyPreview()) {
			return getPreview().getText();
		}
		return getBox().getValue();
	}

	@Override
	public Widget getWidget() {
		return widget;
	}

	@Override
	protected void setValueImpl(String value) {
		// FIXME - for now consume all multi-value attributes as they are
		// FIXME - We should implement value select for users
		if (isOnlyPreview()) {
			Span span = new Span();
			//span.setText(value.split(";")[0]);
			span.setText(value);
			getPreview().add(span);
			return;
		}
		//getBox().setValue(value.split(";")[0]);
		getBox().setValue(value);
	}


	public ExtendedTextBox getBox() {
		if (widget instanceof ExtendedTextBox) {
			return (ExtendedTextBox) widget;
		}
		return null;
	}
	public Paragraph getPreview() {
		if (widget instanceof Paragraph) {
			return (Paragraph) widget;
		}
		return null;
	}
}

@@ -1947,24 +1947,25 @@
 ng value) %7B%0A
+%0A
 %09%09// FIXME -
@@ -1969,34 +1969,111 @@
 E - 
-for now consume all
+We should implement value select for users%0A%0A%09%09// if mail or organization, use only first value of
  multi
--
 valu
@@ -2087,80 +2087,234 @@
 bute
-s as they are%0A%09%09// FIXME - We should implement value select for users
+%0A%09%09if (getItemData() != null && getItemData().getFormItem() != null &&%0A%09%09%09%09(%22mail%22.equals(getItemData().getFormItem().getFederationAttribute()) %7C%7C%0A%09%09%09%09%09%09%22o%22.equals(getItemData().getFormItem().getFederationAttribute()))) %7B%0A
 %0A%09%09
+%09
 if (
@@ -2332,16 +2332,18 @@
 ew()) %7B%0A
+%0A%09
 %09%09%09Span 
@@ -2364,18 +2364,17 @@
 n();%0A%09%09%09
+%09
-//
 span.set
@@ -2405,31 +2405,8 @@
 );%0A%09
-%09%09span.setText(value);%0A
 %09%09%09g
@@ -2432,27 +2432,25 @@
 n);%0A
+%0A
 %09%09%09
-return;%0A%09%09%7D%0A%09%09//
+%7D else %7B%0A%09%09%09%09
 getB
@@ -2485,16 +2485,155 @@
 %22)%5B0%5D);%0A
+%09%09%09%7D%0A%0A%09%09%7D else %7B%0A%0A%09%09%09if (isOnlyPreview()) %7B%0A%09%09%09%09Span span = new Span();%0A%09%09%09%09span.setText(value);%0A%09%09%09%09getPreview().add(span);%0A%09%09%09%7D else %7B%0A%09%09
 %09%09getBox
@@ -2648,24 +2648,35 @@
 lue(value);%0A
+%09%09%09%7D%0A%0A%09%09%7D%0A%0A
 %09%7D%0A%0A%0A%09public
@@ -2926,10 +2926,11 @@
 ull;%0A%09%7D%0A
+%0A
 %7D%0A

Use only first of "mail" and "o" federation attribute values