package pl.tomaszdziurko.jvm_bloggers.mailing;

import com.google.common.base.Preconditions;

import lombok.Getter;

import org.apache.commons.lang3.StringUtils;
import pl.tomaszdziurko.jvm_bloggers.blog_posts.domain.BlogPost;
import pl.tomaszdziurko.jvm_bloggers.blogs.domain.Blog;

@Getter
class BlogPostForMailItem {
    
    private String title;
    private String url;
    private String authorLabel;
    private Long issueNumber;

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

    public static class Builder {

        private final BlogPostForMailItem instance = new BlogPostForMailItem();

        public Builder from(BlogPost blogPost) {
            withTitle(blogPost.getTitle());
            withUrl(blogPost.getUrl());
            withAuthorLabel(blogPost.getBlog());
            return this;
        }

        public Builder withTitle(String title) {
            instance.title = title;
            return this;
        }

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

        public Builder withAuthorLabel(Blog blog) {
            instance.authorLabel = determineAuthorLabel(blog);
            return this;
        }

        public Builder withIssueNumber(long issueNumber) {
            instance.issueNumber = issueNumber;
            return this;
        }

        public BlogPostForMailItem build() {
            Preconditions.checkState(StringUtils.isNotBlank(instance.title),
                    "Tittle cannot be empty.");
            Preconditions.checkState(StringUtils.isNotBlank(instance.url), "Url cannot be empty.");
            Preconditions.checkState(StringUtils.isNotBlank(instance.authorLabel),
                    "Author cannot be empty.");
            return instance;
        }

        private String determineAuthorLabel(Blog blogger) {
            if (StringUtils.isNotBlank(blogger.getTwitter())) {
                return String.format(
                    "<a href=\"https://twitter.com/%s\">%s</a>",
                    blogger.getTwitter().substring(1),
                    blogger.getAuthor()
                );
            } else {
                return blogger.getAuthor();
            }
        }
    }
}


@@ -1520,32 +1520,40 @@
 ittle cannot be 
+NULL or 
 empty.%22);%0A      
@@ -1620,16 +1620,36 @@
 ce.url),
+%0A                   
  %22Url ca
@@ -1648,32 +1648,40 @@
  %22Url cannot be 
+NULL or 
 empty.%22);%0A      
@@ -1753,24 +1753,24 @@
 thorLabel),%0A
-
             
@@ -1795,16 +1795,24 @@
 nnot be 
+NULL or 
 empty.%22)

Update BlogPostForMailItem.java