package seedu.taskell.model.task;

import java.util.Objects;

import seedu.taskell.commons.exceptions.IllegalValueException;
import seedu.taskell.commons.util.CollectionUtil;
import seedu.taskell.model.tag.UniqueTagList;

/**
 * Represents an Event task in the task manager.
 * Guarantees: details are present and not null, field values are validated.
 */
public class EventTask extends Task {
    public static final String MESSAGE_EVENT_CONSTRAINTS = "Start date and time must be before end date and time"
            + "\nAll date and time should not before current time";

    public EventTask(String description, String startDate, String endDate, String startTime, String endTime, String taskPriority, UniqueTagList tags) throws IllegalValueException {
        this(new Description(description),
                EVENT_TASK,
                new TaskDate(startDate),
                new TaskDate(endDate),
                new TaskTime(startTime),
                new TaskTime(endTime),
                new TaskPriority(taskPriority),
                tags);
    }
    /**
     * Every field must be present and not null.
     * @throws IllegalValueException 
     */
    public EventTask(Description description, String taskType, TaskDate startDate, TaskDate endDate, TaskTime startTime, TaskTime endTime, TaskPriority taskPriority, UniqueTagList tags) throws IllegalValueException {
        if (startDate.equals(endDate) && startTime.isAfter(endTime)) {
            endDate = endDate.getNextDay();
        } else if (startDate.getDayNameInWeek().equals(new TaskDate(TaskDate.getTodayDate()).getDayNameInWeek())) {
            endDate = endDate.getNextWeek();
        }
        
        if (!isValidEventDuration(startDate, endDate, startTime, endTime)) {
            throw new IllegalValueException(MESSAGE_EVENT_CONSTRAINTS);
        }
        
        this.description = description;
        this.taskType = EVENT_TASK;
        this.startDate = startDate;
        this.endDate = endDate;
        this.startTime = startTime;
        this.endTime = endTime;
        this.taskPriority = taskPriority;
        this.tags = tags;
    }
    
    private boolean isValidEventDuration(TaskDate startDate, TaskDate endDate, TaskTime startTime, TaskTime endTime) {
        try {
            TaskDate today = new TaskDate(TaskDate.getTodayDate());
            
            if (startDate.isBefore(today) || endDate.isBefore(today)) {
                return false;
            } else if (startDate.isAfter(endDate)) {
                return false;
            } else {
                return true;
            }
        } catch (IllegalValueException ive) {
            return false;
        }
    }

    @Override
    public boolean equals(Object other) {
        return other == this // short circuit if same object
                || (other instanceof ReadOnlyTask // instanceof handles nulls
                && this.isSameStateAs((ReadOnlyTask) other));
    }
}
@@ -1391,285 +1391,76 @@
     
-if (startDate.equals(endDate) && startTime.isAfter(endTime)) %7B%0A            endDate = endDate.getNextDay();%0A        %7D else if (startDate.getDayNameInWeek().equals(new TaskDate(TaskDate.getTodayDate()).getDayNameInWeek())) %7B%0A            endDate = endDate.getNextWeek();%0A        %7D
+endDate = autoAdjustEndDate(startDate, endDate, startTime, endTime);
 %0A   
@@ -2474,16 +2474,720 @@
  %7D%0A    %7D
+%0A    %0A    /**%0A     * Adjust the endDate such that it fits into the real-world context%0A     * @throws IllegalValueException%0A     */%0A    private TaskDate autoAdjustEndDate(TaskDate startDate, TaskDate endDate, TaskTime startTime, TaskTime endTime) throws IllegalValueException %7B%0A        TaskDate today = new TaskDate(TaskDate.getTodayDate());%0A        if (startDate.equals(endDate) && startTime.isAfter(endTime)) %7B%0A            endDate = endDate.getNextDay();%0A        %7D else if (startDate.getDayNameInWeek().equals(today.getDayNameInWeek())%0A                && !endDate.getDayNameInWeek().equals(today.getDayNameInWeek())) %7B%0A            endDate = endDate.getNextWeek();%0A        %7D%0A        return endDate;%0A    %7D
 %0A%0A    @O

Fix issue#103