package com.github.dreambrother.hackernews.client;

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.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.fluent.Request;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class VkClientImpl implements VkClient {

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

    private static final String WALL_POST_URL = "https://api.vk.com/method/wall.post";

    private String token;
    private String communityId;

    @Override
    public void publish(HackernewsItem news) {
        log.info("Publish {}", news);
        try {
            HttpResponse httpResponse = Request.Post(WALL_POST_URL)
                    .bodyForm(paramsForNews(news), Charset.forName("UTF-8"))
                    .execute().returnResponse();

            if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                handleError(httpResponse);
            }
        } 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("access_token", token),
                new BasicNameValuePair("v", "5.14")
        );
    }

    private void handleError(HttpResponse httpResponse) {
        try {
            String error = EntityUtils.toString(httpResponse.getEntity());
            throw new VkException(error);
        } catch (IOException e) {
            throw new RuntimeIOException(e);
        }
    }

    @Override
    public void publish(List<HackernewsItem> items) {
        throw new UnsupportedOperationException();
    }

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

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

@@ -1801,16 +1801,92 @@
 essage%22,
+ formatMessage(news)),%0A                new BasicNameValuePair(%22attachments%22,
  news.ge
@@ -1890,13 +1890,12 @@
 .get
-Title
+Link
 ()),
@@ -2309,24 +2309,179 @@
    %7D%0A    %7D%0A%0A
+    private String formatMessage(HackernewsItem news) %7B%0A        return String.format(%22%25s%25n Comments: %25s%22, news.getTitle(), news.getCommentsLink());%0A    %7D%0A%0A
     @Overrid

format message with attachments