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 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 {
            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,24 +97,25 @@
 sonBuilder;%0A
+%0A
 import de.tb
@@ -186,24 +186,25 @@
 ractFilter;%0A
+%0A
 import org.a
@@ -230,16 +230,66 @@
 leUtils;
+%0Aimport org.apache.commons.lang3.math.NumberUtils;
 %0A%0Aimport
@@ -1675,37 +1675,333 @@
     
-    document.put(name,value);
+%09if(NumberUtils.isNumber(value)) %7B%0A        %09%09try %7B%0A        %09%09%09Long intValue = Long.valueOf(value);%0A            %09%09document.put(name,intValue);%0A        %09%09%7D catch (NumberFormatException e) %7B%0A        %09%09%09document.put(name,value);%0A        %09%09%7D%0A        %09%09%0A        %09%7D else %7B%0A        %09%09document.put(name,value);%0A        %09%7D%0A            
 %0A   

write long values as long object to the json