{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Matterhorn.State.NotifyPrefs
    ( enterEditNotifyPrefsMode
    , exitEditNotifyPrefsMode
    )
where

import Prelude ()
import Matterhorn.Prelude

import Network.Mattermost.Types ( ChannelNotifyProps
                                , TeamId
                                , User(..)
                                , UserNotifyProps(..)
                                , Type(..)
                                , channelNotifyPropsMarkUnread
                                , channelNotifyPropsIgnoreChannelMentions
                                , WithDefault(..)
                                , NotifyOption(..)
                                )

import Brick
import Brick.Forms
import Lens.Micro.Platform ( Lens', (.=), lens )

import Matterhorn.Types


muteLens :: Lens' ChannelNotifyProps Bool
muteLens :: Lens' ChannelNotifyProps Bool
muteLens = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens (\ChannelNotifyProps
props -> ChannelNotifyProps
propsforall s a. s -> Getting a s a -> a
^.Lens' ChannelNotifyProps (WithDefault NotifyOption)
channelNotifyPropsMarkUnreadL forall a. Eq a => a -> a -> Bool
== forall a. a -> WithDefault a
IsValue NotifyOption
NotifyOptionMention)
           (\ChannelNotifyProps
props Bool
muted -> ChannelNotifyProps
props { channelNotifyPropsMarkUnread :: WithDefault NotifyOption
channelNotifyPropsMarkUnread =
                                          if Bool
muted
                                          then forall a. a -> WithDefault a
IsValue NotifyOption
NotifyOptionMention
                                          else forall a. a -> WithDefault a
IsValue NotifyOption
NotifyOptionAll
                                  })

channelMentionLens :: Lens' ChannelNotifyProps Bool
channelMentionLens :: Lens' ChannelNotifyProps Bool
channelMentionLens = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens (\ChannelNotifyProps
props -> ChannelNotifyProps
propsforall s a. s -> Getting a s a -> a
^.Lens' ChannelNotifyProps (WithDefault Bool)
channelNotifyPropsIgnoreChannelMentionsL forall a. Eq a => a -> a -> Bool
== forall a. a -> WithDefault a
IsValue Bool
True)
                     (\ChannelNotifyProps
props Bool
ignoreChannelMentions ->
                          ChannelNotifyProps
props { channelNotifyPropsIgnoreChannelMentions :: WithDefault Bool
channelNotifyPropsIgnoreChannelMentions = if Bool
ignoreChannelMentions
                                                                            then forall a. a -> WithDefault a
IsValue Bool
True
                                                                            else forall a. WithDefault a
Default
                                })

notifyOptionName :: NotifyOption -> Text
notifyOptionName :: NotifyOption -> Text
notifyOptionName NotifyOption
NotifyOptionAll = Text
"All activity"
notifyOptionName NotifyOption
NotifyOptionMention = Text
"Mentions"
notifyOptionName NotifyOption
NotifyOptionNone = Text
"Never"

mkNotifyButtons :: ((WithDefault NotifyOption) -> Name)
                -> Lens' ChannelNotifyProps (WithDefault NotifyOption)
                -> NotifyOption
                -> ChannelNotifyProps
                -> FormFieldState ChannelNotifyProps e Name
mkNotifyButtons :: forall e.
(WithDefault NotifyOption -> Name)
-> Lens' ChannelNotifyProps (WithDefault NotifyOption)
-> NotifyOption
-> ChannelNotifyProps
-> FormFieldState ChannelNotifyProps e Name
mkNotifyButtons WithDefault NotifyOption -> Name
mkName Lens' ChannelNotifyProps (WithDefault NotifyOption)
l NotifyOption
globalDefault =
    let optTuple :: NotifyOption -> (WithDefault NotifyOption, Name, Text)
optTuple NotifyOption
opt = (forall a. a -> WithDefault a
IsValue NotifyOption
opt, WithDefault NotifyOption -> Name
mkName forall a b. (a -> b) -> a -> b
$ forall a. a -> WithDefault a
IsValue NotifyOption
opt, NotifyOption -> Text
notifyOptionName NotifyOption
opt)
        defaultField :: (WithDefault a, Name, Text)
defaultField = (forall a. WithDefault a
Default, WithDefault NotifyOption -> Name
mkName forall a. WithDefault a
Default, Text
"Global default (" forall a. Semigroup a => a -> a -> a
<> NotifyOption -> Text
notifyOptionName NotifyOption
globalDefault forall a. Semigroup a => a -> a -> a
<> Text
")")
        nonDefault :: [(WithDefault NotifyOption, Name, Text)]
nonDefault = NotifyOption -> (WithDefault NotifyOption, Name, Text)
optTuple forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ NotifyOption
NotifyOptionAll
                                  , NotifyOption
NotifyOptionMention
                                  , NotifyOption
NotifyOptionNone
                                  ]
    in forall n a s e.
(Ord n, Show n, Eq a) =>
Lens' s a -> [(a, n, Text)] -> s -> FormFieldState s e n
radioField Lens' ChannelNotifyProps (WithDefault NotifyOption)
l (forall {a}. (WithDefault a, Name, Text)
defaultField forall a. a -> [a] -> [a]
: [(WithDefault NotifyOption, Name, Text)]
nonDefault)

notifyPrefsForm :: TeamId -> UserNotifyProps -> ChannelNotifyProps -> Form ChannelNotifyProps e Name
notifyPrefsForm :: forall e.
TeamId
-> UserNotifyProps
-> ChannelNotifyProps
-> Form ChannelNotifyProps e Name
notifyPrefsForm TeamId
tId UserNotifyProps
globalDefaults =
    forall s e n. [s -> FormFieldState s e n] -> s -> Form s e n
newForm [ forall n s e.
(Ord n, Show n) =>
Lens' s Bool -> n -> Text -> s -> FormFieldState s e n
checkboxField Lens' ChannelNotifyProps Bool
muteLens (TeamId -> Name
MuteToggleField TeamId
tId) Text
"Mute channel"
            , (forall n. Padding -> Widget n -> Widget n
padTop forall a b. (a -> b) -> a -> b
$ Int -> Padding
Pad Int
1) forall n s e.
(Widget n -> Widget n)
-> (s -> FormFieldState s e n) -> s -> FormFieldState s e n
@@= forall n s e.
(Ord n, Show n) =>
Lens' s Bool -> n -> Text -> s -> FormFieldState s e n
checkboxField Lens' ChannelNotifyProps Bool
channelMentionLens (TeamId -> Name
ChannelMentionsField TeamId
tId) Text
"Ignore channel mentions"
            , forall {n}. String -> Widget n -> Widget n
radioStyle String
"Desktop notifications" forall n s e.
(Widget n -> Widget n)
-> (s -> FormFieldState s e n) -> s -> FormFieldState s e n
@@=
                forall e.
(WithDefault NotifyOption -> Name)
-> Lens' ChannelNotifyProps (WithDefault NotifyOption)
-> NotifyOption
-> ChannelNotifyProps
-> FormFieldState ChannelNotifyProps e Name
mkNotifyButtons (TeamId -> WithDefault NotifyOption -> Name
DesktopNotificationsField TeamId
tId) Lens' ChannelNotifyProps (WithDefault NotifyOption)
channelNotifyPropsDesktopL (UserNotifyProps -> NotifyOption
userNotifyPropsDesktop UserNotifyProps
globalDefaults)
            , forall {n}. String -> Widget n -> Widget n
radioStyle String
"Push notifications" forall n s e.
(Widget n -> Widget n)
-> (s -> FormFieldState s e n) -> s -> FormFieldState s e n
@@=
                forall e.
(WithDefault NotifyOption -> Name)
-> Lens' ChannelNotifyProps (WithDefault NotifyOption)
-> NotifyOption
-> ChannelNotifyProps
-> FormFieldState ChannelNotifyProps e Name
mkNotifyButtons (TeamId -> WithDefault NotifyOption -> Name
PushNotificationsField TeamId
tId) Lens' ChannelNotifyProps (WithDefault NotifyOption)
channelNotifyPropsPushL (UserNotifyProps -> NotifyOption
userNotifyPropsPush UserNotifyProps
globalDefaults)
            ]
    where radioStyle :: String -> Widget n -> Widget n
radioStyle String
label = (forall n. Padding -> Widget n -> Widget n
padTop forall a b. (a -> b) -> a -> b
$ Int -> Padding
Pad Int
1 ) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall n. String -> Widget n
str String
label forall n. Widget n -> Widget n -> Widget n
<=>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall n. Padding -> Widget n -> Widget n
padLeft forall a b. (a -> b) -> a -> b
$ Int -> Padding
Pad Int
1)

enterEditNotifyPrefsMode :: TeamId -> MH ()
enterEditNotifyPrefsMode :: TeamId -> MH ()
enterEditNotifyPrefsMode TeamId
tId =
    TeamId -> (ChannelId -> ClientChannel -> MH ()) -> MH ()
withCurrentChannel TeamId
tId forall a b. (a -> b) -> a -> b
$ \ChannelId
_ ClientChannel
chan -> do
        case ClientChannel
chanforall s a. s -> Getting a s a -> a
^.Lens' ClientChannel ChannelInfo
ccInfoforall b c a. (b -> c) -> (a -> b) -> a -> c
.Lens' ChannelInfo Type
cdType of
          Type
Direct -> MHError -> MH ()
mhError forall a b. (a -> b) -> a -> b
$ Text -> MHError
GenericError Text
"Cannot open notification preferences for DM channel."
          Type
_ -> do
            let props :: ChannelNotifyProps
props = ClientChannel
chanforall s a. s -> Getting a s a -> a
^.Lens' ClientChannel ChannelInfo
ccInfoforall b c a. (b -> c) -> (a -> b) -> a -> c
.Lens' ChannelInfo ChannelNotifyProps
cdNotifyProps
            User
user <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Lens' ChatState User
csMe
            TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)forall b c a. (b -> c) -> (a -> b) -> a -> c
.Lens' TeamState (Maybe (Form ChannelNotifyProps MHEvent Name))
tsNotifyPrefs forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= (forall a. a -> Maybe a
Just (forall e.
TeamId
-> UserNotifyProps
-> ChannelNotifyProps
-> Form ChannelNotifyProps e Name
notifyPrefsForm TeamId
tId (User -> UserNotifyProps
userNotifyProps User
user) ChannelNotifyProps
props))
            TeamId -> Mode -> MH ()
pushMode TeamId
tId Mode
EditNotifyPrefs

exitEditNotifyPrefsMode :: TeamId -> MH ()
exitEditNotifyPrefsMode :: TeamId -> MH ()
exitEditNotifyPrefsMode = TeamId -> MH ()
popMode