/*
 * ROMAN DIGITAL - Autor: Américo Patetta
 */
package com.teltonikaServer.network;

import com.teltonikaServer.persistence.Command;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author pc
 */

public class CommandList {

    private final Map<Long, Command> commandList = new HashMap<>();

    public void addCommand(Command aCommand) {
        if (commandList.containsKey(aCommand.getCommandId())) {
            commandList.replace(aCommand.getCommandId(), aCommand);
        } else {
            commandList.put(aCommand.getCommandId(), aCommand);
        }
    }


    
    public Command getNextPendingCommand() {
        Command firstPendingCommand = null;

        if (!commandList.isEmpty()) {

            for (long commandId : commandList.keySet()) {
                //System.out.println("Key: " + commandId + " Value: " + );
                Command c = commandList.get(commandId);
                if (c.goodToSend(Instant.now().getEpochSecond())) {
                    firstPendingCommand = c;
                    break;
                }
            }
            
                
        }
        return firstPendingCommand;
    }

    public boolean remove(Command c) {

        return commandList.remove(c.getCommandId()) != null;

    }
}

