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

import           Prelude ()
import           Matterhorn.Prelude

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

import           Lens.Micro.Platform (_Just, (.=), singular)

import           Matterhorn.Types
import           Matterhorn.Types.KeyEvents
import           Matterhorn.Events.Keybindings
import           Matterhorn.State.NotifyPrefs
import           Matterhorn.State.Async

onEventEditNotifyPrefs :: V.Event -> MH Bool
onEventEditNotifyPrefs :: Event -> MH Bool
onEventEditNotifyPrefs =
    let fallback :: Event -> MH ()
fallback Event
e = do
            Form ChannelNotifyProps MHEvent Name
form <- Getting
  (Form ChannelNotifyProps MHEvent Name)
  ChatState
  (Form ChannelNotifyProps MHEvent Name)
-> MH (Form ChannelNotifyProps MHEvent Name)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use ((TeamState
 -> Const (Form ChannelNotifyProps MHEvent Name) TeamState)
-> ChatState
-> Const (Form ChannelNotifyProps MHEvent Name) ChatState
Lens' ChatState TeamState
csCurrentTeam((TeamState
  -> Const (Form ChannelNotifyProps MHEvent Name) TeamState)
 -> ChatState
 -> Const (Form ChannelNotifyProps MHEvent Name) ChatState)
-> ((Form ChannelNotifyProps MHEvent Name
     -> Const
          (Form ChannelNotifyProps MHEvent Name)
          (Form ChannelNotifyProps MHEvent Name))
    -> TeamState
    -> Const (Form ChannelNotifyProps MHEvent Name) TeamState)
-> Getting
     (Form ChannelNotifyProps MHEvent Name)
     ChatState
     (Form ChannelNotifyProps MHEvent Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe (Form ChannelNotifyProps MHEvent Name)
 -> Const
      (Form ChannelNotifyProps MHEvent Name)
      (Maybe (Form ChannelNotifyProps MHEvent Name)))
-> TeamState
-> Const (Form ChannelNotifyProps MHEvent Name) TeamState
Lens' TeamState (Maybe (Form ChannelNotifyProps MHEvent Name))
tsNotifyPrefs((Maybe (Form ChannelNotifyProps MHEvent Name)
  -> Const
       (Form ChannelNotifyProps MHEvent Name)
       (Maybe (Form ChannelNotifyProps MHEvent Name)))
 -> TeamState
 -> Const (Form ChannelNotifyProps MHEvent Name) TeamState)
-> ((Form ChannelNotifyProps MHEvent Name
     -> Const
          (Form ChannelNotifyProps MHEvent Name)
          (Form ChannelNotifyProps MHEvent Name))
    -> Maybe (Form ChannelNotifyProps MHEvent Name)
    -> Const
         (Form ChannelNotifyProps MHEvent Name)
         (Maybe (Form ChannelNotifyProps MHEvent Name)))
-> (Form ChannelNotifyProps MHEvent Name
    -> Const
         (Form ChannelNotifyProps MHEvent Name)
         (Form ChannelNotifyProps MHEvent Name))
-> TeamState
-> Const (Form ChannelNotifyProps MHEvent Name) TeamState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Traversal
  (Maybe (Form ChannelNotifyProps MHEvent Name))
  (Maybe (Form ChannelNotifyProps MHEvent Name))
  (Form ChannelNotifyProps MHEvent Name)
  (Form ChannelNotifyProps MHEvent Name)
-> Lens
     (Maybe (Form ChannelNotifyProps MHEvent Name))
     (Maybe (Form ChannelNotifyProps MHEvent Name))
     (Form ChannelNotifyProps MHEvent Name)
     (Form ChannelNotifyProps MHEvent Name)
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'
Traversal
  (Maybe (Form ChannelNotifyProps MHEvent Name))
  (Maybe (Form ChannelNotifyProps MHEvent Name))
  (Form ChannelNotifyProps MHEvent Name)
  (Form ChannelNotifyProps MHEvent Name)
_Just)
            Form ChannelNotifyProps MHEvent Name
updatedForm <- EventM Name (Form ChannelNotifyProps MHEvent Name)
-> MH (Form ChannelNotifyProps MHEvent Name)
forall a. EventM Name a -> MH a
mh (EventM Name (Form ChannelNotifyProps MHEvent Name)
 -> MH (Form ChannelNotifyProps MHEvent Name))
-> EventM Name (Form ChannelNotifyProps MHEvent Name)
-> MH (Form ChannelNotifyProps MHEvent Name)
forall a b. (a -> b) -> a -> b
$ BrickEvent Name MHEvent
-> Form ChannelNotifyProps MHEvent Name
-> EventM Name (Form ChannelNotifyProps MHEvent Name)
forall n e s.
Eq n =>
BrickEvent n e -> Form s e n -> EventM n (Form s e n)
handleFormEvent (Event -> BrickEvent Name MHEvent
forall n e. Event -> BrickEvent n e
VtyEvent Event
e) Form ChannelNotifyProps MHEvent Name
form
            (TeamState -> Identity TeamState)
-> ChatState -> Identity ChatState
Lens' ChatState TeamState
csCurrentTeam((TeamState -> Identity TeamState)
 -> ChatState -> Identity ChatState)
-> ((Maybe (Form ChannelNotifyProps MHEvent Name)
     -> Identity (Maybe (Form ChannelNotifyProps MHEvent Name)))
    -> TeamState -> Identity TeamState)
-> (Maybe (Form ChannelNotifyProps MHEvent Name)
    -> Identity (Maybe (Form ChannelNotifyProps MHEvent Name)))
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe (Form ChannelNotifyProps MHEvent Name)
 -> Identity (Maybe (Form ChannelNotifyProps MHEvent Name)))
-> TeamState -> Identity TeamState
Lens' TeamState (Maybe (Form ChannelNotifyProps MHEvent Name))
tsNotifyPrefs ((Maybe (Form ChannelNotifyProps MHEvent Name)
  -> Identity (Maybe (Form ChannelNotifyProps MHEvent Name)))
 -> ChatState -> Identity ChatState)
-> Maybe (Form ChannelNotifyProps MHEvent Name) -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= Form ChannelNotifyProps MHEvent Name
-> Maybe (Form ChannelNotifyProps MHEvent Name)
forall a. a -> Maybe a
Just Form ChannelNotifyProps MHEvent Name
updatedForm
    in (KeyConfig -> KeyHandlerMap)
-> (Event -> MH ()) -> Event -> MH Bool
handleKeyboardEvent KeyConfig -> KeyHandlerMap
editNotifyPrefsKeybindings Event -> MH ()
fallback

editNotifyPrefsKeybindings :: KeyConfig -> KeyHandlerMap
editNotifyPrefsKeybindings :: KeyConfig -> KeyHandlerMap
editNotifyPrefsKeybindings = [KeyEventHandler] -> KeyConfig -> KeyHandlerMap
mkKeybindings [KeyEventHandler]
editNotifyPrefsKeyHandlers

editNotifyPrefsKeyHandlers :: [KeyEventHandler]
editNotifyPrefsKeyHandlers :: [KeyEventHandler]
editNotifyPrefsKeyHandlers =
    [ KeyEvent -> Text -> MH () -> KeyEventHandler
mkKb KeyEvent
CancelEvent Text
"Close channel notification preferences" MH ()
exitEditNotifyPrefsMode
    , KeyEvent -> Text -> MH () -> KeyEventHandler
mkKb KeyEvent
FormSubmitEvent Text
"Save channel notification preferences" (MH () -> KeyEventHandler) -> MH () -> KeyEventHandler
forall a b. (a -> b) -> a -> b
$ do
        ChatState
st <- Getting ChatState ChatState ChatState -> MH ChatState
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting ChatState ChatState ChatState
forall a. a -> a
id
        let form :: Form ChannelNotifyProps MHEvent Name
form = Maybe (Form ChannelNotifyProps MHEvent Name)
-> Form ChannelNotifyProps MHEvent Name
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe (Form ChannelNotifyProps MHEvent Name)
 -> Form ChannelNotifyProps MHEvent Name)
-> Maybe (Form ChannelNotifyProps MHEvent Name)
-> Form ChannelNotifyProps MHEvent Name
forall a b. (a -> b) -> a -> b
$ ChatState
stChatState
-> Getting
     (Maybe (Form ChannelNotifyProps MHEvent Name))
     ChatState
     (Maybe (Form ChannelNotifyProps MHEvent Name))
-> Maybe (Form ChannelNotifyProps MHEvent Name)
forall s a. s -> Getting a s a -> a
^.(TeamState
 -> Const (Maybe (Form ChannelNotifyProps MHEvent Name)) TeamState)
-> ChatState
-> Const (Maybe (Form ChannelNotifyProps MHEvent Name)) ChatState
Lens' ChatState TeamState
csCurrentTeam((TeamState
  -> Const (Maybe (Form ChannelNotifyProps MHEvent Name)) TeamState)
 -> ChatState
 -> Const (Maybe (Form ChannelNotifyProps MHEvent Name)) ChatState)
-> ((Maybe (Form ChannelNotifyProps MHEvent Name)
     -> Const
          (Maybe (Form ChannelNotifyProps MHEvent Name))
          (Maybe (Form ChannelNotifyProps MHEvent Name)))
    -> TeamState
    -> Const (Maybe (Form ChannelNotifyProps MHEvent Name)) TeamState)
-> Getting
     (Maybe (Form ChannelNotifyProps MHEvent Name))
     ChatState
     (Maybe (Form ChannelNotifyProps MHEvent Name))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe (Form ChannelNotifyProps MHEvent Name)
 -> Const
      (Maybe (Form ChannelNotifyProps MHEvent Name))
      (Maybe (Form ChannelNotifyProps MHEvent Name)))
-> TeamState
-> Const (Maybe (Form ChannelNotifyProps MHEvent Name)) TeamState
Lens' TeamState (Maybe (Form ChannelNotifyProps MHEvent Name))
tsNotifyPrefs
            cId :: ChannelId
cId = ChatState
stChatState -> Getting ChannelId ChatState ChannelId -> ChannelId
forall s a. s -> Getting a s a -> a
^.TeamId -> SimpleGetter ChatState ChannelId
csCurrentChannelId(ChatState
stChatState -> Getting TeamId ChatState TeamId -> TeamId
forall s a. s -> Getting a s a -> a
^.Getting TeamId ChatState TeamId
SimpleGetter ChatState TeamId
csCurrentTeamId)

        DoAsyncChannelMM ()
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) (Form ChannelNotifyProps MHEvent Name -> ChannelNotifyProps
forall s e n. Form s e n -> s
formState Form ChannelNotifyProps MHEvent Name
form) Session
s)
          (\ChannelId
_ ()
_ -> Maybe (MH ())
forall a. Maybe a
Nothing)
        MH ()
exitEditNotifyPrefsMode
    ]