🐛 Fix broken change detection
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / ComicState.java
index ffc38b5..0e661bc 100644 (file)
 
 package net.pterodactylus.rhynodge.states;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 
+import net.pterodactylus.rhynodge.Reaction;
 import net.pterodactylus.rhynodge.states.ComicState.Comic;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.google.common.collect.Lists;
+import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.commons.lang3.StringUtils;
+
+import static java.lang.String.format;
 
 /**
  * {@link net.pterodactylus.rhynodge.State} that can store an arbitrary amout of
@@ -33,47 +45,108 @@ import com.google.common.collect.Lists;
  */
 public class ComicState extends AbstractState implements Iterable<Comic> {
 
-       /** The comics in this state. */
        @JsonProperty
        private final List<Comic> comics = Lists.newArrayList();
+       private final Set<Comic> newComics = new HashSet<>();
 
-       /**
-        * Returns the list of comics contained in this state.
-        *
-        * @return The list of comics in this state
-        */
-       public List<Comic> comics() {
-               return comics;
+       @SuppressWarnings("unused")
+       // used for deserialization
+       private ComicState() {
        }
 
-       /**
-        * Adds the given comic to this state.
-        *
-        * @param comic
-        *              The comic to add
-        * @return This comic state
-        */
-       public ComicState add(Comic comic) {
-               comics.add(comic);
-               return this;
+       public ComicState(Collection<Comic> allComics) {
+               this.comics.addAll(allComics);
        }
 
-       //
-       // ITERABLE METHODS
-       //
+       public ComicState(Collection<Comic> allComics, Collection<Comic> newComics) {
+               this(allComics);
+               this.newComics.addAll(newComics);
+       }
+
+       @Override
+       public boolean isEmpty() {
+               return comics.isEmpty();
+       }
+
+       @Override
+       public boolean triggered() {
+               return !newComics.isEmpty();
+       }
+
+       public List<Comic> comics() {
+               return comics;
+       }
 
        @Override
        public Iterator<Comic> iterator() {
                return comics.iterator();
        }
 
-       //
-       // OBJECT METHODS
-       //
-
        @Override
        public String toString() {
-               return String.format("ComicState[comics=%s]", comics());
+               return format("ComicState[comics=%s]", comics());
+       }
+
+       @Nonnull
+       @Override
+       protected String summary(Reaction reaction) {
+               return format("New Comic found for “%s!”", reaction.name());
+       }
+
+       @Nonnull
+       @Override
+       protected String plainText() {
+               StringBuilder text = new StringBuilder();
+
+               for (Comic newComic : newComics) {
+                       text.append("Comic Found: ").append(newComic.title()).append("\n\n");
+                       for (Strip strip : newComic) {
+                               text.append("Image: ").append(strip.imageUrl()).append("\n");
+                               if (!StringUtils.isBlank(strip.comment())) {
+                                       text.append("Comment: ").append(strip.comment()).append("\n");
+                               }
+                       }
+                       text.append("\n\n");
+               }
+
+               return text.toString();
+       }
+
+       @Nullable
+       @Override
+       protected String htmlText() {
+               StringBuilder html = new StringBuilder();
+               html.append("<body>");
+
+               for (Comic newComic : newComics) {
+                       generateComicHtml(html, newComic);
+               }
+
+               List<Comic> latestComics = new ArrayList<>(comics());
+               Collections.reverse(latestComics);
+               int comicCount = 0;
+               for (Comic comic : latestComics) {
+                       if (newComics.contains(comic)) {
+                               continue;
+                       }
+                       generateComicHtml(html, comic);
+                       if (++comicCount == 7) {
+                               break;
+                       }
+               }
+
+               return html.append("</body>").toString();
+       }
+
+       private void generateComicHtml(StringBuilder html, Comic comic) {
+               html.append("<h1>").append(StringEscapeUtils.escapeHtml4(comic.title())).append("</h1>\n");
+               for (Strip strip : comic) {
+                       html.append("<div><img src=\"").append(StringEscapeUtils.escapeHtml4(strip.imageUrl()));
+                       html.append("\" alt=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
+                       html.append("\" title=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
+                       html.append("\"></div>\n");
+                       html.append("<div>").append(StringEscapeUtils.escapeHtml4(strip.comment())).append("</div>\n");
+               }
        }
 
        /**
@@ -83,67 +156,34 @@ public class ComicState extends AbstractState implements Iterable<Comic> {
         */
        public static class Comic implements Iterable<Strip> {
 
-               /** The title of the comic. */
                @JsonProperty
                private final String title;
 
-               /** The strips of the comic. */
                @JsonProperty
                private final List<Strip> strips = Lists.newArrayList();
 
-               /**
-                * Creates a new comic with the given title.
-                *
-                * @param title
-                *              The title of the comic
-                */
                public Comic(@JsonProperty("title") String title) {
                        this.title = title;
                }
 
-               /**
-                * Returns the title of this comic.
-                *
-                * @return The title of this comic
-                */
                public String title() {
                        return title;
                }
 
-               /**
-                * Returns the strips of this comic.
-                *
-                * @return The strips of this comic
-                */
                public List<Strip> strips() {
                        return strips;
                }
 
-               /**
-                * Adds a strip to this comic.
-                *
-                * @param strip
-                *              The strip to add
-                * @return This comic
-                */
                public Comic add(Strip strip) {
                        strips.add(strip);
                        return this;
                }
 
-               //
-               // ITERABLE METHODS
-               //
-
                @Override
                public Iterator<Strip> iterator() {
                        return strips.iterator();
                }
 
-               //
-               // OBJECT METHODS
-               //
-
                @Override
                public int hashCode() {
                        return title.hashCode() ^ strips().hashCode();
@@ -160,7 +200,7 @@ public class ComicState extends AbstractState implements Iterable<Comic> {
 
                @Override
                public String toString() {
-                       return String.format("Comic[title=%s,strips=%s]", title(), strips());
+                       return format("Comic[title=%s,strips=%s]", title(), strips());
                }
 
        }
@@ -172,49 +212,25 @@ public class ComicState extends AbstractState implements Iterable<Comic> {
         */
        public static class Strip {
 
-               /** The URL of the image. */
                @JsonProperty
                private final String imageUrl;
 
-               /** The comment of the image. */
                @JsonProperty
                private final String comment;
 
-               /**
-                * Creates a new strip.
-                *
-                * @param imageUrl
-                *              The URL of the image
-                * @param comment
-                *              The comment of the image
-                */
                public Strip(@JsonProperty("imageUrl") String imageUrl, @JsonProperty("comment") String comment) {
                        this.imageUrl = imageUrl;
                        this.comment = comment;
                }
 
-               /**
-                * Returns the URL of the image.
-                *
-                * @return The URL of the image
-                */
                public String imageUrl() {
                        return imageUrl;
                }
 
-               /**
-                * Returns the comment of the image.
-                *
-                * @return The comment of the image
-                */
                public String comment() {
                        return comment;
                }
 
-               //
-               // OBJECT METHODS
-               //
-
                @Override
                public int hashCode() {
                        return imageUrl().hashCode() ^ comment().hashCode();
@@ -231,7 +247,7 @@ public class ComicState extends AbstractState implements Iterable<Comic> {
 
                @Override
                public String toString() {
-                       return String.format("Strip[imageUrl=%s,comment=%s]", imageUrl(), comment());
+                       return format("Strip[imageUrl=%s,comment=%s]", imageUrl(), comment());
                }
 
        }