module Matterhorn.Events.EditNotifyPrefs
    ( onEventEditNotifyPrefs
    , editNotifyPrefsKeybindings
    , editNotifyPrefsKeyHandlers
    , handleEditNotifyPrefsEvent
    )
where

import           Prelude ()
import           Matterhorn.Prelude

import           Brick
import           Brick.Keybindings
import           Brick.Forms (handleFormEvent, formState)
import           Data.Maybe (fromJust)
import qualified Graphics.Vty as V
import qualified Network.Mattermost.Endpoints as MM
import           Network.Mattermost.Types ( TeamId )

import           Lens.Micro.Platform (_Just, singular)

import           Matterhorn.Types
import           Matterhorn.State.NotifyPrefs
import           Matterhorn.State.Async

onEventEditNotifyPrefs :: TeamId -> V.Event -> MH Bool
onEventEditNotifyPrefs :: TeamId -> Event -> MH Bool
onEventEditNotifyPrefs TeamId
tId =
    [Event -> MH Bool] -> Event -> MH Bool
handleEventWith [ (KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH)
-> Event -> MH Bool
mhHandleKeyboardEvent (TeamId -> KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH
editNotifyPrefsKeybindings TeamId
tId)
                    , TeamId -> BrickEvent Name MHEvent -> MH Bool
handleEditNotifyPrefsEvent TeamId
tId forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n e. Event -> BrickEvent n e
VtyEvent
                    ]

handleEditNotifyPrefsEvent :: TeamId -> BrickEvent Name MHEvent -> MH Bool
handleEditNotifyPrefsEvent :: TeamId -> BrickEvent Name MHEvent -> MH Bool
handleEditNotifyPrefsEvent TeamId
tId BrickEvent Name MHEvent
e = do
    forall b e.
Lens' ChatState b -> (e -> EventM Name b ()) -> e -> MH ()
mhZoom (TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)forall b c a. (b -> c) -> (a -> b) -> a -> c
.Lens' TeamState (Maybe (Form ChannelNotifyProps MHEvent Name))
tsNotifyPrefsforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s t a. HasCallStack => Traversal s t a a -> Lens s t a a
singular forall a a'. Traversal (Maybe a) (Maybe a') a a'
_Just) forall n e s. Eq n => BrickEvent n e -> EventM n (Form s e n) ()
handleFormEvent BrickEvent Name MHEvent
e
    forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True

editNotifyPrefsKeybindings :: TeamId -> KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH
editNotifyPrefsKeybindings :: TeamId -> KeyConfig KeyEvent -> KeyDispatcher KeyEvent MH
editNotifyPrefsKeybindings TeamId
tId KeyConfig KeyEvent
kc = forall k (m :: * -> *).
Ord k =>
KeyConfig k -> [KeyEventHandler k m] -> KeyDispatcher k m
unsafeKeyDispatcher KeyConfig KeyEvent
kc (TeamId -> [MHKeyEventHandler]
editNotifyPrefsKeyHandlers TeamId
tId)

editNotifyPrefsKeyHandlers :: TeamId -> [MHKeyEventHandler]
editNotifyPrefsKeyHandlers :: TeamId -> [MHKeyEventHandler]
editNotifyPrefsKeyHandlers TeamId
tId =
    [ forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
CancelEvent Text
"Close channel notification preferences" forall a b. (a -> b) -> a -> b
$
        TeamId -> MH ()
exitEditNotifyPrefsMode TeamId
tId
    , forall k (m :: * -> *). k -> Text -> m () -> KeyEventHandler k m
onEvent KeyEvent
FormSubmitEvent Text
"Save channel notification preferences" forall a b. (a -> b) -> a -> b
$ do
        ChatState
st <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use forall a. a -> a
id
        TeamId -> (ChannelId -> ClientChannel -> MH ()) -> MH ()
withCurrentChannel TeamId
tId forall a b. (a -> b) -> a -> b
$ \ChannelId
cId ClientChannel
_ -> do
            let form :: Form ChannelNotifyProps MHEvent Name
form = forall a. HasCallStack => Maybe a -> a
fromJust forall a b. (a -> b) -> a -> b
$ ChatState
stforall s a. s -> Getting a s a -> a
^.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 a. DoAsyncChannelMM a
doAsyncChannelMM AsyncPriority
Preempt ChannelId
cId
              (\Session
s ChannelId
_ -> ChannelId -> UserId -> ChannelNotifyProps -> Session -> IO ()
MM.mmUpdateChannelNotifications ChannelId
cId (ChatState -> UserId
myUserId ChatState
st) (forall s e n. Form s e n -> s
formState Form ChannelNotifyProps MHEvent Name
form) Session
s)
              (\ChannelId
_ ()
_ -> forall a. Maybe a
Nothing)
            TeamId -> MH ()
exitEditNotifyPrefsMode TeamId
tId
    ]