module Taskell.IO.Config.Markdown
    ( Config(Config)
    , defaultConfig
    , parser
    , titleOutput
    , taskOutput
    , descriptionOutput
    , dueOutput
    , subtaskOutput
    , localTimes
    ) where

import ClassyPrelude

import Data.Ini.Config

import Taskell.IO.Config.Parser (noEmpty, parseText)

data Config = Config
    { Config -> Text
titleOutput       :: Text
    , Config -> Text
taskOutput        :: Text
    , Config -> Text
descriptionOutput :: Text
    , Config -> Text
dueOutput         :: Text
    , Config -> Text
subtaskOutput     :: Text
    , Config -> Bool
localTimes        :: Bool
    }

defaultConfig :: Config
defaultConfig :: Config
defaultConfig =
    Config :: Text -> Text -> Text -> Text -> Text -> Bool -> Config
Config
    { titleOutput :: Text
titleOutput = Text
"##"
    , taskOutput :: Text
taskOutput = Text
"-"
    , descriptionOutput :: Text
descriptionOutput = Text
"    >"
    , dueOutput :: Text
dueOutput = Text
"    @"
    , subtaskOutput :: Text
subtaskOutput = Text
"    *"
    , localTimes :: Bool
localTimes = Bool
False
    }

titleOutputP :: SectionParser Text
titleOutputP :: SectionParser Text
titleOutputP = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe (Config -> Text
titleOutput Config
defaultConfig) (Maybe Text -> Text)
-> (Maybe Text -> Maybe Text) -> Maybe Text -> Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Text -> Maybe Text
noEmpty (Text -> Maybe Text) -> (Text -> Text) -> Text -> Maybe Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
parseText (Text -> Maybe Text) -> Maybe Text -> Maybe Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (Maybe Text -> Text)
-> SectionParser (Maybe Text) -> SectionParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> SectionParser (Maybe Text)
fieldMb Text
"title"

taskOutputP :: SectionParser Text
taskOutputP :: SectionParser Text
taskOutputP = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe (Config -> Text
taskOutput Config
defaultConfig) (Maybe Text -> Text)
-> (Maybe Text -> Maybe Text) -> Maybe Text -> Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Text -> Maybe Text
noEmpty (Text -> Maybe Text) -> (Text -> Text) -> Text -> Maybe Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
parseText (Text -> Maybe Text) -> Maybe Text -> Maybe Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (Maybe Text -> Text)
-> SectionParser (Maybe Text) -> SectionParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> SectionParser (Maybe Text)
fieldMb Text
"task"

descriptionOutputP :: SectionParser Text
descriptionOutputP :: SectionParser Text
descriptionOutputP =
    Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe (Config -> Text
descriptionOutput Config
defaultConfig) (Maybe Text -> Text)
-> (Maybe Text -> Maybe Text) -> Maybe Text -> Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Text -> Maybe Text
noEmpty (Text -> Maybe Text) -> (Text -> Text) -> Text -> Maybe Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
parseText (Text -> Maybe Text) -> Maybe Text -> Maybe Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (Maybe Text -> Text)
-> SectionParser (Maybe Text) -> SectionParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> SectionParser (Maybe Text)
fieldMb Text
"summary"

dueOutputP :: SectionParser Text
dueOutputP :: SectionParser Text
dueOutputP = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe (Config -> Text
dueOutput Config
defaultConfig) (Maybe Text -> Text)
-> (Maybe Text -> Maybe Text) -> Maybe Text -> Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Text -> Maybe Text
noEmpty (Text -> Maybe Text) -> (Text -> Text) -> Text -> Maybe Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
parseText (Text -> Maybe Text) -> Maybe Text -> Maybe Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (Maybe Text -> Text)
-> SectionParser (Maybe Text) -> SectionParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> SectionParser (Maybe Text)
fieldMb Text
"due"

subtaskOutputP :: SectionParser Text
subtaskOutputP :: SectionParser Text
subtaskOutputP =
    Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe (Config -> Text
subtaskOutput Config
defaultConfig) (Maybe Text -> Text)
-> (Maybe Text -> Maybe Text) -> Maybe Text -> Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Text -> Maybe Text
noEmpty (Text -> Maybe Text) -> (Text -> Text) -> Text -> Maybe Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
parseText (Text -> Maybe Text) -> Maybe Text -> Maybe Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (Maybe Text -> Text)
-> SectionParser (Maybe Text) -> SectionParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> SectionParser (Maybe Text)
fieldMb Text
"subtask"

localTimesP :: SectionParser Bool
localTimesP :: SectionParser Bool
localTimesP = Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"localTimes" (Config -> Bool
localTimes Config
defaultConfig)

parser :: IniParser Config
parser :: IniParser Config
parser =
    Config -> Maybe Config -> Config
forall a. a -> Maybe a -> a
fromMaybe Config
defaultConfig (Maybe Config -> Config)
-> IniParser (Maybe Config) -> IniParser Config
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
    Text -> SectionParser Config -> IniParser (Maybe Config)
forall a. Text -> SectionParser a -> IniParser (Maybe a)
sectionMb
        Text
"markdown"
        (Text -> Text -> Text -> Text -> Text -> Bool -> Config
Config (Text -> Text -> Text -> Text -> Text -> Bool -> Config)
-> SectionParser Text
-> SectionParser (Text -> Text -> Text -> Text -> Bool -> Config)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SectionParser Text
titleOutputP SectionParser (Text -> Text -> Text -> Text -> Bool -> Config)
-> SectionParser Text
-> SectionParser (Text -> Text -> Text -> Bool -> Config)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SectionParser Text
taskOutputP SectionParser (Text -> Text -> Text -> Bool -> Config)
-> SectionParser Text
-> SectionParser (Text -> Text -> Bool -> Config)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SectionParser Text
descriptionOutputP SectionParser (Text -> Text -> Bool -> Config)
-> SectionParser Text -> SectionParser (Text -> Bool -> Config)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SectionParser Text
dueOutputP SectionParser (Text -> Bool -> Config)
-> SectionParser Text -> SectionParser (Bool -> Config)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*>
         SectionParser Text
subtaskOutputP SectionParser (Bool -> Config)
-> SectionParser Bool -> SectionParser Config
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*>
         SectionParser Bool
localTimesP)