aboutsummaryrefslogtreecommitdiff
path: root/src/Model/Mail.hs
blob: e730e32326633f46e972a75d3b7aa305315392a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{-# LANGUAGE OverloadedStrings #-}

module Model.Mail
  ( mailSubject
  , mailBody
  ) where

import Data.Text (Text)
import qualified Data.Text as T

import Model.Date
import Model.Birthdate

mailSubject :: [Birthdate] -> Text
mailSubject birthdates =
  let count = length birthdates
  in  T.concat
        [ "Hey, "
        , if count > 1 then "there are" else "there is"
        , " "
        , T.pack . show $ count
        , " birthday"
        , if count > 1 then "s" else ""
        , " today!"
        ]

mailBody :: Date -> [Birthdate] -> Text
mailBody currentDate birthdates =
  T.concat $ map (mailLine currentDate) (attachLines birthdates)

attachLines :: [Birthdate] -> [(Line, Birthdate)]
attachLines birthdates =
  let count = length birthdates
      lineKind 1 = if count == 1 then SingleLine else FirstLine
      lineKind line = if line == count then LastLine else MiddleLine
      mapFst f (x, y) = (f x, y)
  in  map (mapFst lineKind) . zip [1..] $ birthdates

data Line =
  SingleLine
  | FirstLine
  | MiddleLine
  | LastLine
  deriving (Eq, Show)

mailLine :: Date -> (Line, Birthdate) -> Text
mailLine currDate (line, birthdate) =
  T.concat
    [ case line of
        MiddleLine -> ", "
        LastLine -> " and "
        _ -> ""
    , fullname birthdate
    , case line of
        x | x `elem` [SingleLine, FirstLine] -> " is now "
        _ -> " is "
    , T.pack . show $ age currDate birthdate
    , " years old"
    , if line == LastLine then "." else ""
    ]