{-
Copyright (C) 2013 John Lenz <lenz@math.uic.edu>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
-}

-- | Settings are centralized, as much as possible, into this file. This
-- includes database connection settings, static file locations, etc.
-- In addition, you can configure a number of different aspects of Yesod
-- by overriding methods in the Yesod typeclass. That instance is
-- declared in the Foundation.hs file.
module Settings where

import Prelude
import Data.Aeson
import Text.Shakespeare.Text (st)
import Language.Haskell.TH.Syntax
import Yesod.Default.Config
import Yesod.Default.Util
import Data.Text (Text)
import qualified Data.Text.Encoding as TE
import Data.ByteString (ByteString)
import Data.Yaml
import Control.Applicative
import Data.Default (def)
import Text.Hamlet

-- Static setting below. Changing these requires a recompile

-- | The location of static files on your system. This is a file system
-- path. The default value works properly with your scaffolded site.
staticDir :: FilePath
staticDir = "static"

-- | The base URL for your static files. As you can see by the default
-- value, this can simply be "static" appended to your application root.
-- A powerful optimization can be serving static files from a separate
-- domain name. This allows you to use a web server optimized for static
-- files, more easily set expires and cache values, and avoid possibly
-- costly transference of cookies on static files. For more information,
-- please see:
--   http://code.google.com/speed/page-speed/docs/request.html#ServeFromCookielessDomain
--
-- If you change the resource pattern for StaticR in Foundation.hs, you will
-- have to make a corresponding change here.
--
-- To see how this value is used, see urlRenderOverride in Foundation.hs
staticRoot :: AppConfig DefaultEnv x -> Text
staticRoot conf = [st|#{appRoot conf}/static|]

-- | Settings for 'widgetFile', such as which template languages to support and
-- default Hamlet settings.
widgetFileSettings :: WidgetFileSettings
widgetFileSettings = def
    { wfsHamletSettings = defaultHamletSettings
        { hamletNewlines = AlwaysNewlines
        }
    }

-- The rest of this file contains settings which rarely need changing by a
-- user.

widgetFile :: String -> Q Exp
widgetFile = (if development then widgetFileReload
                             else widgetFileNoReload)
              widgetFileSettings

data RetagEntry = RetagEntry
    { retagName :: Text
    , retagIcon :: Text
    , retagAdd :: [String]
    , retagRemove :: [String]
    } deriving Show

instance ToJSON RetagEntry where
    toJSON r = object [ "name" .= retagName r
                      , "icon" .= retagIcon r
                      , "add" .= retagAdd r
                      , "remove" .= retagRemove r
                      ]
instance FromJSON RetagEntry where
    parseJSON (Object o) = RetagEntry <$> o .: "name"
                                      <*> o .: "icon"
                                      <*> o .:? "add" .!= []
                                      <*> o .:? "remove" .!= []
    parseJSON _ = fail "Retag is not an object"

data Extra = Extra
    { extraHashedPwd  :: ByteString
    , extraFolders :: [(Text, String)]
    , extraRetag :: [RetagEntry]
    , extraFromAddress :: Text
    , extraSentBox :: Maybe FilePath
    , extraMessageIDDomain :: Text
    } deriving Show

parseExtra :: DefaultEnv -> Object -> Parser Extra
parseExtra _ o = Extra
    <$> (TE.encodeUtf8 <$> o .: "hashed-password")
    <*> (o .: "folders" >>= mapM parseFolder)
    <*> o .: "retag"
    <*> o .: "from-address"
    <*> o .:? "sent-box"
    <*> o .:? "message-id-domain" .!= "" 

parseFolder :: Object -> Parser (Text, String)
parseFolder o = (,) <$> o .: "name"
                    <*> o .: "search"

development :: Bool
development =
#if DEVELOPMENT
  True
#else
  False
#endif

production :: Bool
production = not development