/*******************************************************************************
 * Copyright 2017 SalzburgResearch Forschungsgesellschaft
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package at.newmedialab.lmf.search.ldpath.model.functions;

import java.util.Collection;
import java.util.Collections;

import org.apache.marmotta.kiwi.model.rdf.KiWiLiteral;
import org.apache.marmotta.ldpath.api.backend.RDFBackend;
import org.apache.marmotta.ldpath.api.functions.SelectorFunction;
import org.openrdf.model.Value;

import com.google.common.base.CaseFormat;

import at.newmedialab.lmf.search.ldpath.model.backend.WildcardAwareBackend;

public class DynamicFunction  extends SelectorFunction<Value> {

	@Override
    public Collection<Value> apply(RDFBackend<Value> backend, Value context, Collection<Value>... args) throws IllegalArgumentException {
//    	if (args.length < 1 && args.length > 4) {
//            throw new IllegalArgumentException("wrong usage: " + getSignature());
//    	}
//    	if (args.length < 2 && args[0].size() != 1 ) {
//            throw new IllegalArgumentException("wrong usage: " + getSignature());
//    	}
        if (args.length != 2 ) {
            throw new IllegalArgumentException("wrong usage: " + getSignature());
        }
        String fieldName = null;
        String fieldValue = null;
    	
    	for (Value node : args[0]) {
    		if (node instanceof KiWiLiteral) {
    			String value = ((KiWiLiteral) node).stringValue();
    			fieldName = formatNameQualifier(value);
    			break;
    		}
    		
    	}
    	for (Value node : args[1]) {
    		if (node instanceof KiWiLiteral) {
    			String value = ((KiWiLiteral) node).stringValue();
    			fieldValue = value;
    			break;
    		}
    		
    	}
    	if (fieldName !=null && fieldValue !=null ) {
    		if ( backend instanceof WildcardAwareBackend) {
    			return ((WildcardAwareBackend) backend).createDynLiteralCollection(fieldName, fieldValue);
    		}
    	}

    	return Collections.<Value>emptyList();
    }

    @Override
    public String getSignature() {
        return "fn:dynamic(name: Value, value: Value]) : DynNodeList";
    }

    @Override
    public String getDescription() {
        return "dynamically specify solr index field based on name qualifier";
    }

    @Override
    public String getLocalName() {
        return "dynamic";
    }
    private String formatNameQualifier(String input) {
		input = input.replaceAll("[^a-zA-Z0-9 ]", "");
		input = input.trim().replaceAll(" ", "_").toUpperCase();
		return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, input);
    }
}

@@ -827,24 +827,52 @@
 functions;%0A%0A
+import java.util.ArrayList;%0A
 import java.
@@ -1607,18 +1607,16 @@
 /    %09%7D%0A
-//
     %09if 
@@ -1628,18 +1628,18 @@
 .length 
-%3C 
+!=
 2 && arg
@@ -1663,134 +1663,8 @@
 ) %7B%0A
-//            throw new IllegalArgumentException(%22wrong usage: %22 + getSignature());%0A//    %09%7D%0A        if (args.length != 2 ) %7B%0A
     
@@ -1745,20 +1745,17 @@
 ));%0A    
-    
+%09
 %7D%0A      
@@ -1781,32 +1781,34 @@
 = null;%0A        
+//
 String fieldValu
@@ -1821,14 +1821,8 @@
 ll;%0A
-    %09%0A
     
@@ -2042,246 +2042,443 @@
    %09
-for (Value node : args%5B1%5D) %7B%0A    %09%09if (node instanceof KiWiLiteral) %7B%0A    %09%09%09
+if ( backend instanceof WildcardAwareBackend) %7B%0A    %09%09Collection%3C
 String
+%3E
  value
+s
  = 
-((KiWiLiteral) node).stringValue();%0A    %09%09%09fieldValue =
+processArguments(args%5B1%5D);%0A    %09%09return ((WildcardAwareBackend) backend).createDynLiteralCollection(fieldName,
  value
+s)
 ;%0A    %09
-%09%09break;%0A    %09%09%7D%0A    %09%09%0A    %09%7D%0A    %09if (fieldName !=null && fieldValue !=null 
+%7D%0A    %09else %7B%0A    %09%09return Collections.%3CValue%3EemptyList();%0A    %09%7D%0A    %7D%0A%09private Collection%3CString%3E processArguments(Collection%3CValue%3E values ) %7B%0A%09%09Collection%3CString%3E fieldValues = new ArrayList%3C%3E();%0A    %09for (Value node :values
 ) %7B%0A
@@ -2483,32 +2483,28 @@
 %7B%0A    %09%09if (
- backend
+node
  instanceof 
@@ -2507,123 +2507,105 @@
 eof 
-WildcardAwareBackend) %7B%0A    %09%09%09return ((WildcardAwareBackend) backend).createDynLiteralCollection(fieldName, fieldV
+KiWiLiteral) %7B%0A    %09%09%09String value = ((KiWiLiteral) node).stringValue();%0A    %09%09%09fieldValues.add(v
 alue
@@ -2622,17 +2622,16 @@
 %0A    %09%7D%0A
-%0A
     %09ret
@@ -2638,46 +2638,26 @@
 urn 
-Collections.%3CValue%3EemptyList();%0A    %7D%0A
+fieldValues;%0A%09%09%0A%09%7D
 %0A   

allow multiple values for dynamic fields