package com.style24.core.biz.thirdparty;

import java.net.URI;

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.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.gagaframework.web.parameter.GagaMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.persistence.domain.NaverPay;

import lombok.extern.slf4j.Slf4j;

/**
 * 네이버페이 API
 *
 * @author card007
 * @since 2021. 03. 07
 */
@Component
@Slf4j
public class NaverPayApi {

	@Autowired
	private Environment env;

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private RestTemplate restTemplate;

	/**
	 * 네이버페이 JSON API
	 *
	 * @param Order
	 * @return GagaMap
	 * @author card007
	 * @since 2021. 03. 07
	 */
	public NaverPay naverPaymentJsonApi(GagaMap params, String apiUrl) {
		NaverPay naverPay;
		try {
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_JSON);
			headers.set("X-Naver-Client-Id", env.getProperty("naverPay.clientId"));
			headers.set("X-Naver-Client-Secret", env.getProperty("naverPay.clientSecret"));

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

			ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
			log.info("responseEntity.getStatusCode(): {} ", responseEntity.getStatusCode());

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

			Gson gson = new GsonBuilder().create();
			naverPay = gson.fromJson(jsonResult, NaverPay.class);
			naverPay.setStatusCode(responseEntity.getStatusCode().value());

		} catch (Exception e) {
			// throw new IllegalStateException(message.getMessage("FAIL_0004"));
			throw new IllegalStateException(e.getMessage());
		}

		return naverPay;
	}

	/**
	 * 네이버페이 API
	 *
	 * @param Order
	 * @return GagaMap
	 * @author card007
	 * @since 2021. 03. 08
	 */
	public NaverPay naverPaymentApi(MultiValueMap<String, String> params, String apiUrl) {
		// 취소가 완료되기까지 시간이 걸리므로, Timeout 시간을 최소 60초로 설정해야 합니다
		NaverPay naverPay;
		try {
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
			headers.set("X-Naver-Client-Id", env.getProperty("naverPay.clientId"));
			headers.set("X-Naver-Client-Secret", env.getProperty("naverPay.clientSecret"));

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

			ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
			log.info("responseEntity.getStatusCode(): {} ", responseEntity.getStatusCode());

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

			Gson gson = new GsonBuilder().create();
			naverPay = gson.fromJson(jsonResult, NaverPay.class);
			naverPay.setStatusCode(responseEntity.getStatusCode().value());

		} catch (Exception e) {
			// throw new IllegalStateException(message.getMessage("FAIL_0004"));
			throw new IllegalStateException(e.getMessage());
		}

		return naverPay;
	}
}
