package com.github.dreambrother.hackernews.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dreambrother.hackernews.dto.HackernewsItem;
import com.github.dreambrother.hackernews.exceptions.RuntimeIOException;
import com.github.dreambrother.hackernews.exceptions.VkException;
import org.apache.http.NameValuePair;
import org.apache.http.client.fluent.Request;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Map;

public class VkClientImpl implements VkClient {

    private static final Logger log = LoggerFactory.getLogger(VkClientImpl.class);
    private static final ObjectMapper mapper = new ObjectMapper();

    private static final int DEFAULT_TIMEOUT_MILLIS = 15000;

    private static final String USER_AGENT =
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36";

    private String token;
    private String communityId;

    @Override
    public void publish(HackernewsItem news) {
        log.info("Publish {}", news);
        try {
            String responseString = Request.Post("https://api.vk.com/method/wall.post")
                    .connectTimeout(DEFAULT_TIMEOUT_MILLIS)
                    .socketTimeout(DEFAULT_TIMEOUT_MILLIS)
                    .userAgent(USER_AGENT)
                    .bodyForm(paramsForNews(news), Charset.forName("UTF-8"))
                    .execute().returnContent().asString();

            log.info("VK response {}", responseString);

            Map<String, String> response = mapper.readValue(responseString, Map.class);
            checkResponse(response);
        } catch (IOException e) {
            throw new RuntimeIOException(e);
        }
    }

    private Iterable<? extends NameValuePair> paramsForNews(HackernewsItem news) {
        return Arrays.asList(
                new BasicNameValuePair("owner_id", communityId),
                new BasicNameValuePair("from_group", "1"),
                new BasicNameValuePair("message", news.getTitle()),
                new BasicNameValuePair("attachments", news.getUrl()),
                new BasicNameValuePair("access_token", token),
                new BasicNameValuePair("copyright", "https://news.ycombinator.com/item?id=" + news.getId()),
                new BasicNameValuePair("v", "5.131")
        );
    }

    private void checkResponse(Map<String, String> response) {
        if (response.get("response") == null) {
            throw new VkException("VK response contains error");
        }
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setCommunityId(String communityId) {
        this.communityId = communityId;
    }
}

@@ -2172,22 +2172,26 @@
 e%22, 
-news.getTitle(
+formatMessage(news
 )),%0A
@@ -2327,117 +2327,8 @@
 n),%0A
-                new BasicNameValuePair(%22copyright%22, %22https://news.ycombinator.com/item?id=%22 + news.getId()),%0A
     
@@ -2570,16 +2570,16 @@
 rror%22);%0A
-
         
@@ -2579,32 +2579,218 @@
        %7D%0A    %7D%0A%0A
+    private String formatMessage(HackernewsItem news) %7B%0A        return String.format(%22%25s%25nComments: %25s%22, news.getTitle(), %22https://news.ycombinator.com/item?id=%22 + news.getId());%0A    %7D%0A%0A
     public void 

Revert "move comments to copyright block"