package com.dstsystems.hackathon.autotempo.tempo;

import com.dstsystems.hackathon.autotempo.models.WorklogModel;
import com.dstsystems.hackathon.autotempo.tempo.models.TempoAuthor;
import com.dstsystems.hackathon.autotempo.tempo.models.TempoIssue;
import com.dstsystems.hackathon.autotempo.tempo.models.TempoWorklog;
import com.dstsystems.hackathon.autotempo.tempo.models.TempoWorklogAttribute;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TempoSubmitter {

    private static final String ACCOUNT_ATTRIBUTE = "_account_";
    private static final String REST_WORKLOG_PATH = "/rest/tempo-timesheets/3/worklogs/";

    private TempoConfig tempoConfig;

    public TempoSubmitter(TempoConfig tempoConfig) {
        this.tempoConfig = tempoConfig;
    }

    public void submitWorklog(WorklogModel worklogModel) throws IOException {
        String worklogJson = getWorklogJson(worklogModel);
        sendHttpRequest(worklogJson);
    }

    protected String getWorklogJson(WorklogModel worklogModel) throws JsonProcessingException {
        TempoWorklog tempoWorklog = new TempoWorklog();
        tempoWorklog.setComment(worklogModel.getComment());
        tempoWorklog.setDateStarted(worklogModel.getDate());
        tempoWorklog.setTimeSpentSeconds(worklogModel.getTimeSpent());

        TempoAuthor tempoAuthor = new TempoAuthor();
        tempoAuthor.setName(tempoConfig.getUsername());
        tempoWorklog.setAuthor(tempoAuthor);

        TempoIssue tempoIssue = new TempoIssue();
        tempoIssue.setKey(worklogModel.getIssueKey());
        tempoWorklog.setIssue(tempoIssue);
        // TODO: need to get and calculate remaining time for non-internal issues

        List<TempoWorklogAttribute> worklogAttributes = new ArrayList<>();
        worklogAttributes.add(new TempoWorklogAttribute(
                ACCOUNT_ATTRIBUTE, worklogModel.getAccountKey()));
        tempoWorklog.setWorklogAttributes(worklogAttributes);

        return new ObjectMapper().writeValueAsString(tempoWorklog);
    }

    private String sendHttpRequest(String content) throws IOException {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;

        try {
            httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(tempoConfig.getUrl() + REST_WORKLOG_PATH);
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setHeader("Origin", tempoConfig.getUrl()); // Required for JIRA cloud instances
            httpPost.setEntity(new StringEntity(content));

            response = httpClient.execute(httpPost);

            int httpStatus = response.getStatusLine().getStatusCode();
            if (httpStatus != 200) {
                throw new IOException("Got http response code " + httpStatus);
            }

            return IOUtils.toString(response.getEntity().getContent());
        } finally {
            if (httpClient != null) {
                httpClient.close();
            }
            if (response != null) {
                response.close();
            }
        }
    }
}

@@ -643,24 +643,78 @@
 s.HttpPost;%0A
+import org.apache.http.client.methods.HttpUriRequest;%0A
 import org.a
@@ -1406,31 +1406,27 @@
         
-sendHttpRequest
+postWorklog
 (worklog
@@ -2530,23 +2530,19 @@
 ing 
-sendHttpRequest
+postWorklog
 (Str
@@ -2579,182 +2579,8 @@
 n %7B%0A
-        CloseableHttpClient httpClient = null;%0A        CloseableHttpResponse response = null;%0A%0A        try %7B%0A            httpClient = HttpClientBuilder.create().build();%0A    
     
@@ -2659,36 +2659,32 @@
 _PATH);%0A        
-    
 httpPost.setHead
@@ -2723,36 +2723,32 @@
 json%22);%0A        
-   
- 
 httpPost.setHead
@@ -2824,28 +2824,24 @@
 ces%0A        
-    
 httpPost.set
@@ -2888,48 +2888,346 @@
     
-    response = httpClient.execute(httpPo
+return sendHttpRequest(httpPost);%0A    %7D%0A%0A    private String sendHttpRequest(HttpUriRequest request) throws IOException %7B%0A        CloseableHttpClient httpClient = null;%0A        CloseableHttpResponse response = null;%0A%0A        try %7B%0A            httpClient = HttpClientBuilder.create().build();%0A            response = httpClient.execute(reque
 st);

Refactor TempoSubmitterTest