fa8a9c024f6da81cf7a9e5b3de8da27882e44613
[rhynodge.git] / src / main / kotlin / net / pterodactylus / rhynodge / filters / webpages / savoy / MovieState.kt
1 package net.pterodactylus.rhynodge.filters.webpages.savoy
2
3 import com.fasterxml.jackson.annotation.JsonProperty
4 import kotlinx.html.a
5 import kotlinx.html.body
6 import kotlinx.html.div
7 import kotlinx.html.dom.serialize
8 import kotlinx.html.head
9 import kotlinx.html.html
10 import kotlinx.html.img
11 import kotlinx.html.li
12 import kotlinx.html.ol
13 import kotlinx.html.section
14 import kotlinx.html.style
15 import kotlinx.html.title
16 import kotlinx.html.ul
17 import kotlinx.html.unsafe
18 import net.pterodactylus.rhynodge.Reaction
19 import net.pterodactylus.rhynodge.states.AbstractState
20 import java.time.LocalDateTime
21 import java.util.Locale
22
23 class MovieState(@JsonProperty val movies: Collection<Movie>, val newMovies: Collection<Movie> = emptySet(), private val triggered: Boolean = false) : AbstractState() {
24
25         private constructor() : this(emptySet())
26
27         override fun summary(reaction: Reaction) =
28                 "%s – Programme for %tY-%<tm-%<td".format(reaction.name(), earliestMovie?.earliestPerformance)
29
30         override fun plainText() = ""
31
32         override fun htmlText() = kotlinx.html.dom.createHTMLDocument().html {
33                 head {
34                         title { +"Savoy Programme" }
35                         style("text/css") {
36                                 unsafe {
37                                         +"html { font-family: 'Recursive Sans Linear Static', Roboto, serif; }"
38                                         +"section.new-movies > .label { font-family: Impact, sans-serif; background-color: black; padding: 0.5ex; font-size: 200%; color: #ffffee; font-weight: bold; }"
39                                         +"section.new-movies ul { padding: 0; margin: 0; }"
40                                         +"section.new-movies li.movie { list-style: none; display: grid; margin: 1ex 1ex 1ex 0ex; grid-template-rows: 353px; grid-template-columns: 250px auto; }"
41                                         +"section.new-movies li.movie img { grid-area: 1/1/2/2 }"
42                                         +"section.new-movies li.movie .text { color: white; text-shadow: 2px 2px black; font-weight: bold; font-size: 150%; display: flex; flex-direction: column; justify-content: end; grid-area: 1/1/2/2; }"
43                                         +"section.new-movies li.movie .text .name { padding: 1ex; font-size: 120%; }"
44                                         +"section.new-movies li.movie .description { padding: 1ex; }"
45
46                                         +"section.daily-programmes ol { padding: 0; }"
47                                         +"section.daily-programmes li { list-style: none; }"
48                                         +"section.daily-programmes li.day > .label { font-family: Impact, sans-serif; background-color: black; padding: 0.5ex; font-size: 200%; color: #ffffee; font-weight: bold; }"
49                                         +"section.daily-programmes li .performances { display: flex; border-top: 1ex; }"
50                                         +"section.daily-programmes li.performance { width: 250px; height: 353px; display: inline-block; margin: 1ex 1ex 1ex 0ex; position: relative; }"
51                                         +"section.daily-programmes li.performance a { width: 100%; height: 100%; display: block; text-decoration: none; }"
52                                         +"section.daily-programmes li.performance a img { width: 100%; height: 100%; display: block; position: absolute; z-index: -1; }"
53                                         +"section.daily-programmes li.performance a .text { color: white; text-shadow: 2px 2px black; font-weight: bold; font-size: 150%; width: 100%; height: 100%; }"
54                                         +"section.daily-programmes li.performance a .text .time { padding: 1ex 1ex 0ex 1ex; text-align: right; }"
55                                         +"section.daily-programmes li.performance a .text .type { padding: 0ex 1ex 1ex 1ex; text-align: right; }"
56                                         +"section.daily-programmes li.performance a .text .name { padding: 1ex; font-size: 120%; bottom: 0px; position: absolute; }"
57                                 }
58                         }
59                 }
60                 body {
61                         if (newMovies.isNotEmpty()) {
62                                 section("new-movies") {
63                                         div("label") {
64                                                 +"New Movies"
65                                         }
66                                         ul {
67                                                 newMovies.forEach { movie ->
68                                                         li("movie") {
69                                                                 img(src = movie.imageUrl)
70                                                                 div("text") {
71                                                                         div("name") {
72                                                                                 +(movie.name)
73                                                                         }
74                                                                 }
75                                                                 div("description") {
76                                                                         +movie.description
77                                                                 }
78                                                         }
79                                                 }
80                                         }
81                                 }
82                         }
83
84                         section("daily-programmes") {
85
86                                 ol("days") {
87                                         movies.flatMap { it.performances.map(Performance::getTime).map(LocalDateTime::toLocalDate) }.distinct().sorted().forEach { date ->
88                                                 li("day") {
89                                                         attributes += "data-date" to "%tY-%<tm-%<td".format(date)
90                                                         div("label") {
91                                                                 +("Programme for %tA, %<tY-%<tm-%<td".format(Locale.ENGLISH, date))
92                                                         }
93                                                         ol("performances") {
94                                                                 movies
95                                                                         .flatMap { movie -> movie.performances.map { movie to it } }
96                                                                         .filter { (movie, performances) -> performances.time.toLocalDate() == date }
97                                                                         .sortedBy { (_, performance) -> performance.time }
98                                                                         .forEach { (movie, performance) ->
99                                                                                 li("performance") {
100                                                                                         a(href = performance.link) {
101                                                                                                 img(src = movie.imageUrl)
102                                                                                                 div("text") {
103                                                                                                         div("time") {
104                                                                                                                 +("%tH:%<tM".format(performance.time))
105                                                                                                         }
106                                                                                                         div("type") {
107                                                                                                                 +performance.type
108                                                                                                         }
109                                                                                                         div("name") {
110                                                                                                                 +(movie.name)
111                                                                                                         }
112                                                                                                 }
113                                                                                         }
114                                                                                 }
115                                                                         }
116                                                         }
117                                                 }
118                                         }
119                                 }
120                         }
121                 }
122
123         }.serialize()
124
125         override fun triggered() = newMovies.isNotEmpty() || triggered
126
127         private val earliestMovie = movies.minByOrNull { it.earliestPerformance ?: LocalDateTime.MAX }
128         private val Movie.earliestPerformance: LocalDateTime? get() = performances.minOfOrNull(Performance::getTime)
129
130 }