package nlib.info.service.impl;

import java.util.HashMap;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import nlib.cmm.service.NlibProperty;
import nlib.info.service.InformService;
import nlib.restful.service.DataApiReqVO;
import nlib.restful.service.DataApiResVO;
import nlib.util.StringUtil;

/**
 * <pre>
 * @Class Name : InformServiceImpl.java
 * 
 * @Description : 정보성 컨텐츠를 조회하는 서비스 
 * 
 * 
 * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
 *
 * </pre>
 *
 * @ ------------    --------    ---------------------------
 * @ 수정일                           수정자                  수정내용
 * @ ------------    --------    ---------------------------
 * @ 2021. 7. 27.    KNKIM       최초 생성
 *
 * 
 * @author 이씨플라자 * DIGITALSHIP KNKIM
 * @since 2021. 7. 27.
 * @version 1.0
 *
 */
@Service("informService")
public class InformServiceImpl implements InformService
{
	private static final Logger log = LoggerFactory.getLogger(InformServiceImpl.class);
	
	@Resource(name="informDAO")
	private InformDAO informDAO;

	/* 
	 * 이용약관을 조회한다. 
	 * 
	 * (non-Javadoc)
	 * @see nlib.info.service.InformService#selectTermsInfo(nlib.restful.service.DataApiReqVO)
	 */
	public HashMap<String, String> selectInfoContent(String councilCd, String contentType) throws Exception {
		
		if(StringUtil.isEmpty(councilCd)) councilCd = NlibProperty.getString("nlib.council.cd");
		
		HashMap<String, String> ret = informDAO.selectInfoContent(contentType + "@" + councilCd); // conDivAndCounCd
		if(ret == null) {
			ret = new HashMap<String, String>();
			ret.put("TITLE", "(없음)");
			ret.put("CONTENT", "(해당 자료가 존재하지 않습니다.)");
		}
		
		return ret;
	}
	
	/* 
	 * 이용약관을 조회한다. 
	 * 
	 * (non-Javadoc)
	 * @see nlib.info.service.InformService#selectTermsInfo(nlib.restful.service.DataApiReqVO)
	 */
	public HashMap<String, String>  selectTermsInfo(String councilCd) throws Exception {
		
		return selectInfoContent(councilCd, "TERMS");
	}

	/* 
	 * 개인정보처리방침을 조회한다. 
	 * 
	 * (non-Javadoc)
	 * @see nlib.info.service.InformService#selectPrivacyInfo(nlib.restful.service.DataApiReqVO)
	 */
	public HashMap<String, String>  selectPrivacyInfo(String councilCd) throws Exception {

		return selectInfoContent(councilCd, "PRIVACY");
	}

	/* 
	 * 인사말을 조회한다. 
	 * 
	 * (non-Javadoc)
	 * @see nlib.info.service.InformService#selectGreeting(nlib.restful.service.DataApiReqVO)
	 */
	public HashMap<String, String> selectGreeting(String councilCd) throws Exception {

		return selectInfoContent(councilCd, "GREETING");
	}

	/* 
	 * 이용안내를 조회한다. 
	 * 
	 * (non-Javadoc)
	 * @see nlib.info.service.InformService#selectNCultureOperationInfo(nlib.restful.service.DataApiReqVO)
	 */
	public HashMap<String, HashMap<String, String>> selectNCultureOperationInfo(String councilCd) throws Exception {

		HashMap<String, HashMap<String, String>> operationInfo = new HashMap<String, HashMap<String, String>>();
		
		HashMap<String, String> envValues = informDAO.selectOperationEnv(councilCd);
		
		//-----------------------------
		// 전체 문화원 공통 사항 
		//-----------------------------
		HashMap<String, String> operation1 = selectInfoContent(NlibProperty.getString("nlib.council.cd"), "OPERATION1");
		if(envValues != null && operation1 != null && operation1.get("CONTENT") != null) {
			String content = operation1.get("CONTENT");
			for(String key : envValues.keySet()) {
				content = content.replaceAll("%" + key + "%", StringUtil.getString(envValues.get(key), ""));
			}
			operation1.put("CONTENT", content);
		}
		
		HashMap<String, String> operation2 = selectInfoContent(NlibProperty.getString("nlib.council.cd"), "OPERATION2");
		HashMap<String, String> operation3 = selectInfoContent(NlibProperty.getString("nlib.council.cd"), "OPERATION3");
		
		//-----------------------------
		// 해당 문화원 특정 사항 
		//-----------------------------
		HashMap<String, String> operation4 = selectInfoContent(councilCd, "OPERATION4");
		
		operationInfo.put("OPERATION1", operation1);
		operationInfo.put("OPERATION2", operation2);
		operationInfo.put("OPERATION3", operation3);
		operationInfo.put("OPERATION4", operation4);
		
		return operationInfo;
	}

	/* 
	 * 찾아오는길 조회한다. 
	 * 
	 * (non-Javadoc)
	 * @see nlib.info.service.InformService#selectNCultureLocationInfo(nlib.restful.service.DataApiReqVO)
	 */
	public HashMap<String, String> selectNCultureLocationInfo(String councilCd) throws Exception {

		HashMap<String, String> locInfo = selectInfoContent(councilCd, "LOCATION");
		HashMap<String, String> addrInfo = informDAO.selectNCultureAddr(councilCd);
		
		if(addrInfo != null) {
			for(String key : addrInfo.keySet()) {
				locInfo.put(key, addrInfo.get(key));
			}
		}
		
		return locInfo;
	}
	
}