package com.github.onsdigital.babbage.request.handler;

import com.github.onsdigital.babbage.content.client.ContentClient;
import com.github.onsdigital.babbage.content.client.ContentFilter;
import com.github.onsdigital.babbage.content.client.ContentReadException;
import com.github.onsdigital.babbage.content.client.ContentResponse;
import com.github.onsdigital.babbage.pdf.PDFGenerator;
import com.github.onsdigital.babbage.request.handler.base.RequestHandler;
import com.github.onsdigital.babbage.response.BabbageBinaryResponse;
import com.github.onsdigital.babbage.response.base.BabbageResponse;
import com.github.onsdigital.babbage.util.RequestUtil;
import com.github.onsdigital.babbage.util.json.JsonUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import static com.github.onsdigital.babbage.content.client.ContentClient.filter;


/**
 * Created by bren on 07/07/15.
 */
public class PDFRequestHandler implements RequestHandler {

    private static final String REQUEST_TYPE = "pdf-new";
    public static final String CONTENT_TYPE = "application/pdf";

    public BabbageResponse get(String requestedUri, HttpServletRequest requests) throws Exception {
        String uriPath = StringUtils.removeStart(requestedUri, "/");
        System.out.println("Generating pdf for uri:" + uriPath);
        String pdfTable = getPDFTables(uriPath);
        if(pdfTable != null) {
            System.out.println("Using pdfTable: " + pdfTable);
        }
        Path pdfFile = PDFGenerator.generatePdf(requestedUri, getTitle(requestedUri), RequestUtil.getAllCookies(requests), pdfTable);
        InputStream fin = Files.newInputStream(pdfFile);
        BabbageBinaryResponse response = new BabbageBinaryResponse(fin, CONTENT_TYPE);
        response.addHeader("Content-Length", Long.toString(FileUtils.sizeOf(pdfFile.toFile())));
        response.addHeader("Content-Disposition", "attachment; filename=\"" + pdfFile.getFileName() + "\"");
        return response;
    }

    @Override
    public String getRequestType() {
        return REQUEST_TYPE;
    }

    public static String getTitle(String uri) throws IOException, ContentReadException {
        ContentResponse contentResponse = ContentClient.getInstance().getContent(uri, filter(ContentFilter.DESCRIPTION));
        Map<String, Object> stringObjectMap = JsonUtil.toMap(contentResponse.getDataStream());

        Map<String, Object> descriptionMap = (Map<String, Object>) stringObjectMap.get("description");

        String title = (String) descriptionMap.get("title");
        String edition = (String) descriptionMap.get("edition");

        if (StringUtils.isNotEmpty(edition))
            title += " " + edition;

        return title;
    }

    public String getPDFTables(String uri) throws IOException, ContentReadException {
        ContentResponse contentResponse = ContentClient.getInstance().getContent(uri);
        Map<String, Object> stringObjectMap = JsonUtil.toMap(contentResponse.getDataStream());

        List<Map<String, Object>> o = (List<Map<String, Object>>) stringObjectMap.get("pdfTable");

        if(o != null && o.size() > 0) {
            String filename = (String)o.get(0).get("file");
            String file = uri + "/" + filename;
            ContentResponse pdfTableContentResponse = ContentClient.getInstance().getResource(file);
            File targetFile = new File(FileUtils.getTempDirectoryPath() + "/" + filename);
            FileUtils.copyInputStreamToFile(pdfTableContentResponse.getDataStream(), targetFile);
            return targetFile.getAbsolutePath();
        }

        return null;
    }

}

@@ -516,32 +516,113 @@
 BinaryResponse;%0A
+import com.github.onsdigital.babbage.response.BabbageContentBasedBinaryResponse;%0A
 import com.githu
@@ -1494,32 +1494,821 @@
 ows Exception %7B%0A
+%0A        ContentResponse contentResponse = null;%0A        try %7B%0A            contentResponse = ContentClient.getInstance().getResource(requestedUri + %22/page.pdf%22);%0A            BabbageContentBasedBinaryResponse response = new BabbageContentBasedBinaryResponse(contentResponse, contentResponse.getDataStream(), contentResponse.getMimeType());%0A            String contentDispositionHeader = %22attachment; %22;%0A            contentDispositionHeader += contentResponse.getName() == null ? %22%22 : %22filename=%5C%22%22 + contentResponse.getName() + %22%5C%22%22;%0A            response.addHeader(%22Content-Disposition%22, contentDispositionHeader);%0A            return response;%0A        %7D catch (ContentReadException e) %7B%0A            System.out.println(%22Pre-rendered PDF not found - attempting to generate it...%22);%0A        %7D%0A%0A
         String u

Use pregenerated PDF if its available on the new PDF endpoint.