package com.style24.core.biz.thirdparty;

import java.net.URI;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.google.gson.Gson;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.core.exception.GagaBizException;

/**
 * 네이버 단축 URL
 *
 * @author gagamel
 * @since 2021. 5. 21
 */
@Component
@Slf4j
public class NaverShortUrl {

	@Autowired
	private Environment env;

	@Autowired
	private RestTemplate restTemplate;

	private String apiUrl;
	private String clientId;
	private String clientSecret;

	@PostConstruct
	public void init() {
		apiUrl = env.getProperty("naver.shortUrl");
		clientId = env.getProperty("naver.clientId");
		clientSecret = env.getProperty("naver.clientSecret");

		log.info("\n\n---- NaverShortUrl initialization started ----");
		log.info("apiUrl: [{}]", apiUrl);
		log.info("clientId: [{}]", clientId);
		log.info("clientSecret: [{}]", clientSecret);
		log.info("\n--- NaverShortUrl initialization completed ----\n");
	}

	/**
	 * 단축URL 조회
	 * @param longUrl - Long URL
	 * @return 단축URL
	 * @throws GagaBizException
	 */
	public String getShortUrl(String longUrl) throws GagaBizException {
		String shortUrl = "";

		try {
			// Parameter
			MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();

			params.add("url", longUrl);
			log.debug("params: {}", params);

			// Header
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
			headers.add("X-Naver-Client-Id", clientId);
			headers.add("X-Naver-Client-Secret", clientSecret);

			HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(params, headers);
			URI url = URI.create(apiUrl);

			// POST방식으로 호출
			ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
			log.info("responseEntity.getStatusCode(): {} ", responseEntity.getStatusCode());

			String responseJson = responseEntity.getBody();
			log.info("responseEntity.getBody(): {} ", responseJson);

			Gson gson = new Gson();
			UrlInfo urlInfo = gson.fromJson(responseJson, UrlInfo.class);

			// 성공하면
			if (urlInfo.getCode().equals("200")) {
				Result result = gson.fromJson(urlInfo.getResult(), Result.class);
				shortUrl = result.getUrl();
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			shortUrl = "fail";
		}

		return shortUrl;
	}

	@Data
	public class UrlInfo {
		private String code;
		private String result;
	}

	@Data
	public class Result {
		private String url;
	}

}
