package com.style24.front.support.security;

import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.style24.persistence.domain.Login;

/**
 * 로그인 상세 정보
 * 		@JsonSerialize 애노테이션을 지정해야 세션을 레디스에 저장할 수 있다.
 * 
 * @author gagamel
 * @since 2020. 9. 11
 */
@SuppressWarnings("serial")
@JsonSerialize
@JsonIgnoreProperties(ignoreUnknown = true)
public class TsfLoginDetails implements UserDetails {

	private Login loginInfo = null;

	private Collection<SimpleGrantedAuthority> authorities = null;

	// 세션을 JSON 형식으로 레디스에 저장하려면 기본 생성자를 반드시 명시해야 한다
	public TsfLoginDetails() {

	}

	public TsfLoginDetails(Login loginInfo, Collection<SimpleGrantedAuthority> authorities) {
		this.loginInfo = loginInfo;
		this.authorities = authorities;

//		if (authorities != null && !authorities.isEmpty()) {
//			isLogin = true;
//		}
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Override
	public String getPassword() {
		return loginInfo.getPasswd();
	}

	@Override
	public String getUsername() {
		return String.valueOf(loginInfo.getCustNo());
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	public boolean isLogin() {
		return loginInfo.getCustNo() != null ? true : false;
	}

	public Login getLoginInfo() {
		return loginInfo;
	}

}
