package com.style24.front.biz.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.style24.core.support.env.TscConstants;
import com.style24.front.biz.dao.TsfLoginDao;
import com.style24.front.support.security.session.TsfSession;
import com.style24.persistence.domain.Login;
import com.style24.persistence.domain.PersistentToken;

import lombok.extern.slf4j.Slf4j;

/**
 * 로그인 Service
 * 
 * @author gagamel
 * @since 2020. 9. 11
 */
@Service
@Slf4j
public class TsfLoginService {

	@Autowired
	private TsfLoginDao loginDao;

	/**
	 * 로그인체크정보 조회
	 * @param login - 로그인 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 2. 3
	 */
	public Login getLoginCheckInfo(Login login) {
		login.setIpAddr(TsfSession.getIpAddress());
		login.setSiteCd(TscConstants.Site.STYLE24.value());
		return loginDao.getLoginCheckInfo(login);
	}

	/**
	 * 로그인이력 남기기
	 * @param custNo - 고객번호
	 * @author gagamel
	 * @since 2020. 2. 3
	 */
	@Transactional("shopTxnManager")
	public void createLoginHistory(Integer custNo) {
		Login login = new Login();
		login.setCustNo(custNo);
		login.setIpAddr(TsfSession.getIpAddress());
		login.setSiteCd(TscConstants.Site.STYLE24.value());
		login.setFrontGb(TsfSession.getFrontGb());

		loginDao.createLoginHistory(login);
	}

	/**
	 * 최종로그인일시 Update
	 * @param custNo - 고객번호
	 * @author gagamel
	 * @since 2020. 2. 3
	 */
	@Transactional("shopTxnManager")
	public void updateLastLoginDate(Integer custNo) {
		loginDao.updateLastLoginDate(custNo);
	}

	/**
	 * 로그인실패 남기기. 로그인실패여부가 "N:성공"이면 실패건수 0으로 초기화
	 * @param custId - 고객ID(=로그인ID)
	 * @param loginFailYn - 로그인실패여부(Y:실패, N:성공)
	 * @author gagamel
	 * @since 2020. 2. 3
	 */
	@Transactional("shopTxnManager")
	public void createLoginFail(String custId, String loginFailYn) {
		Login login = new Login();
		login.setCustId(custId);
		login.setIpAddr(TsfSession.getIpAddress());
		login.setSiteCd(TscConstants.Site.STYLE24.value());
		login.setLoginFailYn(loginFailYn);

		loginDao.createLoginFail(login);
	}

	/**
	 * 로그인 실패건수 조회
	 * @param custId - 고객ID(=로그인ID)
	 * @return 로그인 실패건수
	 * @author gagamel
	 * @since 2020. 2. 3
	 */
	public int getLoginFailCount(String custId) {
		Login login = new Login();
		login.setCustId(custId);
		login.setIpAddr(TsfSession.getIpAddress());
		login.setSiteCd(TscConstants.Site.STYLE24.value());

		return loginDao.getLoginFailCount(login);
	}

	/**
	 * 로그인실패정보 조회
	 * @param custId - 고객ID(=로그인ID)
	 * @author gagamel
	 * @since 2021. 2. 16
	 */
	public Login getLoginFailInfo(String custId) {
		Login login = new Login();
		login.setCustId(custId);
		login.setIpAddr(TsfSession.getIpAddress());
		login.setSiteCd(TscConstants.Site.STYLE24.value());
		return loginDao.getLoginFailInfo(login);
	}

	/**
	 * 로그인실패 건수가 10회 이상이고 3분 지났으면 실패횟수 초기화
	 * @param custId - 고객ID(=로그인ID)
	 * @author jsshin
	 * @since 2021. 03. 31
	 */
	public void updateLoginFailInfo(String custId) {
		Login login = new Login();
		login.setCustId(custId);
		login.setIpAddr(TsfSession.getIpAddress());
		login.setSiteCd(TscConstants.Site.STYLE24.value());
		loginDao.updateLoginFailInfo(login);
	}

	/**
	 * 로그인유지토큰 생성
	 * @param custNo - 고객번호
	 * @param remembermeToken - Rememberme토큰
	 * @param expiry - 만료일시
	 * @author gagamel
	 * @since 2020. 6. 17
	 */
	@Transactional("shopTxnManager")
	public void createPersistentToken(Integer custNo, String remembermeToken, String expiry) {
		PersistentToken token = new PersistentToken();
		token.setCustNo(custNo);
		token.setRemembermeToken(remembermeToken);
		token.setLimitDt(expiry);
		loginDao.createPersistentToken(token);
	}

	/**
	 * 로그인유지토큰 가져오기
	 * @param remembermeToken - RememberMe토큰
	 * @return
	 * @author gagamel
	 * @since 2020. 6. 17
	 */
	public PersistentToken getPersistentToken(String remembermeToken) {
		return loginDao.getPersistentToken(remembermeToken);
	}

	/**
	 * 로그인유지토큰 갱신
	 * @param custNo - 고객번호
	 * @param prevRemembermeToken - 기존 Rememberme토큰
	 * @param remembermeToken - Rememberme토큰
	 * @param expiry - 만료일시
	 * @author gagamel
	 * @since 2020. 6. 17
	 */
	@Transactional("shopTxnManager")
	public void updatePersistentToken(Integer custNo, String prevRemembermeToken, String remembermeToken, String expiry) {
		PersistentToken token = new PersistentToken();
		token.setCustNo(custNo);
		token.setPrevRemembermeToken(prevRemembermeToken);
		token.setRemembermeToken(remembermeToken);
		token.setLimitDt(expiry);
		loginDao.updatePersistentToken(token);
	}

	/**
	 * 로그인유지토큰 삭제
	 * @param remembermeToken - RememberMe토큰
	 * @author gagamel
	 * @since 2020. 6. 17
	 */
	@Transactional("shopTxnManager")
	public void deletePersistentToken(String remembermeToken) {
		loginDao.deletePersistentToken(remembermeToken);
	}

}
