aboutsummaryrefslogtreecommitdiff
path: root/src/View/Plain/Ad.hs
blob: 5120226e2b5634e2796056bc985102daaee15b7b (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{-# LANGUAGE OverloadedStrings #-}

module View.Plain.Ad
  ( renderConsoleAds
  , renderAds
  ) where

import Data.List (intersperse)
import Data.Maybe (fromMaybe, catMaybes)
import Data.Map (Map)
import qualified Data.Map as M

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

import Model.Ad (Ad)
import qualified Model.Ad as Ad

import Model.Resume (Resume)
import qualified Model.Resume as Resume

import Model.Detail (Detail)
import qualified Model.Detail as Detail

import Model.URL
import Model.Config

renderConsoleAds :: Config -> Text -> [Ad] -> Text
renderConsoleAds config time ads =
  let (title, message) = renderAds config ads
      titleWithTime =
        T.concat
          [ "\n["
          , time
          , "] "
          , title
          ]
      line = T.map (\_ -> '-') titleWithTime
  in  T.intercalate
        "\n"
        [ titleWithTime
        , line
        , ""
        , message
        ]

renderAds :: Config -> [Ad] -> (Text, Text)
renderAds config ads =
  let titleMessage = renderTitle $ length ads
      adsMessage = T.intercalate "\n\n" . map (renderAd config) $ ads
  in  (titleMessage, adsMessage)

renderTitle :: Int -> Text
renderTitle count =
  T.concat
    [ T.pack . show $ count
    , agreement " nouvelle"
    , agreement " annonce"
    ]
  where agreement word =
          T.concat
            [ word
            , if count > 1 then "s" else ""
            ]

renderAd :: Config -> Ad -> Text
renderAd config ad =
  T.concat
    [ renderResume (Ad.resume ad)
    , "\n"
    , renderDetail config (Ad.detail ad)
    ]

renderResume :: Resume -> Text
renderResume resume =
  let formatPrice price = T.concat [" - ", price]
      price = fromMaybe "" . fmap formatPrice . Resume.price $ resume
      titleLine = T.concat [Resume.name resume, price]
  in  T.intercalate "\n" [titleLine, Resume.url resume]

renderDetail :: Config -> Detail -> Text
renderDetail config detail =
  T.concat
    [ renderProperties (properties config) (Detail.properties detail)
    , fromMaybe "−" (Detail.description detail)
    , renderURLs "\n\nImages:" (Detail.images detail)
    ]

renderProperties :: [Text] -> Map Text Text -> Text
renderProperties [] properties = ""
renderProperties keys properties =
  T.concat
    [ "\n"
    , T.concat (catMaybes $ map (renderProperty properties) keys)
    , "\n"
    ]

renderProperty :: Map Text Text -> Text -> Maybe Text
renderProperty properties key =
  fmap
    (\value -> T.concat [key, ": ", value, "\n"])
    (M.lookup key properties)

renderURLs :: Text -> [URL] -> Text
renderURLs title [] = ""
renderURLs title urls =
  T.intercalate "\n" (title:urls)