
package io.gravitee.rest.api.service.impl;

import static io.gravitee.common.http.HttpMethod.GET;
import static io.gravitee.common.http.HttpMethod.POST;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.rest.api.service.EmailValidator;
import io.gravitee.rest.api.service.HttpClientService;
import io.gravitee.rest.api.service.NewsletterService;
import io.gravitee.rest.api.service.exceptions.EmailFormatInvalidException;
import io.gravitee.rest.api.service.exceptions.TechnicalManagementException;
import io.vertx.core.buffer.Buffer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author Azize ELAMRANI (azize.elamrani at graviteesource.com)
 * @author GraviteeSource Team
 */
@Component
public class NewsletterServiceImpl extends TransactionalService implements NewsletterService {

    private static final Logger LOGGER = LoggerFactory.getLogger(NewsletterServiceImpl.class);

    @Autowired
    private HttpClientService httpClientService;

    @Autowired
    private ObjectMapper mapper;

    @Value("${newsletter.enabled:true}")
    private boolean newsletterEnabled;

    @Value("${newsletter.url:https://newsletter.gravitee.io/}")
    private String newsletterUrl;

    @Override
    @Async
    public void subscribe(final String email) {
        try {
            if (isEnabled()) {
                if (!EmailValidator.isValid(email)) {
                    throw new EmailFormatInvalidException(email);
                }
                final Map<String, Object> newsletterInfo = new HashMap<>();
                newsletterInfo.put("email", email);
                httpClientService.request(POST, newsletterUrl, emptyMap(), mapper.writeValueAsString(newsletterInfo), null);
            }
        } catch (final Exception e) {
            LOGGER.error("Error while subscribing to newsletters cause: {}", e.getMessage());
        }
    }

    @Override
    public List<String> getTaglines() {
        try {
            if (isEnabled()) {
                String taglinesPath = "taglines";
                if (newsletterUrl.lastIndexOf('/') != newsletterUrl.length() - 1) {
                    taglinesPath = "/" + taglinesPath;
                }
                final Buffer buffer = httpClientService.request(GET, newsletterUrl + taglinesPath, emptyMap(), null, null);
                return mapper.readValue(buffer.toString(), List.class);
            }
        } catch (Exception e) {
            LOGGER.debug("Error while reading the taglines response: {}", e.getMessage());
        }
        return null;
    }

    @Override
    public boolean isEnabled() {
        return newsletterEnabled;
    }
}
