/*
 * ROMAN DIGITAL - Autor: Américo Patetta
 */
package com.teltonikaServer.persistence;

import java.time.Instant;

/**
 *
 * @author pc
 */
public class Command {

    public static final long TIME_BETWEEN_COMMAND_SENDINGS = 10000;
    public static final long TIME_BETWEEN_COMMAND_CHECKING = 60000;

    private long commandId;
    private String destinationImei;
    private String commandBody;
    private long creationTimestamp;
    private long pickupTimestamp;
    private long sentTimestamp;
    private long ackTimestamp;
    private int retriesLeft;
    private int gap;
    protected boolean updated = false;

    public long getCommandId() {
        return commandId;
    }

    public String getDestinationImei() {
        return destinationImei;
    }

    public String getCommandBody() {
        return commandBody;
    }

    public long getCreationTimestamp() {
        return creationTimestamp;
    }

    public long getPickupTimestamp() {
        return pickupTimestamp;
    }

    public long getSentTimestamp() {
        return sentTimestamp;
    }

    public long getAckTimestamp() {
        return ackTimestamp;
    }

    public void setCommandId(long commandId) {
        this.commandId = commandId;
    }

    public void setDestinationImei(String imei) {
        this.destinationImei = imei;
    }

    public void setCommandBody(String commandBody) {
        this.commandBody = commandBody;
    }

    public void setCreationTimestamp(long creationTimestamp) {
        this.creationTimestamp = creationTimestamp;
    }

    public void setPickupTimestamp(long pickupTimestamp) {
        this.pickupTimestamp = pickupTimestamp;
    }

    public void setSentTimestamp(long sentTimestamp) {
        this.sentTimestamp = sentTimestamp;
    }

    public void setAckTimestamp(long ackTimestamp) {
        this.ackTimestamp = ackTimestamp;
    }

    boolean mustBeSent() {
        long now = Instant.now().getEpochSecond();
        boolean mustBeSent = (this.ackTimestamp == 0) && (this.retriesLeft > 0 && this.sentTimestamp + this.gap < now);
        return mustBeSent;
    }

    public int getRetriesLeft() {
        return retriesLeft;
    }

    public int getGap() {
        return gap;
    }

    public void setRetriesLeft(int retriesLeft) {
        this.retriesLeft = retriesLeft;
    }

    public void setGap(int gap) {
        this.gap = gap;
    }

    public boolean goodToSend(long currentTimestamp) {
        return !(ackTimestamp > 0) && (retriesLeft > 0) && (sentTimestamp + gap < currentTimestamp);
    }

    public boolean isUpdated() {
        return updated;
    }

    public void setUpdated(boolean updated) {
        this.updated = updated;
    }

}
