package com.style24.batch.biz.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.style24.batch.biz.dao.TsbMonitoringDao;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Alarm;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.parameter.GagaMap;

/**
 * 모니터링 Service
 * 
 * @author gagamel
 * @since 2020. 11. 13
 */
@Service
@Slf4j
public class TsbMonitoringService {

	@Autowired
	private TsbMonitoringDao monitoringDao;

	@Autowired
	private Environment env;

	/**
	 * 시스템알람SMS 생성
	 * @param alarmId - 알람ID
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 13
	 */
	@Transactional("shopTxnManager")
	public void createSystemAlarmSms(String alarmId) {
		Alarm params = new Alarm();
		params.setAlarmId(alarmId);
		params.setFuserid(String.valueOf(TsbConstants.REG_NO));
		params.setFcallback(TscConstants.CALLCENTER_TEL_NO);

		String profile = env.getProperty("spring.profiles.active");

		if ("run".equals(profile) || "style".equals(profile) || "brdg".equals(profile)) {
			monitoringDao.createAlarmSms(params);
		} else {
			monitoringDao.createAlarmSmsDev(params);
		}
	}

	/**
	 * 시스템알람SMS 생성
	 * @param alarmId - 알람ID
	 * @param batchNm - 배치명
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 13
	 */
	@Transactional(value = "shopTxnManager", isolation = Isolation.SERIALIZABLE, propagation = Propagation.NESTED)
	public void createSystemAlarmSms(String alarmId, String batchNm) {
		Alarm params = new Alarm();
		params.setAlarmId(alarmId);
		params.setBatchNm(batchNm);

		// 알람메시지 조회
		String alarmMsg = monitoringDao.getAlarmMessage(params);
		params.setAlarmMsg(alarmMsg);

		params.setFuserid(String.valueOf(TsbConstants.REG_NO));
		params.setFcallback(TscConstants.CALLCENTER_TEL_NO);

		String profile = env.getProperty("spring.profiles.active");
		if ("run".equals(profile) || "style".equals(profile) || "brdg".equals(profile)) {
			monitoringDao.createAlarmSms(params);
		} else {
			monitoringDao.createAlarmSmsDev(params);
		}
	}

	/**
	 * 시스템알람SMS 생성
	 * @param alarmId - 알람ID
	 * @param replaceInfo - 메시지내용 중 대체할 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 8. 13
	 */
	@Transactional("shopTxnManager")
	public void createSystemAlarmSms(String alarmId, GagaMap replaceInfo) {
		Alarm params = new Alarm();
		params.setAlarmId(alarmId);

		// 알람메시지 조회
		String alarmMsg = monitoringDao.getAlarmMessage(params);
		params.setAlarmMsg(this.mergeData(alarmMsg, replaceInfo));

		params.setFuserid(String.valueOf(TsbConstants.REG_NO));
		params.setFcallback(TscConstants.CALLCENTER_TEL_NO);

		String profile = env.getProperty("spring.profiles.active");
		if ("run".equals(profile) || "style".equals(profile) || "brdg".equals(profile)) {
			monitoringDao.createAlarmSms(params);
		} else {
			monitoringDao.createAlarmSmsDev(params);
		}
	}

	/**
	 * 메시지 문자열 중에 '#{'로 시작하고 '}'로 끝나는 부분을 대체할 정보(replaceInfo)로 대체해 반환한다.
	 * @param message - 메시지 문자열
	 * @param replaceInfo - 알림톡 메시지 중 대체할 정보
	 * @return
	 * @author gagamel
	 * @date 2018. 1. 21.
	 */
	private String mergeData(String message, GagaMap replaceInfo) {
		List<String> keyList = getKeyList(message, "#{", "}");

		String key = "";
		String value = "";

		if (!keyList.isEmpty()) {
			for (int i = 0; i < keyList.size(); i++) {
				key = keyList.get(i);
				value = replaceInfo.getString(key);
				message = message.replace("#{" + key + "}", value);
			}
		}

		return message;
	}

	/**
	 * 입력 문자열로부터 시작 태그와 종료 태그 안의 key 목록을 반환한다.
	 * @param inputStr - 입력 문자열
	 * @param startTag - 시작 태그
	 * @param endTag - 종료 태그
	 * @return
	 * @author gagamel
	 * @date 2018. 1. 21.
	 */
	private List<String> getKeyList(String inputStr, String startTag, String endTag) {
		List<String> keyList = new ArrayList<>();

		String targetStr = inputStr;

		while (targetStr.indexOf(startTag) > -1) {
			int startPos = targetStr.indexOf(startTag) + startTag.length();
			int endPos = targetStr.indexOf(endTag, startPos);

			keyList.add(targetStr.substring(startPos, endPos));
			targetStr = targetStr.substring(endPos + endTag.length());
		}

		return keyList;
	}

	/**
	 * 1일내 회원가입건수 조회
	 * @param siteCd - 사이트코드
	 * @return
	 * @author gagamel
	 * @since 2020. 5. 18
	 */
	public int getJoinCountWithinOneDay(String siteCd) {
		return monitoringDao.getJoinCountWithinOneDay(siteCd);
	}

	/**
	 * 1시간내 로그인건수 조회
	 * @param siteCd - 사이트코드
	 * @return
	 * @author gagamel
	 * @since 2020. 5. 18
	 */
	public int getLoginCountWithinOneHour(String siteCd) {
		return monitoringDao.getLoginCountWithinOneHour(siteCd);
	}

	/**
	 * 1시간내 유입건수 조회
	 * @param siteCd - 사이트코드
	 * @return
	 * @author gagamel
	 * @since 2020. 5. 18
	 */
	public int getInthrowCountWithinOneHour(String siteCd) {
		return monitoringDao.getInthrowCountWithinOneHour(siteCd);
	}

	/**
	 * 2시간내 결제건수 조회
	 * @return
	 * @author gagamel
	 * @since 2020. 5. 18
	 */
	public int getPayCountWithinTwoHour() {
		return monitoringDao.getPayCountWithinTwoHour();
	}

	/**
	 * 1일내 외부몰주문수집건수 조회
	 * @return
	 * @author gagamel
	 * @since 2020. 5. 18
	 */
	public int getExtmallOrderCollectionCountWithinOneDay() {
		return monitoringDao.getExtmallOrderCollectionCountWithinOneDay();
	}

}
