{-# LANGUAGE RankNTypes #-}
module Matterhorn.Draw.ManageAttachments
  ( drawAttachmentList
  , drawFileBrowser
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import           Brick
import           Brick.Widgets.Border
import           Brick.Widgets.Center
import qualified Brick.Widgets.FileBrowser as FB
import           Brick.Widgets.List
import           Data.Maybe ( fromJust )
import           Lens.Micro.Platform ( Lens' )

import           Matterhorn.Types
import           Matterhorn.Types.KeyEvents
import           Matterhorn.Events.Keybindings ( firstActiveBinding )
import           Matterhorn.Themes


drawAttachmentList :: ChatState -> Lens' ChatState (MessageInterface Name i) -> Widget Name
drawAttachmentList :: ChatState
-> Lens' ChatState (MessageInterface Name i) -> Widget Name
drawAttachmentList ChatState
st Lens' ChatState (MessageInterface Name i)
which =
    let addBinding :: Text
addBinding = Binding -> Text
ppBinding (Binding -> Text) -> Binding -> Text
forall a b. (a -> b) -> a -> b
$ KeyConfig -> KeyEvent -> Binding
firstActiveBinding KeyConfig
kc KeyEvent
AttachmentListAddEvent
        delBinding :: Text
delBinding = Binding -> Text
ppBinding (Binding -> Text) -> Binding -> Text
forall a b. (a -> b) -> a -> b
$ KeyConfig -> KeyEvent -> Binding
firstActiveBinding KeyConfig
kc KeyEvent
AttachmentListDeleteEvent
        escBinding :: Text
escBinding = Binding -> Text
ppBinding (Binding -> Text) -> Binding -> Text
forall a b. (a -> b) -> a -> b
$ KeyConfig -> KeyEvent -> Binding
firstActiveBinding KeyConfig
kc KeyEvent
CancelEvent
        openBinding :: Text
openBinding = Binding -> Text
ppBinding (Binding -> Text) -> Binding -> Text
forall a b. (a -> b) -> a -> b
$ KeyConfig -> KeyEvent -> Binding
firstActiveBinding KeyConfig
kc KeyEvent
AttachmentOpenEvent
        kc :: KeyConfig
kc = ChatState
stChatState -> Getting KeyConfig ChatState KeyConfig -> KeyConfig
forall s a. s -> Getting a s a -> a
^.(ChatResources -> Const KeyConfig ChatResources)
-> ChatState -> Const KeyConfig ChatState
Lens' ChatState ChatResources
csResources((ChatResources -> Const KeyConfig ChatResources)
 -> ChatState -> Const KeyConfig ChatState)
-> ((KeyConfig -> Const KeyConfig KeyConfig)
    -> ChatResources -> Const KeyConfig ChatResources)
-> Getting KeyConfig ChatState KeyConfig
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Config -> Const KeyConfig Config)
-> ChatResources -> Const KeyConfig ChatResources
Lens' ChatResources Config
crConfiguration((Config -> Const KeyConfig Config)
 -> ChatResources -> Const KeyConfig ChatResources)
-> ((KeyConfig -> Const KeyConfig KeyConfig)
    -> Config -> Const KeyConfig Config)
-> (KeyConfig -> Const KeyConfig KeyConfig)
-> ChatResources
-> Const KeyConfig ChatResources
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(KeyConfig -> Const KeyConfig KeyConfig)
-> Config -> Const KeyConfig Config
Lens' Config KeyConfig
configUserKeysL
    in 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
"Attachments") (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 [ (Bool -> AttachmentData -> Widget Name)
-> Bool -> GenericList Name Vector AttachmentData -> Widget Name
forall (t :: * -> *) n e.
(Traversable t, Splittable t, Ord n, Show n) =>
(Bool -> e -> Widget n) -> Bool -> GenericList n t e -> Widget n
renderList Bool -> AttachmentData -> Widget Name
renderAttachmentItem Bool
True (ChatState
stChatState
-> Getting
     (GenericList Name Vector AttachmentData)
     ChatState
     (GenericList Name Vector AttachmentData)
-> GenericList Name Vector AttachmentData
forall s a. s -> Getting a s a -> a
^.(MessageInterface Name i
 -> Const
      (GenericList Name Vector AttachmentData) (MessageInterface Name i))
-> ChatState
-> Const (GenericList Name Vector AttachmentData) ChatState
Lens' ChatState (MessageInterface Name i)
which((MessageInterface Name i
  -> Const
       (GenericList Name Vector AttachmentData) (MessageInterface Name i))
 -> ChatState
 -> Const (GenericList Name Vector AttachmentData) ChatState)
-> ((GenericList Name Vector AttachmentData
     -> Const
          (GenericList Name Vector AttachmentData)
          (GenericList Name Vector AttachmentData))
    -> MessageInterface Name i
    -> Const
         (GenericList Name Vector AttachmentData) (MessageInterface Name i))
-> Getting
     (GenericList Name Vector AttachmentData)
     ChatState
     (GenericList Name Vector AttachmentData)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(EditState Name
 -> Const (GenericList Name Vector AttachmentData) (EditState Name))
-> MessageInterface Name i
-> Const
     (GenericList Name Vector AttachmentData) (MessageInterface Name i)
forall n i. Lens' (MessageInterface n i) (EditState n)
miEditor((EditState Name
  -> Const (GenericList Name Vector AttachmentData) (EditState Name))
 -> MessageInterface Name i
 -> Const
      (GenericList Name Vector AttachmentData) (MessageInterface Name i))
-> ((GenericList Name Vector AttachmentData
     -> Const
          (GenericList Name Vector AttachmentData)
          (GenericList Name Vector AttachmentData))
    -> EditState Name
    -> Const (GenericList Name Vector AttachmentData) (EditState Name))
-> (GenericList Name Vector AttachmentData
    -> Const
         (GenericList Name Vector AttachmentData)
         (GenericList Name Vector AttachmentData))
-> MessageInterface Name i
-> Const
     (GenericList Name Vector AttachmentData) (MessageInterface Name i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(GenericList Name Vector AttachmentData
 -> Const
      (GenericList Name Vector AttachmentData)
      (GenericList Name Vector AttachmentData))
-> EditState Name
-> Const (GenericList Name Vector AttachmentData) (EditState Name)
forall n. Lens' (EditState n) (List n AttachmentData)
esAttachmentList)
            , Widget Name
forall n. Widget n
hBorder
            , Widget Name -> Widget Name
forall n. Widget n -> Widget n
hCenter (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$ AttrName -> Widget Name -> Widget Name
forall n. AttrName -> Widget n -> Widget n
withDefAttr AttrName
clientMessageAttr (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 -> Widget Name) -> Text -> Widget Name
forall a b. (a -> b) -> a -> b
$ Text
addBinding Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":add " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
                              Text
delBinding Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":delete " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
                              Text
openBinding Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":open " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
                              Text
escBinding Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":close"
            ]

renderAttachmentItem :: Bool -> AttachmentData -> Widget Name
renderAttachmentItem :: Bool -> AttachmentData -> Widget Name
renderAttachmentItem Bool
_ AttachmentData
d =
    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
$ String -> Widget Name
forall n. String -> Widget n
str (String -> Widget Name) -> String -> Widget Name
forall a b. (a -> b) -> a -> b
$ FileInfo -> String
FB.fileInfoSanitizedFilename (FileInfo -> String) -> FileInfo -> String
forall a b. (a -> b) -> a -> b
$ AttachmentData -> FileInfo
attachmentDataFileInfo AttachmentData
d

drawFileBrowser :: ChatState -> Lens' ChatState (MessageInterface Name i) -> Widget Name
drawFileBrowser :: ChatState
-> Lens' ChatState (MessageInterface Name i) -> Widget Name
drawFileBrowser ChatState
st Lens' ChatState (MessageInterface Name i)
which =
    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
"Attach File") (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
    -- invariant: cedFileBrowser is not Nothing if appMode is
    -- ManageAttachmentsBrowseFiles, and that is the only way to reach
    -- this code, ergo the fromJust.
    Bool -> FileBrowser Name -> Widget Name
forall n. (Show n, Ord n) => Bool -> FileBrowser n -> Widget n
FB.renderFileBrowser Bool
True (FileBrowser Name -> Widget Name)
-> FileBrowser Name -> Widget Name
forall a b. (a -> b) -> a -> b
$ Maybe (FileBrowser Name) -> FileBrowser Name
forall a. HasCallStack => Maybe a -> a
fromJust (ChatState
stChatState
-> Getting
     (Maybe (FileBrowser Name)) ChatState (Maybe (FileBrowser Name))
-> Maybe (FileBrowser Name)
forall s a. s -> Getting a s a -> a
^.(MessageInterface Name i
 -> Const (Maybe (FileBrowser Name)) (MessageInterface Name i))
-> ChatState -> Const (Maybe (FileBrowser Name)) ChatState
Lens' ChatState (MessageInterface Name i)
which((MessageInterface Name i
  -> Const (Maybe (FileBrowser Name)) (MessageInterface Name i))
 -> ChatState -> Const (Maybe (FileBrowser Name)) ChatState)
-> ((Maybe (FileBrowser Name)
     -> Const (Maybe (FileBrowser Name)) (Maybe (FileBrowser Name)))
    -> MessageInterface Name i
    -> Const (Maybe (FileBrowser Name)) (MessageInterface Name i))
-> Getting
     (Maybe (FileBrowser Name)) ChatState (Maybe (FileBrowser Name))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(EditState Name
 -> Const (Maybe (FileBrowser Name)) (EditState Name))
-> MessageInterface Name i
-> Const (Maybe (FileBrowser Name)) (MessageInterface Name i)
forall n i. Lens' (MessageInterface n i) (EditState n)
miEditor((EditState Name
  -> Const (Maybe (FileBrowser Name)) (EditState Name))
 -> MessageInterface Name i
 -> Const (Maybe (FileBrowser Name)) (MessageInterface Name i))
-> ((Maybe (FileBrowser Name)
     -> Const (Maybe (FileBrowser Name)) (Maybe (FileBrowser Name)))
    -> EditState Name
    -> Const (Maybe (FileBrowser Name)) (EditState Name))
-> (Maybe (FileBrowser Name)
    -> Const (Maybe (FileBrowser Name)) (Maybe (FileBrowser Name)))
-> MessageInterface Name i
-> Const (Maybe (FileBrowser Name)) (MessageInterface Name i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe (FileBrowser Name)
 -> Const (Maybe (FileBrowser Name)) (Maybe (FileBrowser Name)))
-> EditState Name
-> Const (Maybe (FileBrowser Name)) (EditState Name)
forall n. Lens' (EditState n) (Maybe (FileBrowser n))
esFileBrowser)