package com.openbanking.eai.common.token;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;
import java.util.Date;

/** 
* 1. 기능 : OAuth2 토큰 정보를 표현하는 Java Value Object 클래스.
* 2. 처리 개요 : OAuth2 토큰 정보를 정의한다.
* 3. 주의사항 
*
* @author  : 
* @version : v 1.0.0
* @see : 관련 기능을 참조
* @since   : 
*  : 
*/
public class OAuth2AccessTokenVO implements AccessTokenVO, Serializable
{ 
    private String accessToken;
    private String tokenType;
    private Date expiration;
    private String scope;
    private String clientUseCode;
    private String clientId;
    
    public OAuth2AccessTokenVO() {
    	
    }
    
    public OAuth2AccessTokenVO(OAuth2AccessTokenVO oAuth2AccessTokenVO) {
    	this.accessToken = oAuth2AccessTokenVO.getAccessToken();
    	this.tokenType   = oAuth2AccessTokenVO.getTokenType();
    	this.expiration  = oAuth2AccessTokenVO.getExpiration();
    	this.scope       = oAuth2AccessTokenVO.getScope();
    	this.clientUseCode = oAuth2AccessTokenVO.getClientUseCode();
    	this.clientId = oAuth2AccessTokenVO.getClientId();
    }

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public String getAuthorization() {
		return String.format("%s %s", BEARER_TYPE, accessToken);
	}
	
	public String getTokenType() {
		return tokenType;
	}

	public void setTokenType(String tokenType) {
		if(tokenType.equalsIgnoreCase(BEARER_TYPE))
			this.tokenType = BEARER_TYPE;
		else
			this.tokenType = tokenType;
	}

	public long getExpiresIn() {
		return expiration == null ? 0 : Long.valueOf((expiration.getTime() - System.currentTimeMillis()) / 1000L);
	}

	public void setExpiresIn(long expiresIn) {
		setExpiration(new Date(System.currentTimeMillis() + (expiresIn * 1000L)));
	}

	public Date getExpiration() {
		return expiration;
	}

	public void setExpiration(Date expiration) {
		this.expiration = expiration;
	}

	public boolean isExpired() {
        return expiration != null && expiration.before(new Date());
    }
	
	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getClientUseCode() {
		return clientUseCode;
	}

	public void setClientUseCode(String clientUseCode) {
		this.clientUseCode = clientUseCode;
	}
	
	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}
    
    public String toString() {
    	return ReflectionToStringBuilder.toString(this, ToStringStyle.DEFAULT_STYLE);
    }
} 
