package kr.co.uplus.ez.common.components;

import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONObject;
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.http.HttpEntity;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;

import kr.co.uplus.ez.common.data.Const;

@Component
public class HubeasyApiComponents {

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

	@Autowired
	private WebClientRequestService clientRequestService;

	@Value("${msghubez.api-url:http://localhost:7070}")
	private String apiDomain;

	@Value("${msghubez.uri.sendTemplateMail:/api/v1/fo/sendTemplateMail}")
	private String apiSendTemplateMail;

	@SuppressWarnings("unchecked")
	public Map<Object, Object> sendTemplateMailApi(Map<Object, Object> apiReqMap) {

		Map<Object, Object> apiResultMap = new HashMap<Object, Object>();
		JSONObject jsonObject = new JSONObject();

		String callUrl = apiDomain + apiSendTemplateMail;
		String applicationId = (String) apiReqMap.get("applicationId");
		String authToken = (String) apiReqMap.get("authToken");

		log.debug("파라미터 확인하기 ={}",apiReqMap);
		if(Const.TMPL_CODE_005.equals(apiReqMap.get("templeteCode"))) {
			log.debug("사용자 등록 메일 전송, 템플릿 코드={}",apiResultMap.get("templeteCode"));
			
			jsonObject.put("userId", apiReqMap.get("userId"));
			jsonObject.put("tmpPwd", apiReqMap.get("tmpPwd"));
			jsonObject.put("homeUrl", apiReqMap.get("homeUrl"));
			
		} else if(Const.TMPL_CODE_007.equals(apiReqMap.get("templeteCode"))) {
			log.debug("발신번호 반려 메일 전송, 템플릿 코드={}",apiReqMap.get("templeteCode"));
			jsonObject.put("rejectListSndrNo", apiReqMap.get("rejectListSndrNo"));
			jsonObject.put("rejectHtml", apiReqMap.get("rejectHtml"));
		} else {
			log.debug("템플릿코드 확인 ={}",apiReqMap.get("templeteCode"));
		}
		

		String jsonString = jsonObject.toString();

		MultipartBodyBuilder builder = new MultipartBodyBuilder();
		builder.part("fromEmail", apiReqMap.get("fromEmail"));
		builder.part("toEmail", apiReqMap.get("toEmail"));
		builder.part("templeteCode", apiReqMap.get("templeteCode"));
		builder.part("templeteReplace", jsonString);

		log.debug("메일 내용 ={}",jsonString);
		
		MultiValueMap<String, HttpEntity<?>> parts = builder.build();

		try {

			apiResultMap = (Map<Object, Object>) clientRequestService.callBySyncPostWithTokenMultipart(callUrl, applicationId, authToken, parts);

			if(apiResultMap == null) {
				apiResultMap.put("API_RSLT", "F");
				apiResultMap.put("API_RSLT_MSG", "API Result Recive null error");
			} else {
				if("0000".equals(apiResultMap.get("retCode"))) {
					apiResultMap.put("API_RSLT", "S");
				} else {
					apiResultMap.put("API_RSLT", "F");
					apiResultMap.put("API_RSLT_MSG", apiResultMap.get("retMsg"));
				}
			}
		} catch(Exception e) {
			apiResultMap.put("API_RSLT", "F");
			apiResultMap.put("API_RSLT_MSG", e.getMessage());
		}

		return apiResultMap;
	}

}
