package com.oguerreiro.resilient.activity.registry;

import java.time.Instant;

import com.oguerreiro.resilient.security.SecurityUtils;

public class ActivityProgressDescriptor {
  public static final String INITIAL_PROGRESS_TASK = "INITIALIZING";

  private Instant start;
  private Instant end;
  private String username;
  private Instant lastTick; // Just an evolution time based, even if there's no progress info. at least, we
                           // know the process is still alive and working.

  private Integer progressPercentage;
  private String progressTask;

  public ActivityProgressDescriptor(Instant start, String username) {
    this.start = start;

    // If username is null, use the currently logged user
    if (username == null || username.trim().length() == 0) {
      username = SecurityUtils.getCurrentUserLogin().orElseThrow();
    }
    this.username = username;

    // Initialize percentage
    this.progressPercentage = Integer.valueOf(0);
    this.progressTask = INITIAL_PROGRESS_TASK;
  }

  // Properties
  public Instant getStart() {
    return start;
  }

  public Instant getEnd() {
    return end;
  }

  public String getUsername() {
    return username;
  }

  public Instant getLastTick() {
    return lastTick;
  }

  // Methods
  public void tick() {
    this.lastTick = Instant.now();
  }

  public Integer getProgressPercentage() {
    return this.progressPercentage;
  }

  public String getProgressTask() {
    return this.progressTask;
  }

  public void updateProgress(Integer progress, String task) {
    this.progressPercentage = progress;
    this.progressTask = task;
  }
}
