package cokr.xit.ens.core.monitor.slack.support;

import cokr.xit.ens.core.monitor.slack.struct.SlackSenderTemplate;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@SuperBuilder
public class ImageSender extends SlackSenderTemplate {

    private String pretext;
    private String message;
    private String imgUrl;


    @Override
    public void execute(String token, String channel) {
        Map<String, Object> params = new HashMap<>();
        params.put("channel", channel);
        Map<String, String> attachment = new HashMap<>();
        attachment.put("pretext", pretext);
        attachment.put("text", message);
        attachment.put("image_url", imgUrl);
        List<Map<String, String>> attachments = new ArrayList<>();
        attachments.add(attachment);
        params.put("attachments", attachments);
        String body = gson.toJson(params);


        HttpEntity<String> requestEntity = new HttpEntity<>(body, this.createHeaders(ContentType.APPLICATION_JSON, token));
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(this.API_URL, HttpMethod.POST, requestEntity, String.class);
        log.info("====================================================================");
        log.info("==== Slack Send-Photo Response... ====");
        log.info(String.format("[ HttpStatusCode %d ]", responseEntity.getStatusCode().value()));
        log.info(responseEntity.getBody());
        log.info("====================================================================");

    }

}
