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 customer - 고객정보
	 * @return 고객정보
	 * @author gagamel
	 * @since 2020. 2. 3
	 */
	public Login getLoginCheckInfo(Login customer) {
		customer.setIpAddr(TsfSession.getIpAddress());
		customer.setSiteCd(TscConstants.Site.STYLE24.value());
		return loginDao.getLoginCheckInfo(customer);
	}

	/**
	 * 로그인이력 남기기
	 * @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
	 * @param loginFailYn - 로그인실패여부(Y:실패, N:성공)
	 * @author gagamel
	 * @since 2020. 2. 3
	 */
	@Transactional("shopTxnManager")
	public void createLoginFail(String custId, String loginFailYn) {
		Login customer = new Login();
		customer.setCustId(custId);
		customer.setIpAddr(TsfSession.getIpAddress());
		customer.setSiteCd(TscConstants.Site.STYLE24.value());
		customer.setLoginFailYn(loginFailYn);

		loginDao.createLoginFail(customer);
	}

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

		return loginDao.getLoginFailCount(login);
	}

	/**
	 * 로그인유지토큰 생성
	 * @param custNo - 고객번호
	 * @param password - 비밀번호
	 * @param expiry - 만료기간
	 * @author gagamel
	 * @since 2020. 6. 17
	 */
	@Transactional("shopTxnManager")
	public void createPersistentToken(Integer custNo, String remembermeToken, Date expiry) {
		PersistentToken token = PersistentToken.builder()
			.custNo(custNo)
			.remembermeToken(remembermeToken)
			.limitDt(expiry)
			.build();

		loginDao.createPersistentToken(token);
	}

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

	/**
	 * 로그인유지토큰 갱신
	 * @param token - 토큰 정보
	 * @author gagamel
	 * @since 2020. 6. 17
	 */
	@Transactional("shopTxnManager")
	public void updatePersistentToken(Integer custNo, String prevRemembermeToken, String remembermeToken, Date expiry) {
		PersistentToken token = PersistentToken.builder()
			.custNo(custNo)
			.prevRemembermeToken(prevRemembermeToken)
			.remembermeToken(remembermeToken)
			.limitDt(expiry)
			.build();

		loginDao.updatePersistentToken(token);
	}

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

}
