aboutsummaryrefslogtreecommitdiff
path: root/src/View/Plain/Ad.hs
blob: 75e35e253e4d197e9c4a55f223ff1286b8b0f49b (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.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 (URL)
import Conf (Conf)
import qualified Conf

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

renderAds :: Conf -> [Ad] -> (Text, Text)
renderAds conf ads =
  let titleMessage = renderTitle $ length ads
      adsMessage = T.intercalate "\n\n" . map (renderAd conf) $ 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 :: Conf -> Ad -> Text
renderAd conf ad =
  T.concat
    [ renderResume (Ad.resume ad)
    , "\n"
    , renderDetail conf (Ad.detail ad)
    ]

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

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

renderProperties :: [Text] -> Map Text Text -> Text
renderProperties [] _ = ""
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 _ [] = ""
renderURLs title urls =
  T.intercalate "\n" (title:urls)