package de.jeha.kame.crawler.scheduler.services;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.jeha.kame.crawler.core.http.ContentType;
import de.jeha.kame.crawler.core.http.Headers;
import de.jeha.kame.crawler.scheduler.model.CrawlJob;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author jenshadlich@googlemail.com
 */
public class QuartzCrawlJob implements Job {

    private static final Logger LOG = LoggerFactory.getLogger(QuartzCrawlJob.class);

    public static final String CRAWL_JOB = "CRAWL_JOB";

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        CrawlJob crawlJob = (CrawlJob) context.getJobDetail().getJobDataMap().get(CRAWL_JOB);
        LOG.info("{}", crawlJob);

        ObjectMapper objectMapper = new ObjectMapper();
        CrawlRequest request = new CrawlRequest(crawlJob.getSeedUrl());

        HttpPost httpPost = new HttpPost("http://localhost:8080/crawl");
        httpPost.setHeader(Headers.CONTENT_TYPE, ContentType.APPLICATION_JSON.getValue());

        try {
            httpPost.setEntity(new StringEntity(objectMapper.writeValueAsString(request), "UTF-8"));
            HttpResponse response = HttpClients.createDefault().execute(httpPost);

            String responseJson = EntityUtils.toString(response.getEntity());
            LOG.info("{}", responseJson);

        } catch (IOException e) {
            throw new JobExecutionException(e);
        }

    }

    public class CrawlRequest {

        @JsonProperty
        private String url;

        public CrawlRequest(String url) {
            this.url = url;
        }

        public String getUrl() {
            return url;
        }

    }

}

@@ -43,16 +43,78 @@
 vices;%0A%0A
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;%0A
 import c
@@ -769,16 +769,39 @@
 ception;
+%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -1690,17 +1690,21 @@
 esponse 
-r
+httpR
 esponse 
@@ -1808,17 +1808,21 @@
 oString(
-r
+httpR
 esponse.
@@ -1831,24 +1831,121 @@
 tEntity());%0A
+            CrawlResponse response = objectMapper.readValue(responseJson, CrawlResponse.class);%0A%0A
             
@@ -1967,20 +1967,24 @@
 response
-Json
+.getId()
 );%0A%0A    
@@ -2086,24 +2086,31 @@
 %0A    public 
+static 
 class CrawlR
@@ -2326,10 +2326,623 @@
 %0A    %7D%0A%0A
+    @JsonIgnoreProperties(ignoreUnknown = true)%0A    public static class CrawlResponse %7B%0A%0A        @JsonProperty%0A        private String id;%0A%0A        @JsonProperty%0A        private String date;%0A%0A        @JsonProperty%0A        private Integer statusCode;%0A%0A        @JsonProperty%0A        private List%3CString%3E links;%0A%0A        public String getId() %7B%0A            return id;%0A        %7D%0A%0A        public String getDate() %7B%0A            return date;%0A        %7D%0A%0A        public Integer getStatusCode() %7B%0A            return statusCode;%0A        %7D%0A%0A        public List%3CString%3E getLinks() %7B%0A            return links;%0A        %7D%0A%0A    %7D%0A%0A
 %7D%0A

call crawler service (wip hack)