module Matterhorn.Draw.ChannelTopicWindow
  ( drawChannelTopicWindow
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import           Brick
import           Brick.Focus
import           Brick.Widgets.Border
import           Brick.Widgets.Center
import           Brick.Widgets.Edit

import           Control.Arrow ( (>>>) )
import qualified Data.Text as T
import           Data.Text.Zipper ( insertChar, gotoEOL )

import           Network.Mattermost.Types ( TeamId )

import           Matterhorn.Types
import           Matterhorn.Draw.Buttons
import           Matterhorn.Draw.RichText
import           Matterhorn.Themes


drawChannelTopicWindow :: ChatState -> TeamId -> Widget Name
drawChannelTopicWindow :: ChatState -> TeamId -> Widget Name
drawChannelTopicWindow ChatState
st TeamId
tId =
    Widget Name -> Widget Name
forall n. Widget n -> Widget n
centerLayer (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
    Int -> Widget Name -> Widget Name
forall n. Int -> Widget n -> Widget n
hLimit Int
maxWindowWidth (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
    Widget Name -> Widget Name
forall n. Widget n -> Widget n
joinBorders (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
    Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
borderWithLabel (AttrName -> Widget Name -> Widget Name
forall n. AttrName -> Widget n -> Widget n
withDefAttr AttrName
clientEmphAttr (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$ Text -> Widget Name
forall n. Text -> Widget n
txt Text
"Edit Channel Topic") (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
    [Widget Name] -> Widget Name
forall n. [Widget n] -> Widget n
vBox [ Int -> Widget Name -> Widget Name
forall n. Int -> Widget n -> Widget n
vLimit Int
editorHeight (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
           FocusRing Name
-> (Bool -> Editor Text Name -> Widget Name)
-> Editor Text Name
-> Widget Name
forall n a b.
(Eq n, Named a n) =>
FocusRing n -> (Bool -> a -> b) -> a -> b
withFocusRing FocusRing Name
foc (([Text] -> Widget Name) -> Bool -> Editor Text Name -> Widget Name
forall n t.
(Ord n, Show n, Monoid t, TextWidth t, GenericTextZipper t) =>
([t] -> Widget n) -> Bool -> Editor t n -> Widget n
renderEditor [Text] -> Widget Name
forall n. [Text] -> Widget n
drawTopicEditorTxt) Editor Text Name
ed
         , Widget Name -> Widget Name
forall n. Widget n -> Widget n
hBorderWithLabel (AttrName -> Widget Name -> Widget Name
forall n. AttrName -> Widget n -> Widget n
withDefAttr AttrName
clientEmphAttr (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$ Text -> Widget Name
forall n. Text -> Widget n
txt Text
"Preview")
         , Int -> Widget Name -> Widget Name
forall n. Int -> Widget n -> Widget n
vLimit Int
previewHeight (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
           Name -> ViewportType -> Widget Name -> Widget Name
forall n.
(Ord n, Show n) =>
n -> ViewportType -> Widget n -> Widget n
viewport (TeamId -> Name
ChannelTopicEditorPreview TeamId
tId) ViewportType
Vertical (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
           Maybe TeamBaseURL
-> Text
-> HighlightSet
-> Maybe (Int -> Inline -> Maybe Name)
-> Text
-> Widget Name
forall a.
SemEq a =>
Maybe TeamBaseURL
-> Text
-> HighlightSet
-> Maybe (Int -> Inline -> Maybe a)
-> Text
-> Widget a
renderText' (TeamBaseURL -> Maybe TeamBaseURL
forall a. a -> Maybe a
Just TeamBaseURL
baseUrl) Text
"" HighlightSet
hSet Maybe (Int -> Inline -> Maybe Name)
forall a. Maybe a
Nothing Text
topicTxtWithCursor
         , Widget Name
forall n. Widget n
hBorder
         , [Widget Name] -> Widget Name
forall n. [Widget n] -> Widget n
hBox [ Padding -> Widget Name -> Widget Name
forall n. Padding -> Widget n -> Widget n
padRight Padding
Max (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
                  Padding -> Widget Name -> Widget Name
forall n. Padding -> Widget n -> Widget n
padLeft (Int -> Padding
Pad Int
1) (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
                  FocusRing Name -> Name -> Text -> Widget Name
forall n. Eq n => FocusRing n -> n -> Text -> Widget n
drawButton FocusRing Name
foc (TeamId -> Name
ChannelTopicSaveButton TeamId
tId) Text
"Save"
                , Padding -> Widget Name -> Widget Name
forall n. Padding -> Widget n -> Widget n
padRight (Int -> Padding
Pad Int
1) (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
                  FocusRing Name -> Name -> Text -> Widget Name
forall n. Eq n => FocusRing n -> n -> Text -> Widget n
drawButton FocusRing Name
foc (TeamId -> Name
ChannelTopicCancelButton TeamId
tId) Text
"Cancel"
                ]
         ]
    where
        baseUrl :: TeamBaseURL
baseUrl = ChatState -> TeamId -> TeamBaseURL
serverBaseUrl ChatState
st TeamId
tId
        editorHeight :: Int
editorHeight = Int
5
        previewHeight :: Int
previewHeight = Int
5
        maxWindowWidth :: Int
maxWindowWidth = Int
70
        foc :: FocusRing Name
foc = ChatState
stChatState
-> Getting (FocusRing Name) ChatState (FocusRing Name)
-> FocusRing Name
forall s a. s -> Getting a s a -> a
^.TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)((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
        ed :: Editor Text Name
ed = ChatState
stChatState
-> Getting (Editor Text Name) ChatState (Editor Text Name)
-> Editor Text Name
forall s a. s -> Getting a s a -> a
^.TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)((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
        hSet :: HighlightSet
hSet = ChatState -> TeamId -> HighlightSet
getHighlightSet ChatState
st TeamId
tId
        topicTxtWithCursor :: Text
topicTxtWithCursor = [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 -> [Text]) -> Editor Text Name -> [Text]
forall a b. (a -> b) -> a -> b
$
                             (TextZipper Text -> TextZipper Text)
-> Editor Text Name -> Editor Text Name
forall t n.
(TextZipper t -> TextZipper t) -> Editor t n -> Editor t n
applyEdit (TextZipper Text -> TextZipper Text
forall a. Monoid a => TextZipper a -> TextZipper a
gotoEOL (TextZipper Text -> TextZipper Text)
-> (TextZipper Text -> TextZipper Text)
-> TextZipper Text
-> TextZipper Text
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Char -> TextZipper Text -> TextZipper Text
forall a. Monoid a => Char -> TextZipper a -> TextZipper a
insertChar Char
cursorSentinel) Editor Text Name
ed
        drawTopicEditorTxt :: [Text] -> Widget n
drawTopicEditorTxt = Text -> Widget n
forall n. Text -> Widget n
txt (Text -> Widget n) -> ([Text] -> Text) -> [Text] -> Widget n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Text
T.unlines