package seedu.agendum.storage;

import seedu.agendum.commons.exceptions.IllegalValueException;
import seedu.agendum.model.tag.Tag;
import seedu.agendum.model.tag.UniqueTagList;
import seedu.agendum.model.task.*;

import javax.xml.bind.annotation.XmlElement;
import java.util.ArrayList;
import java.util.List;

/**
 * JAXB-friendly version of the Task.
 */
public class XmlAdaptedTask {

    @XmlElement(required = true)
    private String name;
    @XmlElement(required = true)
    private String isCompleted;

    @XmlElement
    private List<XmlAdaptedTag> tagged = new ArrayList<>();

    /**
     * No-arg constructor for JAXB use.
     */
    public XmlAdaptedTask() {}


    /**
     * Converts a given Task into this class for JAXB use.
     *
     * @param source future changes to this will not affect the created XmlAdaptedTask
     */
    public XmlAdaptedTask(ReadOnlyTask source) {
        name = source.getName().fullName;
        isCompleted = Boolean.toString(source.isCompleted());
        tagged = new ArrayList<>();
        for (Tag tag : source.getTags()) {
            tagged.add(new XmlAdaptedTag(tag));
        }
    }

    /**
     * Converts this jaxb-friendly adapted task object into the model's Task object.
     *
     * @throws IllegalValueException if there were any data constraints violated in the adapted task
     */
    public Task toModelType() throws IllegalValueException {
        final List<Tag> taskTags = new ArrayList<>();
        for (XmlAdaptedTag tag : tagged) {
            taskTags.add(tag.toModelType());
        }
        final Name name = new Name(this.name);
        final boolean markedAsCompleted = Boolean.valueOf(isCompleted);
        final UniqueTagList tags = new UniqueTagList(taskTags);
        
        Task newTask = new Task(name,tags);
        if (markedAsCompleted) {
            newTask.markAsCompleted();
        }
        return newTask;
    }
}

@@ -250,16 +250,92 @@
 Element;
+%0A%0Aimport java.time.LocalDateTime;%0Aimport java.time.format.DateTimeFormatter;
 %0Aimport 
@@ -377,16 +377,43 @@
 il.List;
+%0Aimport java.util.Optional;
 %0A%0A/**%0A *
@@ -483,16 +483,107 @@
 Task %7B%0A%0A
+    private DateTimeFormatter formatter = DateTimeFormatter.ofPattern(%22yyyy-MM-dd HH:mm%22);%0A
     @Xml
@@ -696,16 +696,150 @@
 mpleted;
+%0A    @XmlElement(required = false)%0A    private String startDateTime;%0A    @XmlElement(required = false)%0A    private String endDateTime;
 %0A%0A    @X
@@ -1348,32 +1348,310 @@
  ArrayList%3C%3E();%0A
+        if (source.getStartDateTime().isPresent()) %7B%0A            startDateTime = source.getStartDateTime().get().format(formatter);%0A        %7D%0A        if (source.getEndDateTime().isPresent()) %7B%0A            endDateTime = source.getEndDateTime().get().format(formatter);%0A        %7D%0A
         for (Tag
@@ -2429,24 +2429,24 @@
 ompleted) %7B%0A
-
             
@@ -2474,32 +2474,342 @@
 ed();%0A        %7D%0A
+        if (startDateTime != null) %7B%0A            newTask.setStartDateTime(Optional.ofNullable(LocalDateTime.parse(this.startDateTime, formatter)));%0A        %7D%0A        if (endDateTime != null) %7B%0A            newTask.setEndDateTime(Optional.ofNullable(LocalDateTime.parse(this.endDateTime, formatter)));%0A        %7D%0A
         return n

update xml adapted task to store time