package guitests.guihandles;

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

import guitests.GuiRobot;
import javafx.scene.Node;
import javafx.scene.control.Labeled;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
import seedu.address.model.tag.UniqueTagList;
import seedu.address.model.task.ReadOnlyTask;

/**
 * Provides a handle to a task card in the task list panel.
 */
public class TaskCardHandle extends GuiHandle {
    private static final String NAME_FIELD_ID = "#name";
    private static final String DEADLINE_FIELD_ID = "#deadline";
    private static final String DESCRIPTION_FIELD_ID = "#description";
    private static final String ID_FIELD_ID = "#id";
    private static final String TAGS_FIELD_ID = "#tags";

    private Node node;

    public TaskCardHandle(GuiRobot guiRobot, Stage primaryStage, Node node) {
        super(guiRobot, primaryStage, null);
        this.node = node;
    }

    protected String getTextFromLabel(String fieldId) {
        return getTextFromLabel(fieldId, node);
    }

    public String getName() {
        return getTextFromLabel(NAME_FIELD_ID);
    }

    public String getDeadline() {
        return getTextFromLabel(DEADLINE_FIELD_ID);
    }

    public String getDescription() {
        return getTextFromLabel(DESCRIPTION_FIELD_ID);
    }

    public String getID() {
        return getTextFromLabel(ID_FIELD_ID);
    }

    public List<String> getTags() {
        return getTags(getTagsContainer());
    }

    private List<String> getTags(Region tagsContainer) {
        return tagsContainer
                .getChildrenUnmodifiable()
                .stream()
                .map(node -> ((Labeled) node).getText())
                .collect(Collectors.toList());
    }

    private List<String> getTags(UniqueTagList tags) {
        return tags
                .asObservableList()
                .stream()
                .map(tag -> tag.tagName)
                .collect(Collectors.toList());
    }

    private Region getTagsContainer() {
        return guiRobot.from(node).lookup(TAGS_FIELD_ID).query();
    }

    public boolean isSameTask(ReadOnlyTask task) {
        return getName().equals(task.getName().toString())
                && getDeadline().equals(task.getDeadline().toString())
                && getDescription().equals(task.getDescription().toString())
                && getID().equals(task.getID().toString())
                && getTags().equals(getTags(task.getTags()));
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof TaskCardHandle) {
            TaskCardHandle handle = (TaskCardHandle) obj;
            return getName().equals(handle.getName())
                    && getDeadline().equals(handle.getDeadline())
                    && getDescription().equals(handle.getDescription())
                    && getID().equals(handle.getID())
                    && getTags().equals(handle.getTags());
        }
        return super.equals(obj);
    }

    @Override
    public String toString() {
        return getName() + " " + getID();
    }
}

@@ -19,24 +19,50 @@
 uihandles;%0A%0A
+import java.util.HashSet;%0A
 import java.
@@ -2167,24 +2167,406 @@
 ask task) %7B%0A
+        /*System.out.println(getID() + task.getID().toString());%0A        System.out.println(getName() + task.getName().toString());%0A        System.out.println(getDeadline() + task.getDeadline().toString());%0A        System.out.println(getDescription() + task.getDescription().toString());%0A        System.out.println(getTags());%0A        System.out.println(getTags(task.getTags()));*/%0A
         retu
@@ -2838,33 +2838,61 @@
      && 
-getTags().equals(
+compareStringListOrderInsensitive(getTags(), 
 getTags(
@@ -3342,16 +3342,50 @@
      && 
+compareStringListOrderInsensitive(
 getTags(
@@ -3385,22 +3385,24 @@
 etTags()
-.equal
+, getTag
 s(handle
@@ -3404,32 +3404,33 @@
 andle.getTags())
+)
 ;%0A        %7D%0A    
@@ -3458,32 +3458,218 @@
 ls(obj);%0A    %7D%0A%0A
+    public boolean compareStringListOrderInsensitive(List%3CString%3E list1, List%3CString%3E list2) %7B%0A        return list1 == list2 %7C%7C new HashSet%3C%3E(list1).equals(new HashSet%3C%3E(list2));%0A    %7D%0A%0A
     @Override%0A  

Add method to compare string list order-insensitively to TaskCardHandle