package se.westcoastcode.scat.integration.job;

import se.westcoastcode.scat.integration.WithIdentity;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static java.util.UUID.randomUUID;
import static org.apache.commons.lang.StringUtils.isNotEmpty;

public class Job implements WithIdentity {
    private final UUID id;
    private final String name;
    private final String image;
    private final List<Action> actions;
    private final String user;
    private final int executionCount;

    public Job(String name, String image, List<Action> actions, String user, int executionCount) {
        this.id = randomUUID();
        this.name = name;
        this.image = image;
        this.actions = actions;
        this.user = user;
        this.executionCount = executionCount;
    }

    public Job(UUID id, String name, String image, List<Action> actions, String user, int executionCount) {
        this.id = id;
        this.name = name;
        this.image = image;
        this.actions = actions;
        this.user = user;
        this.executionCount = executionCount;
    }

    public UUID getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Job setName(String name) {
        return new Job(id, name, image, actions, user, executionCount);
    }

    public String getImage() {
        return image;
    }

    public Job setImage(final String image) {
        return new Job(id, name, image, actions, user, executionCount);
    }

    public List<Action> getActions() {
        return actions;
    }

    public Job setActions(final List<Action> actions) {
        return new Job(id, name, image, actions, user, executionCount);
    }

    /**
     * Add an action to this job
     *
     * @param action The new action
     * @return
     */
    public Job addAction(Action action) {
        final List<Action> newActions = new ArrayList<>(actions);
        newActions.add(action);
        return new Job(id, name, image, actions, user, executionCount);
    }

    /**
     * Retrieve the action given the supplied name
     *
     * @param name The action name
     * @return
     */
    public Optional<Action> getAction(final String name) {
        return actions.stream()
                .filter(a -> a.getName().equals(name))
                .findFirst();
    }

    public String getUser() {
        return user;
    }

    public Job setUser(final String user) {
        return new Job(id, name, image, actions, user, executionCount);
    }

    public int getExecutionCount() {
        return executionCount;
    }

    public Job increaseExecutionCount() {
        return new Job(id, name, image, actions, user, executionCount + 1);
    }

    public Path getDataPath() {
        return Paths.get(id + ".xml");
    }

    /**
     * @return If this image is valid
     */
    public boolean isValid() {
        return id != null && isNotEmpty(name) && isNotEmpty(image) && !this.actions.isEmpty();
    }
}
