package de.tblsoft.solr.pipeline.filter;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import de.tblsoft.solr.http.HTTPHelper;
import de.tblsoft.solr.pipeline.AbstractFilter;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class JsonWriter extends AbstractFilter {

    private Map<String, Object> document = new HashMap<String, Object>();
    private Gson gson;


    private String outputField;
    private String output;

    private String type;

    private String location;




    @Override
    public void init() {
        type = getProperty("type", "file");
        Boolean pretty = getPropertyAsBoolean("pretty", false);
        location = getProperty("location", null);
        verify(location, "For the JsonWriter a location must be defined.");


        GsonBuilder builder = new GsonBuilder();
        if(pretty) {
            builder = builder.setPrettyPrinting();
        }
        gson = builder.create();


        super.init();
    }

    @Override
    public void field(String name, String value) {


        Object docValue = document.get(name);
        if(docValue != null) {
            List<String> values;
            if(docValue instanceof String) {
                values = new ArrayList<String>();
                values.add((String) docValue);

            } else {
                values = (List<String>) docValue;
            }
            values.add(value);
            document.put(name,values);

        } else {
        	if(NumberUtils.isNumber(value)) {
        		try {
        			Long intValue = Long.valueOf(value);
            		document.put(name,intValue);
        		} catch (NumberFormatException e) {
        			document.put(name,value);
        		}
        		
        	} else {
        		document.put(name,value);
        	}
            
        }


        super.field(name,value);

    }

    @Override
    public void endDocument() {

        if(!document.isEmpty()) {

            String json = gson.toJson(document);



            if("elastic".equals(type)) {
                HTTPHelper.post(location, json);
            } else if ("file".equals(type)) {
                try {
                    FileUtils.writeStringToFile(new File("foo.txt"), json, true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if ("stdout".equals(type)) {
                System.out.println(json);
            }



        }
        document = new HashMap<String, Object>();
        super.endDocument();
    }

    @Override
    public void end() {
        super.end();
    }

}


@@ -97,25 +97,67 @@
 sonBuilder;%0A
+import de.tblsoft.solr.http.ElasticHelper;
 %0A
-
 import de.tb
@@ -232,17 +232,16 @@
 Filter;%0A
-%0A
 import o
@@ -368,24 +368,60 @@
 OException;%0A
+import java.net.URISyntaxException;%0A
 import java.
@@ -773,16 +773,44 @@
 cation;%0A
+    private boolean delete;%0A
 %0A%0A%0A%0A    
@@ -1075,24 +1075,88 @@
 efined.%22);%0A%0A
+        delete = getPropertyAsBoolean(%22delete%22, Boolean.TRUE);%0A%0A
 %0A        Gso
@@ -2386,32 +2386,368 @@
 equals(type)) %7B%0A
+                if(delete) %7B%0A                    try %7B%0A                        String indexUrl = ElasticHelper.getIndexUrl(location);%0A                        HTTPHelper.delete(indexUrl);%0A                    %7D catch (URISyntaxException e) %7B%0A                        throw new RuntimeException(e);%0A                    %7D%0A                %7D%0A%0A
                 
@@ -2775,24 +2775,26 @@
 ion, json);%0A
+%0A%0A
             
@@ -3294,18 +3294,20 @@
 .end();%0A
-
     %7D%0A%0A
+%0A%0A
 %7D%0A%0A

add the possibility to the delete the elastic search index before indexing