package seedu.task.storage;

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

import seedu.task.commons.exceptions.IllegalValueException;
import seedu.task.model.ReadOnlyTaskManager;
import seedu.task.model.tag.Tag;
import seedu.task.model.tag.UniqueTagList;
import seedu.task.model.task.ReadOnlyTask;
import seedu.task.model.task.UniqueMarkedTaskList;
import seedu.task.model.task.UniqueTaskList;

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

/**
 * An Immutable TaskManager that is serializable to XML format
 */
@XmlRootElement(name = "taskmanager")
public class XmlSerializableTaskManager implements ReadOnlyTaskManager {

    @XmlElement
    private List<XmlAdaptedTask> tasks;
    @XmlElement
    private List<XmlAdaptedTask> markedTasks;
    @XmlElement
    private List<Tag> tags;

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

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

    /**
     * Conversion
     */
    public XmlSerializableTaskManager(ReadOnlyTaskManager src) {
        tasks.addAll(src.getTaskList().stream().map(XmlAdaptedTask::new).collect(Collectors.toList()));
        markedTasks.addAll(src.getMarkedTaskList().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 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<Tag> getTagList() {
        return Collections.unmodifiableList(tags);
    }

  //@@author A0127720M
	@Override
	public UniqueMarkedTaskList getUniqueMarkedList() {
        UniqueMarkedTaskList lists = new UniqueMarkedTaskList();
        for (XmlAdaptedTask p : markedTasks) {
            try {
                lists.add(p.toModelType());
            } catch (IllegalValueException e) {
                //TODO: better error handling
            }
        }
        return lists;
	}

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

}

@@ -1132,16 +1132,21 @@
 ager() %7B
+%0A    
 %7D%0A%0A    /
@@ -1681,32 +1681,33 @@
 %7B%0A            //
+ 
 TODO: better err
@@ -2069,32 +2069,33 @@
               //
+ 
 TODO: better err
@@ -2413,32 +2413,33 @@
               //
+ 
 TODO: better err
@@ -2669,20 +2669,23 @@
 %0A    %7D%0A%0A
+  
   //
+ 
 @@author
@@ -2695,17 +2695,20 @@
 127720M%0A
-%09
+    
 @Overrid
@@ -2705,25 +2705,28 @@
   @Override%0A
-%09
+    
 public Uniqu
@@ -2997,32 +2997,33 @@
               //
+ 
 TODO: better err
@@ -3084,13 +3084,19 @@
 ts;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 @Ove
@@ -3101,17 +3101,20 @@
 verride%0A
-%09
+    
 public L
@@ -3361,16 +3361,17 @@
       //
+ 
 TODO: be
@@ -3498,9 +3498,12 @@
 ));%0A
-%09
+    
 %7D%0A%0A%7D

Fix small indentation errors