package seedu.jimi.storage;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;

import seedu.jimi.commons.exceptions.IllegalValueException;
import seedu.jimi.model.datetime.DateTime;
import seedu.jimi.model.event.Event;
import seedu.jimi.model.tag.Tag;
import seedu.jimi.model.tag.UniqueTagList;
import seedu.jimi.model.task.DeadlineTask;
import seedu.jimi.model.task.FloatingTask;
import seedu.jimi.model.task.Name;
import seedu.jimi.model.task.ReadOnlyTask;

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

    @XmlElement(required = true)
    private String name;
    @XmlElement(required = true)
    private String isCompleted;
    
    @XmlElement(required = true)
    private String taskDeadline;
    @XmlElement(required = true)
    private String eventStartDatetime;
    @XmlElement(required = true)
    private String eventEndDatetime;
    
    @XmlElement
    private List<XmlAdaptedTag> tagged = new ArrayList<>();

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


    /**
     * Converts a given FloatingTask 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());
        
        if (source instanceof DeadlineTask) {
            taskDeadline = ((DeadlineTask) source).getDeadline().toString();
        } else if (source instanceof Event) {
            eventStartDatetime = ((Event) source).getStart().toString();
            eventEndDatetime = ((Event) source).getEnd().toString();
        } else {
            /* Set all null since floating task */
            taskDeadline = eventStartDatetime = eventEndDatetime = null;
        }
        tagged = new ArrayList<>();
        for (Tag tag : source.getTags()) {
            tagged.add(new XmlAdaptedTag(tag));
        }
    }

    /**
     * Converts this jaxb-friendly adapted task object into the model's FloatingTask object.
     *
     * @throws IllegalValueException if there were any data constraints violated in the adapted task
     */
    public ReadOnlyTask 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 UniqueTagList tags = new UniqueTagList(taskTags);
        final boolean isCompletedBoolean = Boolean.parseBoolean(isCompleted);
        ReadOnlyTask toConvert;
        
        if (taskDeadline == null && eventStartDatetime == null) { // floating
            toConvert = new FloatingTask(name, tags);
            ((FloatingTask) toConvert).setCompleted(isCompletedBoolean);
        } else if (taskDeadline != null) { // deadline task
            toConvert = new DeadlineTask(name, new DateTime(taskDeadline), tags);
            ((DeadlineTask) toConvert).setCompleted(isCompletedBoolean);
        } else {
            toConvert = new Event(
                    name, 
                    new DateTime(eventStartDatetime), 
                    new DateTime(eventEndDatetime), 
                    tags);
            ((Event) toConvert).setCompleted(isCompletedBoolean);
        }
        
        return toConvert;
    }
}

@@ -642,16 +642,21 @@
 ing name
+ = %22%22
 ;%0A    @X
@@ -712,16 +712,21 @@
 ompleted
+ = %22%22
 ;%0A    %0A 
@@ -788,16 +788,21 @@
 Deadline
+ = %22%22
 ;%0A    @X
@@ -861,24 +861,29 @@
 tartDatetime
+ = %22%22
 ;%0A    @XmlEl
@@ -940,16 +940,21 @@
 Datetime
+ = %22%22
 ;%0A    %0A 
@@ -1711,32 +1711,32 @@
         
-eventE
+DateTime e
 ndDate
-time
  = ((Eve
@@ -1759,126 +1759,69 @@
 nd()
-.toString();%0A        %7D else %7B%0A            /* Set all null since floating task */%0A            taskDeadline =
+;%0A            if (endDate != null) %7B%0A               
  event
-Start
+End
 Date
@@ -1832,31 +1832,40 @@
  = e
-ventE
 ndDate
-time = null;
+.toString();%0A            %7D
 %0A   
@@ -1863,32 +1863,41 @@
     %7D%0A        %7D%0A
+        %0A
         tagged =
@@ -2725,24 +2725,26 @@
 Deadline
- == null
+.isEmpty()
  && even
@@ -2761,16 +2761,18 @@
 time
- == null
+.isEmpty()
 ) %7B 
@@ -2838,63 +2838,10 @@
 tags
-);%0A            ((FloatingTask) toConvert).setCompleted(
+, 
 isCo
@@ -2872,24 +2872,25 @@
  %7D else if (
+!
 taskDeadline
@@ -2889,24 +2889,26 @@
 Deadline
- != null
+.isEmpty()
 ) %7B // d
@@ -3003,63 +3003,10 @@
 tags
-);%0A            ((DeadlineTask) toConvert).setCompleted(
+, 
 isCo
@@ -3233,18 +3233,18 @@
     tags
-);
+, 
 %0A       
@@ -3252,41 +3252,16 @@
     
-((Event) toConvert).setCompleted(
+        
 isCo

Initialize all XmlElements properly