package com.atlassian.webdriver.bitbucket.element;

import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.timeout.TimeoutType;
import org.openqa.selenium.By;

import java.util.ArrayList;
import java.util.List;

public class FileBreadcrumbs extends LegacyElementPageObject {

    public static final String className = "breadcrumbs";

    public FileBreadcrumbs(PageElement container) {
        super(container);
    }

    public FileBreadcrumbs(By locator, TimeoutType timeoutType) {
        super(locator, timeoutType);
    }

    public List<Part> getParts() {
        List<LinkElement> links = findAll(By.tagName("a"), LinkElement.class);
        List<Part> parts = new ArrayList<Part>(links.size() + 1);
        for (LinkElement element : links) {
            parts.add(new Part(element.getText(), element.getUrl()));
        }
        PageElement stub = this.find(By.className("stub"));
        parts.add(new Part(stub.getText(), null));
        return parts;
    }

    public static class Part {
        private final String name;
        private final String href;

        public Part(String name, String href) {
            this.name = name;
            this.href = href;
        }

        public String getName() {
            return name;
        }

        public String getHref() {
            return href;
        }
    }

}
