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 =
    forall n. Widget n -> Widget n
centerLayer forall a b. (a -> b) -> a -> b
$
    forall n. Int -> Widget n -> Widget n
hLimit Int
maxWindowWidth forall a b. (a -> b) -> a -> b
$
    forall n. Widget n -> Widget n
joinBorders forall a b. (a -> b) -> a -> b
$
    forall n. Widget n -> Widget n -> Widget n
borderWithLabel (forall n. AttrName -> Widget n -> Widget n
withDefAttr AttrName
clientEmphAttr forall a b. (a -> b) -> a -> b
$ forall n. Text -> Widget n
txt Text
"Edit Channel Topic") forall a b. (a -> b) -> a -> b
$
    forall n. [Widget n] -> Widget n
vBox [ forall n. Int -> Widget n -> Widget n
vLimit Int
editorHeight forall a b. (a -> b) -> a -> b
$
           forall n a b.
(Eq n, Named a n) =>
FocusRing n -> (Bool -> a -> b) -> a -> b
withFocusRing FocusRing Name
foc (forall n t.
(Ord n, Show n, Monoid t, TextWidth t, GenericTextZipper t) =>
([t] -> Widget n) -> Bool -> Editor t n -> Widget n
renderEditor forall {n}. [Text] -> Widget n
drawTopicEditorTxt) Editor Text Name
ed
         , forall n. Widget n -> Widget n
hBorderWithLabel (forall n. AttrName -> Widget n -> Widget n
withDefAttr AttrName
clientEmphAttr forall a b. (a -> b) -> a -> b
$ forall n. Text -> Widget n
txt Text
"Preview")
         , forall n. Int -> Widget n -> Widget n
vLimit Int
previewHeight forall a b. (a -> b) -> a -> b
$
           forall n.
(Ord n, Show n) =>
n -> ViewportType -> Widget n -> Widget n
viewport (TeamId -> Name
ChannelTopicEditorPreview TeamId
tId) ViewportType
Vertical forall a b. (a -> b) -> a -> b
$
           forall a.
SemEq a =>
Maybe TeamBaseURL
-> Text
-> HighlightSet
-> Maybe (Int -> Inline -> Maybe a)
-> Text
-> Widget a
renderText' (forall a. a -> Maybe a
Just TeamBaseURL
baseUrl) Text
"" HighlightSet
hSet forall a. Maybe a
Nothing Text
topicTxtWithCursor
         , forall n. Widget n
hBorder
         , forall n. Widget n -> Widget n
hCenter forall a b. (a -> b) -> a -> b
$
           forall a.
SemEq a =>
Maybe TeamBaseURL
-> Text
-> HighlightSet
-> Maybe (Int -> Inline -> Maybe a)
-> Text
-> Widget a
renderText' forall a. Maybe a
Nothing Text
"" HighlightSet
hSet forall a. Maybe a
Nothing
           Text
"Press `Tab` to change focus to buttons."
         , forall n. [Widget n] -> Widget n
hBox [ forall n. Padding -> Widget n -> Widget n
padRight Padding
Max forall a b. (a -> b) -> a -> b
$
                  forall n. Padding -> Widget n -> Widget n
padLeft (Int -> Padding
Pad Int
1) forall a b. (a -> b) -> a -> b
$
                  forall n. (Eq n, Ord n) => FocusRing n -> n -> Text -> Widget n
drawButton FocusRing Name
foc (TeamId -> Name
ChannelTopicSaveButton TeamId
tId) Text
"Save"
                , forall n. Padding -> Widget n -> Widget n
padRight (Int -> Padding
Pad Int
1) forall a b. (a -> b) -> a -> b
$
                  forall n. (Eq n, Ord 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
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 ChannelTopicDialogState
tsChannelTopicDialogforall b c a. (b -> c) -> (a -> b) -> a -> c
.Lens' ChannelTopicDialogState (FocusRing Name)
channelTopicDialogFocus
        ed :: Editor Text Name
ed = 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 ChannelTopicDialogState
tsChannelTopicDialogforall b c a. (b -> c) -> (a -> b) -> a -> c
.Lens' ChannelTopicDialogState (Editor Text Name)
channelTopicDialogEditor
        hSet :: HighlightSet
hSet = ChatState -> TeamId -> HighlightSet
getHighlightSet ChatState
st TeamId
tId
        topicTxtWithCursor :: Text
topicTxtWithCursor = [Text] -> Text
T.unlines forall a b. (a -> b) -> a -> b
$
                             forall t n. Monoid t => Editor t n -> [t]
getEditContents forall a b. (a -> b) -> a -> b
$
                             forall t n.
(TextZipper t -> TextZipper t) -> Editor t n -> Editor t n
applyEdit (forall a. Monoid a => TextZipper a -> TextZipper a
gotoEOL forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall a. Monoid a => Char -> TextZipper a -> TextZipper a
insertChar Char
cursorSentinel) Editor Text Name
ed
        drawTopicEditorTxt :: [Text] -> Widget n
drawTopicEditorTxt = forall n. Text -> Widget n
txt forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Text
T.unlines