module Matterhorn.Events.ChannelTopicWindow
  ( onEventChannelTopicWindow
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import           Brick.Focus
import           Brick.Widgets.Edit ( handleEditorEvent, getEditContents )
import qualified Data.Text as T
import           Lens.Micro.Platform ( (%=) )
import qualified Graphics.Vty as Vty

import           Matterhorn.Types
import           Matterhorn.State.Channels ( setChannelTopic )


onEventChannelTopicWindow :: Vty.Event -> MH ()
onEventChannelTopicWindow :: Event -> MH ()
onEventChannelTopicWindow (Vty.EvKey (Vty.KChar Char
'\t') []) =
    (TeamState -> Identity TeamState)
-> ChatState -> Identity ChatState
Lens' ChatState TeamState
csCurrentTeam((TeamState -> Identity TeamState)
 -> ChatState -> Identity ChatState)
-> ((FocusRing Name -> Identity (FocusRing Name))
    -> TeamState -> Identity TeamState)
-> (FocusRing Name -> Identity (FocusRing Name))
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelTopicDialogState -> Identity ChannelTopicDialogState)
-> TeamState -> Identity TeamState
Lens' TeamState ChannelTopicDialogState
tsChannelTopicDialog((ChannelTopicDialogState -> Identity ChannelTopicDialogState)
 -> TeamState -> Identity TeamState)
-> ((FocusRing Name -> Identity (FocusRing Name))
    -> ChannelTopicDialogState -> Identity ChannelTopicDialogState)
-> (FocusRing Name -> Identity (FocusRing Name))
-> TeamState
-> Identity TeamState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(FocusRing Name -> Identity (FocusRing Name))
-> ChannelTopicDialogState -> Identity ChannelTopicDialogState
Lens' ChannelTopicDialogState (FocusRing Name)
channelTopicDialogFocus ((FocusRing Name -> Identity (FocusRing Name))
 -> ChatState -> Identity ChatState)
-> (FocusRing Name -> FocusRing Name) -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= FocusRing Name -> FocusRing Name
forall n. FocusRing n -> FocusRing n
focusNext
onEventChannelTopicWindow (Vty.EvKey Key
Vty.KBackTab []) =
    (TeamState -> Identity TeamState)
-> ChatState -> Identity ChatState
Lens' ChatState TeamState
csCurrentTeam((TeamState -> Identity TeamState)
 -> ChatState -> Identity ChatState)
-> ((FocusRing Name -> Identity (FocusRing Name))
    -> TeamState -> Identity TeamState)
-> (FocusRing Name -> Identity (FocusRing Name))
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelTopicDialogState -> Identity ChannelTopicDialogState)
-> TeamState -> Identity TeamState
Lens' TeamState ChannelTopicDialogState
tsChannelTopicDialog((ChannelTopicDialogState -> Identity ChannelTopicDialogState)
 -> TeamState -> Identity TeamState)
-> ((FocusRing Name -> Identity (FocusRing Name))
    -> ChannelTopicDialogState -> Identity ChannelTopicDialogState)
-> (FocusRing Name -> Identity (FocusRing Name))
-> TeamState
-> Identity TeamState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(FocusRing Name -> Identity (FocusRing Name))
-> ChannelTopicDialogState -> Identity ChannelTopicDialogState
Lens' ChannelTopicDialogState (FocusRing Name)
channelTopicDialogFocus ((FocusRing Name -> Identity (FocusRing Name))
 -> ChatState -> Identity ChatState)
-> (FocusRing Name -> FocusRing Name) -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= FocusRing Name -> FocusRing Name
forall n. FocusRing n -> FocusRing n
focusPrev
onEventChannelTopicWindow e :: Event
e@(Vty.EvKey Key
Vty.KEnter []) = do
    FocusRing Name
f <- Getting (FocusRing Name) ChatState (FocusRing Name)
-> MH (FocusRing Name)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use ((TeamState -> Const (FocusRing Name) TeamState)
-> ChatState -> Const (FocusRing Name) ChatState
Lens' ChatState TeamState
csCurrentTeam((TeamState -> Const (FocusRing Name) TeamState)
 -> ChatState -> Const (FocusRing Name) ChatState)
-> ((FocusRing Name -> Const (FocusRing Name) (FocusRing Name))
    -> TeamState -> Const (FocusRing Name) TeamState)
-> Getting (FocusRing Name) ChatState (FocusRing Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelTopicDialogState
 -> Const (FocusRing Name) ChannelTopicDialogState)
-> TeamState -> Const (FocusRing Name) TeamState
Lens' TeamState ChannelTopicDialogState
tsChannelTopicDialog((ChannelTopicDialogState
  -> Const (FocusRing Name) ChannelTopicDialogState)
 -> TeamState -> Const (FocusRing Name) TeamState)
-> ((FocusRing Name -> Const (FocusRing Name) (FocusRing Name))
    -> ChannelTopicDialogState
    -> Const (FocusRing Name) ChannelTopicDialogState)
-> (FocusRing Name -> Const (FocusRing Name) (FocusRing Name))
-> TeamState
-> Const (FocusRing Name) TeamState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(FocusRing Name -> Const (FocusRing Name) (FocusRing Name))
-> ChannelTopicDialogState
-> Const (FocusRing Name) ChannelTopicDialogState
Lens' ChannelTopicDialogState (FocusRing Name)
channelTopicDialogFocus)
    case FocusRing Name -> Maybe Name
forall n. FocusRing n -> Maybe n
focusGetCurrent FocusRing Name
f of
        Just (ChannelTopicSaveButton {}) -> do
            Editor Text Name
ed <- Getting (Editor Text Name) ChatState (Editor Text Name)
-> MH (Editor Text Name)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use ((TeamState -> Const (Editor Text Name) TeamState)
-> ChatState -> Const (Editor Text Name) ChatState
Lens' ChatState TeamState
csCurrentTeam((TeamState -> Const (Editor Text Name) TeamState)
 -> ChatState -> Const (Editor Text Name) ChatState)
-> ((Editor Text Name
     -> Const (Editor Text Name) (Editor Text Name))
    -> TeamState -> Const (Editor Text Name) TeamState)
-> Getting (Editor Text Name) ChatState (Editor Text Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelTopicDialogState
 -> Const (Editor Text Name) ChannelTopicDialogState)
-> TeamState -> Const (Editor Text Name) TeamState
Lens' TeamState ChannelTopicDialogState
tsChannelTopicDialog((ChannelTopicDialogState
  -> Const (Editor Text Name) ChannelTopicDialogState)
 -> TeamState -> Const (Editor Text Name) TeamState)
-> ((Editor Text Name
     -> Const (Editor Text Name) (Editor Text Name))
    -> ChannelTopicDialogState
    -> Const (Editor Text Name) ChannelTopicDialogState)
-> (Editor Text Name
    -> Const (Editor Text Name) (Editor Text Name))
-> TeamState
-> Const (Editor Text Name) TeamState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Editor Text Name -> Const (Editor Text Name) (Editor Text Name))
-> ChannelTopicDialogState
-> Const (Editor Text Name) ChannelTopicDialogState
Lens' ChannelTopicDialogState (Editor Text Name)
channelTopicDialogEditor)
            let topic :: Text
topic = [Text] -> Text
T.unlines ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ Editor Text Name -> [Text]
forall t n. Monoid t => Editor t n -> [t]
getEditContents Editor Text Name
ed
            Text -> MH ()
setChannelTopic Text
topic
            Mode -> MH ()
setMode Mode
Main
        Just (ChannelTopicEditor {}) ->
            Lens' ChatState (Editor Text Name)
-> (Event -> Editor Text Name -> EventM Name (Editor Text Name))
-> Event
-> MH ()
forall b e.
Lens' ChatState b -> (e -> b -> EventM Name b) -> e -> MH ()
mhHandleEventLensed ((TeamState -> f TeamState) -> ChatState -> f ChatState
Lens' ChatState TeamState
csCurrentTeam((TeamState -> f TeamState) -> ChatState -> f ChatState)
-> ((Editor Text Name -> f (Editor Text Name))
    -> TeamState -> f TeamState)
-> (Editor Text Name -> f (Editor Text Name))
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelTopicDialogState -> f ChannelTopicDialogState)
-> TeamState -> f TeamState
Lens' TeamState ChannelTopicDialogState
tsChannelTopicDialog((ChannelTopicDialogState -> f ChannelTopicDialogState)
 -> TeamState -> f TeamState)
-> ((Editor Text Name -> f (Editor Text Name))
    -> ChannelTopicDialogState -> f ChannelTopicDialogState)
-> (Editor Text Name -> f (Editor Text Name))
-> TeamState
-> f TeamState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Editor Text Name -> f (Editor Text Name))
-> ChannelTopicDialogState -> f ChannelTopicDialogState
Lens' ChannelTopicDialogState (Editor Text Name)
channelTopicDialogEditor)
                                Event -> Editor Text Name -> EventM Name (Editor Text Name)
forall t n.
(DecodeUtf8 t, Eq t, GenericTextZipper t) =>
Event -> Editor t n -> EventM n (Editor t n)
handleEditorEvent Event
e
        Just (ChannelTopicCancelButton {}) ->
            Mode -> MH ()
setMode Mode
Main
        Maybe Name
_ ->
            Mode -> MH ()
setMode Mode
Main
onEventChannelTopicWindow (Vty.EvKey Key
Vty.KEsc []) = do
    Mode -> MH ()
setMode Mode
Main
onEventChannelTopicWindow Event
e = do
    FocusRing Name
f <- Getting (FocusRing Name) ChatState (FocusRing Name)
-> MH (FocusRing Name)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use ((TeamState -> Const (FocusRing Name) TeamState)
-> ChatState -> Const (FocusRing Name) ChatState
Lens' ChatState TeamState
csCurrentTeam((TeamState -> Const (FocusRing Name) TeamState)
 -> ChatState -> Const (FocusRing Name) ChatState)
-> ((FocusRing Name -> Const (FocusRing Name) (FocusRing Name))
    -> TeamState -> Const (FocusRing Name) TeamState)
-> Getting (FocusRing Name) ChatState (FocusRing Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelTopicDialogState
 -> Const (FocusRing Name) ChannelTopicDialogState)
-> TeamState -> Const (FocusRing Name) TeamState
Lens' TeamState ChannelTopicDialogState
tsChannelTopicDialog((ChannelTopicDialogState
  -> Const (FocusRing Name) ChannelTopicDialogState)
 -> TeamState -> Const (FocusRing Name) TeamState)
-> ((FocusRing Name -> Const (FocusRing Name) (FocusRing Name))
    -> ChannelTopicDialogState
    -> Const (FocusRing Name) ChannelTopicDialogState)
-> (FocusRing Name -> Const (FocusRing Name) (FocusRing Name))
-> TeamState
-> Const (FocusRing Name) TeamState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(FocusRing Name -> Const (FocusRing Name) (FocusRing Name))
-> ChannelTopicDialogState
-> Const (FocusRing Name) ChannelTopicDialogState
Lens' ChannelTopicDialogState (FocusRing Name)
channelTopicDialogFocus)
    case FocusRing Name -> Maybe Name
forall n. FocusRing n -> Maybe n
focusGetCurrent FocusRing Name
f of
        Just (ChannelTopicEditor {}) ->
            Lens' ChatState (Editor Text Name)
-> (Event -> Editor Text Name -> EventM Name (Editor Text Name))
-> Event
-> MH ()
forall b e.
Lens' ChatState b -> (e -> b -> EventM Name b) -> e -> MH ()
mhHandleEventLensed ((TeamState -> f TeamState) -> ChatState -> f ChatState
Lens' ChatState TeamState
csCurrentTeam((TeamState -> f TeamState) -> ChatState -> f ChatState)
-> ((Editor Text Name -> f (Editor Text Name))
    -> TeamState -> f TeamState)
-> (Editor Text Name -> f (Editor Text Name))
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelTopicDialogState -> f ChannelTopicDialogState)
-> TeamState -> f TeamState
Lens' TeamState ChannelTopicDialogState
tsChannelTopicDialog((ChannelTopicDialogState -> f ChannelTopicDialogState)
 -> TeamState -> f TeamState)
-> ((Editor Text Name -> f (Editor Text Name))
    -> ChannelTopicDialogState -> f ChannelTopicDialogState)
-> (Editor Text Name -> f (Editor Text Name))
-> TeamState
-> f TeamState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Editor Text Name -> f (Editor Text Name))
-> ChannelTopicDialogState -> f ChannelTopicDialogState
Lens' ChannelTopicDialogState (Editor Text Name)
channelTopicDialogEditor)
                                Event -> Editor Text Name -> EventM Name (Editor Text Name)
forall t n.
(DecodeUtf8 t, Eq t, GenericTextZipper t) =>
Event -> Editor t n -> EventM n (Editor t n)
handleEditorEvent Event
e
        Maybe Name
_ ->
            () -> MH ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()