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.label.UniqueLabelList;
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 TITLE_FIELD_ID = "#title";
    private static final String STARTTIME_FIELD_ID = "#startTime";
    private static final String DEADLINE_FIELD_ID = "#deadline";
    private static final String LABELS_FIELD_ID = "#labels";
    private static final String STATUS_FIELD_ID = "#status";

    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 getTitle() {
        return getTextFromLabel(TITLE_FIELD_ID);
    }

    public String getStartTime() {
        return getTextFromLabel(STARTTIME_FIELD_ID);
    }

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

    public List<String> getLabels() {
        return getLabels(getLabelsContainer());
    }

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

    private List<String> getLabels(UniqueLabelList labels) {
        return labels
                .asObservableList()
                .stream()
                .map(label -> label.labelName)
                .collect(Collectors.toList());
    }

    private Region getLabelsContainer() {
        return guiRobot.from(node).lookup(LABELS_FIELD_ID).query();
    }

    public boolean isSameTask(ReadOnlyTask task) {
        assert(task != null);
        boolean result;
        if (task.getDeadline().isPresent() && task.getStartTime().isPresent()
                && this.getDeadline() != null && this.getDeadline() != null) {
            result = getTitle().equals(task.getTitle().title)
                && getDeadline().equals(task.getDeadline().get().toString())
                && getLabels().equals(getLabels(task.getLabels()))
                && getStartTime().equals(task.getStartTime().get().toString())
                && isCompleted().equals(task.isCompleted());
        } else if (task.getDeadline().isPresent() && this.getDeadline() != null) {
            result = getTitle().equals(task.getTitle().title)
                    && getDeadline().equals(task.getDeadline().get().toString())
                    && getLabels().equals(getLabels(task.getLabels()))
                    && isCompleted().equals(task.isCompleted());
        } else {
            result = getTitle().equals(task.getTitle().title)
                    && getLabels().equals(getLabels(task.getLabels()))
                    && isCompleted().equals(task.isCompleted());
        }
        return result;
    }

    private Boolean isCompleted() {
        String text = getTextFromLabel(STATUS_FIELD_ID);
        if (text.equals("completed")) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof TaskCardHandle) {
            TaskCardHandle handle = (TaskCardHandle) obj;
            return getTitle().equals(handle.getTitle())
                    && getStartTime().equals(handle.getStartTime())
                    && getDeadline().equals(handle.getDeadline())
                    && isCompleted().equals(handle.isCompleted())
                    && getLabels().equals(handle.getLabels());
        }
        return super.equals(obj);
    }

    @Override
    public String toString() {
        return getTitle() + " " + getDeadline();
    }
}

@@ -3398,17 +3398,17 @@
 equals(%22
-c
+C
 ompleted

Change typo error in TaskCardHandle