package net.jsreport.java.service;

import com.google.gson.Gson;
import net.jsreport.java.JsReportException;
import net.jsreport.java.dto.RenderTemplateRequest;
import net.jsreport.java.entity.Report;
import net.jsreport.java.entity.Template;
import org.apache.http.HttpResponse;

import java.io.IOException;
import java.net.URISyntaxException;

public class ReportingServiceImpl implements ReportingService {

    private static final Gson GSON = new Gson();

    private final HttpRemoteService remoteService;

    public ReportingServiceImpl(HttpRemoteService remoteService) {
        this.remoteService = remoteService;
    }

    public Report render(String templateShortid, Object data) throws JsReportException {
        Template template = new Template();
        template.setShortid(templateShortid);

        RenderTemplateRequest renderRequest = new RenderTemplateRequest();
        renderRequest.setTemplate(template);
        renderRequest.setData(data);

        return render(renderRequest);
    }

    public Report render(String templateShortid, String jsonData) throws JsReportException {
        String requestString = String.format("{ \"template\" : { \"shortid\" : \"%s\"}, \"data\" : %s }", templateShortid, jsonData);

        try {
            return renderString(requestString);
        } catch (IOException | URISyntaxException e) {
            throw new JsReportException(e);
        }
    }

    public Report render(RenderTemplateRequest request) throws JsReportException {
        try {
            return renderString(GSON.toJson(request));
        } catch (IOException | URISyntaxException e) {
            throw new JsReportException(e);
        }
    }

    // --- private

    private Report renderString(String request) throws IOException, JsReportException, URISyntaxException {
        HttpResponse response = remoteService.post("/api/report", request);

        if (response.getStatusLine().getStatusCode() >= 400) {
            throw new JsReportException(String.format("Invalid status code (%d) !!!", response.getStatusLine().getStatusCode()));
        }

        Report result = new Report();

        try {
            result.setContent(response.getEntity().getContent());
        } catch (IOException e) {
            throw new JsReportException(e);
        }

        result.setContentType(remoteService.findHeader(response, HttpRemoteServiceImpl.HEADER_CONTENT_TYPE));
        result.setFileExtension(remoteService.findAndParseHeader(response, HttpRemoteServiceImpl.HEADER_FILE_EXTENSION));
        // TODO
        result.setPermanentLink(null);

        return result;
    }
}

@@ -298,24 +298,69 @@
 OException;%0A
+import java.io.UnsupportedEncodingException;%0A
 import java.
@@ -1279,35 +1279,16 @@
 nData);%0A
-%0A        try %7B%0A    
         
@@ -1327,117 +1327,8 @@
 g);%0A
-        %7D catch (IOException %7C URISyntaxException e) %7B%0A            throw new JsReportException(e);%0A        %7D%0A
     
@@ -1413,34 +1413,16 @@
 ption %7B%0A
-        try %7B%0A    
         
@@ -1468,117 +1468,8 @@
 ));%0A
-        %7D catch (IOException %7C URISyntaxException e) %7B%0A            throw new JsReportException(e);%0A        %7D%0A
     
@@ -1549,21 +1549,8 @@
 rows
- IOException,
  JsR
@@ -1567,28 +1567,8 @@
 tion
-, URISyntaxException
  %7B%0A 
@@ -1587,16 +1587,52 @@
 esponse 
+response;%0A        try %7B%0A            
 response
@@ -1677,16 +1677,142 @@
 equest);
+%0A        %7D catch (UnsupportedEncodingException %7C URISyntaxException e) %7B%0A            throw new JsReportException(e);%0A        %7D
 %0A%0A      

update exception handlign