package edu.isi.karma.kr2rml.formatter;

import org.json.JSONArray;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jayway.jsonpath.internal.PathToken;
import com.jayway.jsonpath.internal.PathTokenizer;

public class KR2RMLJSONPathColumnNameFormatter extends
		KR2RMLColumnNameFormatter {

	private static Logger logger = LoggerFactory.getLogger(KR2RMLJSONPathColumnNameFormatter.class);
	
	@Override
	protected String format(String columnName) {
		StringBuilder result = new StringBuilder("$.");
		try {	
			if (columnName.startsWith("[") && columnName.endsWith("]") && columnName.contains(",")) {
	    		JSONArray strArr = new JSONArray(columnName);
				for (int i=0; i<strArr.length(); i++) {
					result.append(strArr.getString(i));
					if(i < strArr.length() - 1)
					{
						result.append(".");
					}
	    		}
				return result.toString();
			}
			else
			{
				return columnName;
			}
		} catch (JSONException e) {
			logger.error("Unable to format column name: " + columnName);
		}
		return result.toString();
	}

	@Override
	protected String removeFormatting(String formattedColumnName) {
		
		PathTokenizer pt = new PathTokenizer(formattedColumnName);
	
		JSONArray strArr = new JSONArray();
		for(PathToken token : pt.getPathTokens())
		{
			if(token.getFragment().equalsIgnoreCase("$"))
			{
				continue;
			}
			//TODO test for array index
			strArr.put(token.getFragment());
		}
		if(strArr.length() == 1)
		{
			return strArr.getString(0);
		}
		
		return strArr.toString();
	
	}


}

@@ -537,16 +537,19 @@
 ilder(%22$
+%5B*%5D
 .%22);%0A%09%09t
@@ -1331,27 +1331,90 @@
 t().
-equalsIgnoreCase(%22$
+startsWith(%22$%22))%0A%09%09%09%7B%0A%09%09%09%09continue;%0A%09%09%09%7D%0A%09%09%09if(token.getFragment().startsWith(%22%5B*%5D
 %22))%0A

Handle [*] at beginning of JSONPath