package de.ivu.elect.business.system.entity;

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

import javax.annotation.Nonnull;
import javax.ejb.Timer;

/**
 *  on 31.10.2016.
 */
public class TimerStatus implements Serializable, Comparable<TimerStatus> {

  private final String timerToString;

  private final String schedule;

  private final Date nextTimeout;

  private final boolean calendarTimer;

  private final boolean persistent;

  private final Serializable info;

  public TimerStatus(Timer timer) {

    nextTimeout = timer.getNextTimeout();
    calendarTimer = timer.isCalendarTimer();
    persistent = timer.isPersistent();

    info = timer.getInfo();

    timerToString = timer.toString();

    if (timer.getSchedule() == null) {
      schedule = null;
    } else {
      schedule = timer.getSchedule().toString();
    }

  }

  public String getTimerToString() {
    return timerToString;
  }

  public String getSchedule() {
    return schedule;
  }

  public Date getNextTimeout() {
    return nextTimeout;
  }

  public boolean isCalendarTimer() {
    return calendarTimer;
  }

  public boolean isPersistent() {
    return persistent;
  }

  public Serializable getInfo() {
    return info;
  }

  @Override
  public int compareTo(@Nonnull TimerStatus other) {
    return this.getTimerToString().compareTo(other.getTimerToString());
  }

  public Long getId() {
    return (long) getTimerToString().hashCode();
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    TimerStatus that = (TimerStatus) o;

    return timerToString != null ? timerToString.equals(that.timerToString) : that.timerToString == null;
  }

  @Override
  public int hashCode() {
    return timerToString != null ? timerToString.hashCode() : 0;
  }
}
