package guitests.guihandles;

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

import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.layout.Region;
import seedu.address.model.tag.Tag;

/**
 * Provides a handle to a person card in the person list panel.
 */
public class PersonCardHandle extends NodeHandle<Node> {
    private static final String ID_FIELD_ID = "#id";
    private static final String NAME_FIELD_ID = "#name";
    private static final String ADDRESS_FIELD_ID = "#address";
    private static final String PHONE_FIELD_ID = "#phone";
    private static final String EMAIL_FIELD_ID = "#email";
    private static final String TAGS_FIELD_ID = "#tags";

    private Label idLabel;
    private Label nameLabel;
    private Label addressLabel;
    private Label phoneLabel;
    private Label emailLabel;
    private Region tagsContainer;

    public PersonCardHandle(Node cardNode) {
        super(cardNode);

        this.idLabel = getChildNode(ID_FIELD_ID);
        this.nameLabel = getChildNode(NAME_FIELD_ID);
        this.addressLabel = getChildNode(ADDRESS_FIELD_ID);
        this.phoneLabel = getChildNode(PHONE_FIELD_ID);
        this.emailLabel = getChildNode(EMAIL_FIELD_ID);
        this.tagsContainer = getChildNode(TAGS_FIELD_ID);
    }

    public String getId() {
        return idLabel.getText();
    }

    public String getName() {
        return nameLabel.getText();
    }

    public String getAddress() {
        return addressLabel.getText();
    }

    public String getPhone() {
        return phoneLabel.getText();
    }

    public String getEmail() {
        return emailLabel.getText();
    }

    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(Set<Tag> tags) {
        return tags
                .stream()
                .map(tag -> tag.tagName)
                .collect(Collectors.toList());
    }

    private Region getTagsContainer() {
        return tagsContainer;
    }

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

@@ -50,30 +50,8 @@
 st;%0A
-import java.util.Set;%0A
 impo
@@ -168,94 +168,21 @@
 ene.
-control.Labeled;%0Aimport javafx.scene.layout.Region;%0Aimport seedu.address.model.tag.Tag
+layout.Region
 ;%0A%0A/
@@ -666,24 +666,30 @@
     private 
+final 
 Label idLabe
@@ -699,24 +699,30 @@
     private 
+final 
 Label nameLa
@@ -734,24 +734,30 @@
     private 
+final 
 Label addres
@@ -772,24 +772,30 @@
     private 
+final 
 Label phoneL
@@ -812,16 +812,22 @@
 private 
+final 
 Label em
@@ -848,36 +848,43 @@
 private 
-Region tagsContainer
+final List%3CLabel%3E tagLabels
 ;%0A%0A    p
@@ -1221,32 +1221,33 @@
 _FIELD_ID);%0A
+%0A
         
 this.tagsCon
@@ -1234,21 +1234,23 @@
         
-this.
+Region 
 tagsCont
@@ -1282,24 +1282,219 @@
 _FIELD_ID);%0A
+        this.tagLabels = tagsContainer%0A                .getChildrenUnmodifiable()%0A                .stream()%0A                .map(Label.class::cast)%0A                .collect(Collectors.toList());%0A
     %7D%0A%0A    p
@@ -1914,172 +1914,17 @@
 urn 
-getTags(getTagsContainer());%0A    %7D%0A%0A    private List%3CString%3E getTags(Region tagsContainer) %7B%0A        return tagsContainer%0A                .getChildrenUnmodifiable()
+tagLabels
 %0A   
@@ -1971,42 +1971,22 @@
 map(
-node -%3E ((Labeled) node).
+Label::
 getText
-()
 )%0A  
@@ -2039,374 +2039,7 @@
    %7D
-%0A%0A    private List%3CString%3E getTags(Set%3CTag%3E tags) %7B%0A        return tags%0A                .stream()%0A                .map(tag -%3E tag.tagName)%0A                .collect(Collectors.toList());%0A    %7D%0A%0A    private Region getTagsContainer() %7B%0A        return tagsContainer;%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        return getName() + %22 %22 + getAddress();%0A    %7D
 %0A%7D%0A

Clean up PersonCardHandle