package seedu.jimi.storage;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import seedu.jimi.commons.exceptions.IllegalValueException;
import seedu.jimi.model.ReadOnlyTaskBook;
import seedu.jimi.model.tag.Tag;
import seedu.jimi.model.tag.UniqueTagList;
import seedu.jimi.model.task.ReadOnlyTask;
import seedu.jimi.model.task.UniqueTaskList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * An Immutable TaskBook that is serializable to XML format
 */
@XmlRootElement(name = "jimi")
public class XmlSerializableTaskBook implements ReadOnlyTaskBook {

    @XmlElement
    private List<XmlAdaptedTask> tasks;
    @XmlElement
    private List<XmlAdaptedTask> deadlineTasks;
    @XmlElement
    private List<XmlAdaptedTask> events;
    @XmlElement
    private List<Tag> tags;

    {
        tasks = new ArrayList<>();
        deadlineTasks = new ArrayList<>();
        events = new ArrayList<>();
        tags = new ArrayList<>();
    }

    /**
     * Empty constructor required for marshalling
     */
    public XmlSerializableTaskBook() {}

    /**
     * Conversion
     */
    public XmlSerializableTaskBook(ReadOnlyTaskBook src) {
        tasks.addAll(src.getTaskList().stream().map(XmlAdaptedTask::new).collect(Collectors.toList()));
        tags = src.getTagList();
    }

    @Override
    public UniqueTagList getUniqueTagList() {
        try {
            return new UniqueTagList(tags);
        } catch (UniqueTagList.DuplicateTagException e) {
            //TODO: better error handling
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public UniqueTaskList getUniqueTaskList() {
        UniqueTaskList lists = new UniqueTaskList();
        for (XmlAdaptedTask p : tasks) {
            try {
                lists.add(p.toModelType());
            } catch (IllegalValueException e) {
                //TODO: better error handling
            }
        }
        return lists;
    }
    
    @Override
    public UniqueTaskList getUniqueDeadlineTaskList() {
        UniqueTaskList lists = new UniqueTaskList();
        for (XmlAdaptedTask p : deadlineTasks) {
            try {
                lists.add(p.toModelType());
            } catch (IllegalValueException e) {
                //TODO: better error handling
            }
        }
        return lists;
    }
    
    @Override
    public UniqueTaskList getUniqueEventList() {
        UniqueTaskList lists = new UniqueTaskList();
        for (XmlAdaptedTask p : events) {
            try {
                lists.add(p.toModelType());
            } catch (IllegalValueException e) {
                //TODO: better error handling
            }
        }
        return lists;
    }

    @Override
    public List<ReadOnlyTask> getTaskList() {
        return tasks.stream().map(p -> {
            try {
                return p.toModelType();
            } catch (IllegalValueException e) {
                e.printStackTrace();
                //TODO: better error handling
                return null;
            }
        }).collect(Collectors.toCollection(ArrayList::new));
    }
    
    @Override
    public List<ReadOnlyTask> getDeadlineTaskList() {
        return deadlineTasks.stream().map(p -> {
            try {
                return p.toModelType();
            } catch (IllegalValueException e) {
                e.printStackTrace();
                //TODO: better error handling
                return null;
            }
        }).collect(Collectors.toCollection(ArrayList::new));
    }
    
    @Override
    public List<ReadOnlyTask> getEventList() {
        return events.stream().map(p -> {
            try {
                return p.toModelType();
            } catch (IllegalValueException e) {
                e.printStackTrace();
                //TODO: better error handling
                return null;
            }
        }).collect(Collectors.toCollection(ArrayList::new));
    }

    @Override
    public List<Tag> getTagList() {
        return Collections.unmodifiableList(tags);
    }

}

@@ -219,16 +219,53 @@
 skBook;%0A
+import seedu.jimi.model.event.Event;%0A
 import s
@@ -328,24 +328,110 @@
 queTagList;%0A
+import seedu.jimi.model.task.DeadlineTask;%0Aimport seedu.jimi.model.task.FloatingTask;%0A
 import seedu
@@ -1475,24 +1475,250 @@
 toList()));%0A
+        deadlineTasks.addAll(src.getDeadlineTaskList().stream().map(XmlAdaptedTask::new).collect(Collectors.toList()));%0A        events.addAll(src.getEventList().stream().map(XmlAdaptedTask::new).collect(Collectors.toList()));%0A
         tags
@@ -2204,32 +2204,209 @@
           try %7B%0A
+                ReadOnlyTask toAdd = p.toModelType();%0A                if(!(toAdd instanceof DeadlineTask) && !(toAdd instanceof Event) && (toAdd instanceof FloatingTask)) %7B%0A    
                 
@@ -2425,32 +2425,50 @@
 toModelType());%0A
+                %7D%0A
             %7D ca
@@ -2784,32 +2784,173 @@
           try %7B%0A
+                ReadOnlyTask toAdd = p.toModelType();%0A                if(!(toAdd instanceof Event) && (toAdd instanceof DeadlineTask)) %7B%0A    
                 
@@ -2969,32 +2969,50 @@
 toModelType());%0A
+                %7D%0A
             %7D ca
@@ -3314,32 +3314,173 @@
           try %7B%0A
+                ReadOnlyTask toAdd = p.toModelType();%0A                if(!(toAdd instanceof DeadlineTask) && (toAdd instanceof Event)) %7B%0A    
                 
@@ -3499,32 +3499,50 @@
 toModelType());%0A
+                %7D%0A
             %7D ca
@@ -3807,38 +3807,252 @@
         
-return p.toModelType()
+ReadOnlyTask toAdd = p.toModelType();%0A                if(!(toAdd instanceof DeadlineTask) && !(toAdd instanceof Event) && (toAdd instanceof FloatingTask)) %7B%0A                    return toAdd;%0A                %7D%0A                return null
 ;%0A      
@@ -4442,38 +4442,216 @@
         
-return p.toModelType()
+ReadOnlyTask toAdd = p.toModelType();%0A                if(!(toAdd instanceof Event) && (toAdd instanceof DeadlineTask)) %7B%0A                    return toAdd;%0A                %7D%0A                return null
 ;%0A      
@@ -5031,30 +5031,208 @@
     
-return p.toModelType()
+ReadOnlyTask toAdd = p.toModelType();%0A                if(!(toAdd instanceof DeadlineTask) && (toAdd instanceof Event)) %7B%0A                    return toAdd;%0A                %7D%0A                return null
 ;%0A  

Implement loading methods for each list