package ru.ibs.socrates.confexporter.external.access.service;

import java.time.Instant;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;


/**
 * Хранилище для работы с access токеном.
 */
@Component
@RequiredArgsConstructor
public class AccessTokenHolder {

  private volatile String token;
  private volatile String tokenWithType;
  private volatile Instant expired;
  @Getter
  private volatile Instant acquired;

  public String getTokenWithType() {
    return tokenWithType;
  }

  /**
   * Задать токен.
   * @param token
   * @param expired
   * @param acquired
   */
  public void setToken(String token, Instant expired, Instant acquired) {
    this.token = token;
    this.tokenWithType = "Bearer ".concat(token);
    this.expired = expired;
    this.acquired = acquired;
  }

  /**
   * Проверка что токен существует в хранилище.
   * @return
   */
  public boolean tokenNotExistsOrExpired() {
    return token == null
        || expired == null
        || expired.isBefore(Instant.now());
  }

}
