/*

Copyright 2011, Google Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
    * Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,           
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY           
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

package com.google.refine.importing;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import com.google.refine.RefineServlet;
import com.google.refine.commands.Command;
import com.google.refine.importing.ImportingManager.Format;
import com.google.refine.util.JSONUtilities;
import com.google.refine.util.ParsingUtilities;

public class DefaultImportingController extends Command implements ImportingController {

    protected RefineServlet servlet;

    @Override
    public void init(RefineServlet servlet) {
        this.servlet = servlet;
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        throw new ServletException("GET verb not implemented");
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        /*
         * The uploaded file is in the POST body as a "file part". If we call request.getParameter() then the POST body
         * will get read and we won't have a chance to parse the body ourselves. This is why we have to parse the URL
         * for parameters ourselves.
         */
        Map<String, String> parameters = ParsingUtilities.parseParameters(request);
        String subCommand = parameters.get("subCommand");
        if ("load-raw-data".equals(subCommand)) {
            doLoadRawData(request, response, parameters);
        } else if ("update-file-selection".equals(subCommand)) {
            doUpdateFileSelection(request, response, parameters);
        } else if ("initialize-parser-ui".equals(subCommand)) {
            doInitializeParserUI(request, response, parameters);
        } else if ("update-format-and-options".equals(subCommand)) {
            doUpdateFormatAndOptions(request, response, parameters);
        } else if ("create-project".equals(subCommand)) {
            doCreateProject(request, response, parameters);
        } else {
            respondStatusError(response, "No such sub command");
        }
    }

    private void doLoadRawData(HttpServletRequest request, HttpServletResponse response, Map<String, String> parameters)
            throws ServletException, IOException {

        long jobID = Long.parseLong(parameters.get("jobID"));
        ImportingJob job = ImportingManager.getJob(jobID);
        if (job == null) {
            respondStatusError(response, "No such import job");
            return;
        }

        job.updating = true;
        ObjectNode config = job.getOrCreateDefaultConfig();
        if (!("new".equals(JSONUtilities.getString(config, "state", null)))) {
            respondStatusError(response, "Job already started; cannot load more data");
            return;
        }

        ImportingUtilities.loadDataAndPrepareJob(
                request, response, parameters, job, config);
        job.touch();
        job.updating = false;
    }

    private void doUpdateFileSelection(HttpServletRequest request, HttpServletResponse response, Map<String, String> parameters)
            throws ServletException, IOException {

        long jobID = Long.parseLong(parameters.get("jobID"));
        ImportingJob job = ImportingManager.getJob(jobID);
        if (job == null) {
            respondStatusError(response, "No such import job");
            return;
        }

        job.updating = true;
        ObjectNode config = job.getOrCreateDefaultConfig();
        if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) {
            respondStatusError(response, "Job not ready");
            return;
        }

        ArrayNode fileSelectionArray = ParsingUtilities.evaluateJsonStringToArrayNode(
                request.getParameter("fileSelection"));

        String sortCriteria = request.getParameter("sortCriteria");
        String sortOrder = request.getParameter("sortOrder");

        if (sortCriteria != null && sortOrder != null) {
            ObjectNode retrievalRecord = job.getRetrievalRecord();
            if (retrievalRecord != null) {
                ArrayNode fileRecords = JSONUtilities.getArray(retrievalRecord, "files");
                if (fileRecords != null) {
                    List<ObjectNode> fileList = new ArrayList<>();
                    for (int i = 0; i < fileRecords.size(); i++) {
                        fileList.add((ObjectNode) fileRecords.get(i));
                    }
                    fileList.sort((o1, o2) -> {
                        int comparison = 0;
                        switch (sortCriteria) {
                            case "fileName":
                                comparison = o1.get("fileName").asText().compareTo(o2.get("fileName").asText());
                                break;
                            case "fileSize":
                                comparison = Long.compare(o1.get("size").asLong(), o2.get("size").asLong());
                                break;
                        }
                        return "desc".equals(sortOrder) ? -comparison : comparison;
                    });

                    fileRecords.removeAll();
                    for (ObjectNode fileObject : fileList) {
                        fileRecords.add(fileObject);
                    }
                }
            }
        }

        ImportingUtilities.updateJobWithNewFileSelection(job, fileSelectionArray);

        replyWithJobData(request, response, job);
        job.touch();
        job.updating = false;
    }

    private void doUpdateFormatAndOptions(HttpServletRequest request, HttpServletResponse response, Map<String, String> parameters)
            throws ServletException, IOException {

        long jobID = Long.parseLong(parameters.get("jobID"));
        ImportingJob job = ImportingManager.getJob(jobID);
        if (job == null) {
            respondStatusError(response, "No such import job");
            return;
        }

        job.updating = true;
        ObjectNode config = job.getOrCreateDefaultConfig();
        if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) {
            respondStatusError(response, "Job not ready");
            return;
        }

        String format = request.getParameter("format");
        ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode(
                request.getParameter("options"));

        List<Exception> exceptions = new LinkedList<>();

        ImportingUtilities.previewParse(job, format, optionObj, exceptions);

        try {
            if (exceptions.size() == 0) {
                job.project.update(); // update all internal models, indexes, caches, etc.
                respondStatusOk(response);
            } else {
                // TODO: Perhaps use a different function since a wrong format guess can lead to extraneous exceptions
                respondStatusErrors(response, exceptions);
            }
        } catch (IOException e) {
            throw new ServletException(e);
        }
        job.touch();
        job.updating = false;
    }

    private void doInitializeParserUI(HttpServletRequest request, HttpServletResponse response, Map<String, String> parameters)
            throws ServletException, IOException {

        long jobID = Long.parseLong(parameters.get("jobID"));
        ImportingJob job = ImportingManager.getJob(jobID);
        if (job == null) {
            respondStatusError(response, "No such import job");
            return;
        }

        String format = request.getParameter("format");
        Format formatRecord = ImportingManager.formatToRecord.get(format);
        if (formatRecord != null && formatRecord.parser != null) {
            ObjectNode options = formatRecord.parser.createParserUIInitializationData(
                    job, job.getSelectedFileRecords(), format);
            String error = JSONUtilities.getString(options, "error", null);
            // Check for XML parse errors and other unexpected problems
            if (error != null) {
                respondStatusError(response, "Error during initialization - " + error);
            } else {
                respondJSON(response,
                        Map.of("status", "ok",
                                "options", options));
            }
        } else {
            respondStatusError(response, "Unrecognized format or format has no parser");
        }
    }

    private void doCreateProject(HttpServletRequest request, HttpServletResponse response, Map<String, String> parameters)
            throws ServletException, IOException {

        long jobID = Long.parseLong(parameters.get("jobID"));
        ImportingJob job = ImportingManager.getJob(jobID);
        if (job == null) {
            respondStatusError(response, "No such import job");
            return;
        }

        job.updating = true;
        job.touch();
        ObjectNode config = job.getOrCreateDefaultConfig();
        if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) {
            respondStatusError(response, "Job not ready");
            return;
        }

        String format = request.getParameter("format");
        ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode(
                request.getParameter("options"));

        List<Exception> exceptions = new LinkedList<Exception>();

        ImportingUtilities.createProject(job, format, optionObj, exceptions, false);

        respondOkDone(response);
    }

    protected static class JobResponse {

        @JsonProperty("code")
        protected String code;
        @JsonProperty("job")
        protected ImportingJob job;

        protected JobResponse(String code, ImportingJob job) {
            this.code = code;
            this.job = job;
        }

    }

    /**
     * return the job to the front end.
     * 
     * @param request
     * @param response
     * @param job
     * @throws ServletException
     * @throws IOException
     */
    private void replyWithJobData(HttpServletRequest request, HttpServletResponse response, ImportingJob job)
            throws ServletException, IOException {

        respondJSON(response, new JobResponse("ok", job));
    }

    /**
     * @deprecated for v3.8. Unused internally and will be removed in next release
     */
    @Deprecated
    static public void writeErrors(JsonGenerator writer, List<Exception> exceptions) throws IOException {
        for (Exception e : exceptions) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));

            writer.writeStartObject();
            writer.writeStringField("message", e.getLocalizedMessage());
            writer.writeStringField("stack", sw.toString());
            writer.writeEndObject();
        }
    }

    static public ArrayNode convertErrorsToJsonArray(List<Exception> exceptions) {
        ArrayNode a = ParsingUtilities.mapper.createArrayNode();
        for (Exception e : exceptions) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));

            ObjectNode o = ParsingUtilities.mapper.createObjectNode();
            JSONUtilities.safePut(o, "message", e.getLocalizedMessage());
            JSONUtilities.safePut(o, "stack", sw.toString());
            JSONUtilities.append(a, o);
        }
        return a;
    }

}
