package org.orienteer.core.component.visualizer;

import java.util.Collection;

import com.orientechnologies.orient.core.metadata.schema.OClass;
import org.apache.wicket.Component;
import org.apache.wicket.model.IModel;
import org.orienteer.core.component.meta.OClassMetaPanel;
import org.orienteer.core.component.property.DisplayMode;

import org.orienteer.core.util.ODocumentChoiceProvider;
import org.wicketstuff.select2.*;

import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;

/**
 * {@link IVisualizer} to show links as tags control
 */
public class TagsVisualizer extends AbstractSimpleVisualizer {
	
    public TagsVisualizer() {
        super("suggest", false, OType.LINK, OType.LINKLIST, OType.LINKSET);
    }

    @SuppressWarnings("unchecked")
    @Override
    public <V> Component createComponent(String id, DisplayMode mode,
                                         IModel<ODocument> documentModel, IModel<OProperty> propertyModel, IModel<V> valueModel) {
        if (DisplayMode.EDIT.equals(mode)) {
            OProperty property = propertyModel.getObject();
            OClass oClass = property.getLinkedClass();
            AbstractSelect2Choice<?, ?> choice = property.getType().isMultiValue() ?
            		new Select2MultiChoice<ODocument>(id, (IModel<Collection<ODocument>>) valueModel, new ODocumentChoiceProvider(oClass))
            		: new Select2Choice<ODocument>(id, (IModel<ODocument>) valueModel, new ODocumentChoiceProvider(oClass));
            choice.getSettings()
						.setWidth("100%")
						.setCloseOnSelect(true)
						.setTheme(OClassMetaPanel.BOOTSTRAP_SELECT2_THEME);
			return choice;
        } else {
            return null;
        }
    }

}
@@ -386,16 +386,73 @@
 ovider;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -477,16 +477,16 @@
 ect2.*;%0A
-
 %0Aimport 
@@ -801,16 +801,100 @@
 zer %7B%0A%09%0A
+%09private static final Logger LOG = LoggerFactory.getLogger(TagsVisualizer.class);%0A%09%0A
     publ
@@ -1404,24 +1404,56 @@
 kedClass();%0A
+            if(oClass!=null) %7B%0A%09
             
@@ -1525,16 +1525,17 @@
 lue() ?%0A
+%09
         
@@ -1659,16 +1659,17 @@
 Class))%0A
+%09
         
@@ -1779,16 +1779,17 @@
 lass));%0A
+%09
         
@@ -1813,16 +1813,17 @@
 tings()%0A
+%09
 %09%09%09%09%09%09.s
@@ -1840,24 +1840,25 @@
 00%25%22)%0A%09%09%09%09%09%09
+%09
 .setCloseOnS
@@ -1869,16 +1869,17 @@
 t(true)%0A
+%09
 %09%09%09%09%09%09.s
@@ -1931,16 +1931,17 @@
 ME);%0A%09%09%09
+%09
 return c
@@ -1947,16 +1947,20 @@
 choice;%0A
+    
         
@@ -1980,16 +1980,146 @@
         
+%09LOG.warn(%22Property '%22+property.getFullName()+%22' doesn't have linked class specified.%22);%0A            %7D%0A        %7D%0A        %0A        
 return n
@@ -2122,26 +2122,16 @@
 rn null;
-%0A        %7D
 %0A    %7D%0A%0A

Fix NPE in TagsVisualizer if linked class was not specified