package com.evolveum.midpoint.gui.api.component.path;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.component.autocomplete.AutoCompleteItemDefinitionPanel;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.page.admin.configuration.component.EmptyOnBlurAjaxFormUpdatingBehaviour;

public class ItemPathSegmentPanel extends BasePanel<ItemPathDto> {

	private static final long serialVersionUID = 1L;

	private static final String ID_DEFINITION = "definition";
	private static final String ID_PARENT = "parentPath";

	public ItemPathSegmentPanel(String id, ItemPathDto model) {
		this(id, Model.of(model));
	}
	
	public ItemPathSegmentPanel(String id, IModel<ItemPathDto> model) {
		super(id, model);

		initLayout();
	}

	private void initLayout() {

		Label label = new Label(ID_PARENT,
				new AbstractReadOnlyModel<String>() {
			private static final long serialVersionUID = 1L;
				@Override
				public String getObject() {
					
					if (getModelObject().getParentPath() == null) {
						return null;
					}
					
					return getString("ItemPathSegmentPanel.itemToSearch", getModelObject().getParentPath().toItemPath().toString());
				}
				});

		label.add(new VisibleEnableBehaviour() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isVisible() {
				return getModelObject().getParentPath() != null;
			}
		});
		label.setOutputMarkupId(true);
		add(label);

		final AutoCompleteItemDefinitionPanel itemDefPanel = new AutoCompleteItemDefinitionPanel(
				ID_DEFINITION, new PropertyModel<ItemDefinition<?>>(getModel(), "itemDef")) {
			private static final long serialVersionUID = 1L;

			protected Map<String, ItemDefinition<?>> listChoices(String input) {
				return collectAvailableDefinitions(input);

			}
		};
//		itemDefPanel.getBaseFormComponent().add(new EmptyOnBlurAjaxFormUpdatingBehaviour());
		itemDefPanel.setOutputMarkupId(true);
		add(itemDefPanel);
	}

	private Map<String, ItemDefinition<?>> collectAvailableDefinitions(String input) {
		Map<String, ItemDefinition<?>> toSelect = new HashMap<>();
		ItemPathDto parentItem = getModelObject().getParentPath();
		if (parentItem != null) {
			if (parentItem.getItemDef() instanceof PrismContainerDefinition<?>) {
				PrismContainerDefinition<?> parentContainer = (PrismContainerDefinition<?>) parentItem
						.getItemDef();
				for (ItemDefinition<?> def : parentContainer.getDefinitions()) {
					if (def.getName() != null) {
						if (StringUtils.isBlank(input)) {
							toSelect.put(def.getName().getLocalPart(), def);
						} else {
							if (def.getName().getLocalPart().startsWith(input)) {
								toSelect.put(def.getName().getLocalPart(), def);
							}
						}
					}
				}
			}
		} else {

			for (ItemDefinition<?> def : getSchemaDefinitionMap().get(getModelObject().getObjectType())) {
				if (def.getName() != null) {
					if (StringUtils.isBlank(input)) {
						toSelect.put(def.getName().getLocalPart(), def);
					} else {
						if (def.getName().getLocalPart().startsWith(input)) {
							toSelect.put(def.getName().getLocalPart(), def);
						}
					}
				}
			}

		}
		// }
		return toSelect;
	}

	public void refreshModel(ItemPathDto newModel) {
		getModel().setObject(newModel);
	}

	protected Map<QName, Collection<ItemDefinition<?>>> getSchemaDefinitionMap() {
		return new HashMap<>();
	}
	
	public boolean validate() {
//		AutoCompleteItemDefinitionPanel autocompletePanel = (AutoCompleteItemDefinitionPanel) get(ID_DEFINITION);
//		String current = (String) autocompletePanel.getBaseFormComponent().getModelObject();
//		if (getModelObject().getParentPath() != null && StringUtils.isNotBlank(current)) {
//			ItemDefinition<?> def = getModelObject().getParentPath().getItemDef();
//			if (def.getName().getLocalPart())
//			( !=
//		}
		return getModelObject().getItemDef() != null;
		
//		return autocompletePanel.getBaseFormComponent().getModelObject() != null;
	}

}

@@ -3384,17 +3384,98 @@
  else %7B%0A
-%0A
+%09%09%09if (getSchemaDefinitionMap().get(getModelObject().getObjectType()) != null) %7B%0A%09
 %09%09%09for (
@@ -3564,24 +3564,25 @@
 e())) %7B%0A%09%09%09%09
+%09
 if (def.getN
@@ -3594,24 +3594,25 @@
  != null) %7B%0A
+%09
 %09%09%09%09%09if (Str
@@ -3634,24 +3634,25 @@
 k(input)) %7B%0A
+%09
 %09%09%09%09%09%09toSele
@@ -3695,24 +3695,25 @@
  def);%0A%09%09%09%09%09
+%09
 %7D else %7B%0A%09%09%09
@@ -3705,24 +3705,25 @@
 %09%09%09%7D else %7B%0A
+%09
 %09%09%09%09%09%09if (de
@@ -3769,32 +3769,33 @@
 nput)) %7B%0A%09%09%09%09%09%09%09
+%09
 toSelect.put(def
@@ -3823,24 +3823,33 @@
 rt(), def);%0A
+%09%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09%7D%0A%09%09%09%09
@@ -3862,17 +3862,16 @@
 %09%7D%0A%09%09%09%7D%0A
-%0A
 %09%09%7D%0A%09%09//

fix of npe on the history tab