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;

	/* 
	 * 이용약관을 조회한다. 
	 * 
	 */
	public HashMap<String, String> selectInfoContent(String orgCd, String contentType) throws Exception {
		
		if(StringUtil.isEmpty(orgCd)) orgCd = NlibProperty.getString("nlib.council.cd");
		
		HashMap<String, String> ret = informDAO.selectInfoContent(orgCd, contentType);
		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 orgCd) throws Exception {
		
		return selectInfoContent(orgCd, "TERMS");
	}

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

		return selectInfoContent(orgCd, "PRIVACY");
	}

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

		return selectInfoContent(orgCd, "GREETING");
	}

	/* 
	 * 이용안내를 조회한다. 
	 * 
	 */
	public HashMap<String, String> selectNCultureOperationInfo(String orgCd) throws Exception {

		HashMap<String, String> operationInfo = new HashMap<String, String>();
		
		HashMap<String, String> envValues = informDAO.selectOperationEnv(orgCd);
		
		// 대출안내 : 전체 문화원 공통 사항 
		HashMap<String, String> operation = selectInfoContent(NlibProperty.getString("nlib.council.cd"), "OPERATION");
		String content = (operation != null ? operation.get("CONTENT") : "");
		
		if(envValues != null && StringUtil.isNotEmpty(content)) {
			
			for(String key : envValues.keySet()) {
				content = content.replaceAll("%" + key + "%", StringUtil.getString(envValues.get(key), ""));
			}
		}
		
		// 문화원별 개폐시간 및 휴관일 
		HashMap<String, String> openTime = selectNCultureOpenTime(orgCd);
		
		operationInfo.put("informTitle"   , (operation != null ? operation.get("TITLE") : ""));
		operationInfo.put("informContent" , content);
		operationInfo.put("openTime"      , (openTime != null ? openTime.get("OPEN_TIME") : ""));
		operationInfo.put("closeTime"     , (openTime != null ? openTime.get("CLOSED_TIME") : ""));
		operationInfo.put("closingInfo"   , (openTime != null ? openTime.get("CLOSING_INFO") : ""));
		
		return operationInfo;
	}
	
	/**
	 * 해당 문화원의 운영 시간 및 공휴일 추가 정보를 조회한다. 
	 * 
	 * @param orgCd
	 * @return
	 * @throws Exception
	 */
	public HashMap<String, String> selectNCultureOpenTime(String orgCd) throws Exception {
		return informDAO.selectNCultureOpenTime(orgCd);
	}

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

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