package ch.zhaw.gartenverwaltung.types;


import java.time.LocalDate;
import java.util.Optional;

/**
 * Models a Task
 * May be created using the builder pattern.
 */
public class Task {
    private long id;
    private final String name;
    private final String description;
    private final LocalDate startDate;
    private Integer interval;
    private LocalDate endDate;
    private long cropId;

    /**
     * default constructor
     * (used by Json deserializer)
     */
    public Task(){
        name= "";
        description= "";
        startDate = LocalDate.now();
    }

    public Task(String name, String description, LocalDate startDate, long cropId) {
        this.name = name;
        this.description = description;
        this.startDate = startDate;
        this.cropId = cropId;
    }

    public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval, long cropId) {
        this.name = name;
        this.description = description;
        this.startDate = startDate;
        this.endDate = endDate;
        this.interval = interval;
        this.cropId = cropId;
    }

    // Builder-pattern-style setters
    public Task withId(long id) {
        this.id = id;
        return this;
    }
    public Task withInterval(int interval) {
        this.interval = interval;
        return this;
    }
    public Task withEndDate(LocalDate endDate) {
        this.endDate = endDate;
        return this;
    }

    public boolean isInTimePeriode(LocalDate searchStartDate, LocalDate searchEndDate){
        return startDate.isAfter(searchStartDate) && startDate.isBefore(searchEndDate);
    }

    // Getters
    public long getId() { return id; }
    public String getName() { return name; }
    public String getDescription() { return description; }
    public LocalDate getStartDate() { return startDate; }
    public long getCropId() { return cropId; }

    public Optional<Integer> getInterval() {
        return Optional.ofNullable(interval);
    }
    public Optional<LocalDate> getEndDate() {
        return Optional.ofNullable(endDate);
    }
}
