package de.jeha.kame.crawler.core.model;

import de.jeha.kame.crawler.core.LinkExtractor;
import de.jeha.kame.crawler.core.robots.RobotsMetaContentExtractor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.List;

/**
 * @author jenshadlich@googlemail.com
 */
public class Page {

    private final Domain domain;
    private final Hash hash;
    private final String url;
    private final List<String> links;

    public Page(Domain domain, String url, List<String> links) {
        this.domain = domain;
        this.hash = new Hash(url);
        this.url = url;
        this.links = links;
    }

    public Domain getDomain() {
        return domain;
    }

    public Hash getHash() {
        return hash;
    }

    public String getUrl() {
        return url;
    }

    public List<String> getLinks() {
        return links;
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

    // -----------------------------------------------------------------------------------------------------------------

    public static class Builder {

        private static final LinkExtractor LINK_EXTRACTOR = new LinkExtractor();
        private static final RobotsMetaContentExtractor ROBOTS_META_CONTENT_EXTRACTOR = new RobotsMetaContentExtractor();

        private String url;
        private String content;

        public Builder withUrl(String url) {
            this.url = url;
            return this;
        }

        public Builder withContent(String content) {
            this.content = content;
            return this;
        }

        public Page build() {
            Document document = Jsoup.parse(content);
            List<String> links = LINK_EXTRACTOR.get(document);

            String domain = extractDomainName(url);
            return new Page(new Domain(domain), url, links);
        }

        public static Builder New() {
            return new Builder();
        }

        private String extractDomainName(String url) {
            return StringUtils.substringBefore(StringUtils.substringAfter(url, "://"), "/").replaceFirst("^www.*?\\.", "");
        }

    }
}

@@ -398,16 +398,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -614,30 +614,28 @@
  final List%3C
-String
+Link
 %3E links;%0A%0A  
@@ -676,30 +676,28 @@
 g url, List%3C
-String
+Link
 %3E links) %7B%0A 
@@ -1009,22 +1009,20 @@
 ic List%3C
-String
+Link
 %3E getLin
@@ -1955,22 +1955,20 @@
    List%3C
-String
+Link
 %3E links 
@@ -1997,16 +1997,69 @@
 ocument)
+.stream().map(Link::new).collect(Collectors.toList())
 ;%0A%0A     

return a list of type Link