module Conf
  ( get
  , Conf(..)
  ) where

import qualified Data.ConfigManager as Conf
import           Data.Text          (Text)
import qualified Data.Text          as T
import           Data.Time.Clock    (NominalDiffTime)

import           Common.Model       (Currency (..))

data Conf = Conf
  { hostname         :: Text
  , port             :: Int
  , signInExpiration :: NominalDiffTime
  , currency         :: Currency
  , noReplyMail      :: Text
  , https            :: Bool
  , devMode          :: Bool
  } deriving Show

get :: FilePath -> IO Conf
get path = do
  conf <-
    (flip fmap) (Conf.readConfig path) (\configOrError -> do
      conf <- configOrError
      Conf <$>
        Conf.lookup "hostname" conf <*>
        Conf.lookup "port" conf <*>
        Conf.lookup "signInExpiration" conf <*>
        fmap Currency (Conf.lookup "currency" conf) <*>
        Conf.lookup "noReplyMail" conf <*>
        Conf.lookup "https" conf <*>
        Conf.lookup "devMode" conf
    )
  case conf of
    Left msg -> error (T.unpack msg)
    Right c  -> return c