{-# LANGUAGE TemplateHaskell #-}
module Matterhorn.Types.MessageInterface
  ( MessageInterface(..)
  , miMessages
  , miEditor
  , miMode
  , miMessageSelect
  , miRootPostId
  , miChannelId
  , miTarget
  , miUrlListSource
  , miUrlList
  , miSaveAttachmentDialog

  , messageInterfaceCursor

  , MessageInterfaceMode(..)
  , MessageInterfaceTarget(..)
  , URLListSource(..)

  , URLList(..)
  , ulList
  , ulSource

  , SaveAttachmentDialogState(..)
  , attachmentPathEditor
  , attachmentPathDialogFocus
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import           Brick ( getName )
import           Brick.Focus ( FocusRing )
import           Brick.Widgets.List ( List )
import           Brick.Widgets.Edit ( Editor )
import           Brick.Widgets.FileBrowser ( fileBrowserNameG )
import qualified Data.Text as T
import           Lens.Micro.Platform ( makeLenses, _Just )
import           Network.Mattermost.Types ( ChannelId, TeamId )

import           Matterhorn.Types.Core ( MessageSelectState )
import           Matterhorn.Types.EditState
import           Matterhorn.Types.Messages


-- | A UI region in which a specific message listing is viewed, where
-- the user can send messages in that channel or thread.
data MessageInterface n i =
    MessageInterface { forall n i. MessageInterface n i -> Messages
_miMessages :: Messages
                     -- ^ The messages.
                     , forall n i. MessageInterface n i -> EditState n
_miEditor :: EditState n
                     -- ^ The editor and associated state for composing
                     -- messages in this channel or thread.
                     , forall n i. MessageInterface n i -> MessageSelectState
_miMessageSelect :: MessageSelectState
                     -- ^ Message selection state for the interface.
                     , forall n i. MessageInterface n i -> i
_miRootPostId :: i
                     -- ^ The root post ID if these messages belong to a
                     -- thread.
                     , forall n i. MessageInterface n i -> ChannelId
_miChannelId :: ChannelId
                     -- ^ The channel that these messages belong to.
                     , forall n i. MessageInterface n i -> MessageInterfaceMode
_miMode :: MessageInterfaceMode
                     -- ^ The mode of the interface.
                     , forall n i. MessageInterface n i -> MessageInterfaceTarget
_miTarget :: MessageInterfaceTarget
                     -- ^ The target value for this message interface
                     , forall n i. MessageInterface n i -> URLListSource
_miUrlListSource :: URLListSource
                     -- ^ How to characterize the URLs found in messages
                     -- in this interface
                     , forall n i. MessageInterface n i -> URLList n
_miUrlList :: URLList n
                     -- ^ The URL listing for this interface
                     , forall n i. MessageInterface n i -> SaveAttachmentDialogState n
_miSaveAttachmentDialog :: SaveAttachmentDialogState n
                     -- ^ The state for the interactive attachment-saving
                     -- editor window.
                     }

messageInterfaceCursor :: MessageInterface n i -> Maybe n
messageInterfaceCursor :: forall n i. MessageInterface n i -> Maybe n
messageInterfaceCursor MessageInterface n i
mi =
    case forall n i. MessageInterface n i -> MessageInterfaceMode
_miMode MessageInterface n i
mi of
        MessageInterfaceMode
Compose           -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a n. Named a n => a -> n
getName forall a b. (a -> b) -> a -> b
$ forall n. EditState n -> Editor Text n
_esEditor forall a b. (a -> b) -> a -> b
$ forall n i. MessageInterface n i -> EditState n
_miEditor MessageInterface n i
mi
        SaveAttachment {} -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a n. Named a n => a -> n
getName forall a b. (a -> b) -> a -> b
$ forall n. SaveAttachmentDialogState n -> Editor Text n
_attachmentPathEditor forall a b. (a -> b) -> a -> b
$ forall n i. MessageInterface n i -> SaveAttachmentDialogState n
_miSaveAttachmentDialog MessageInterface n i
mi
        MessageInterfaceMode
BrowseFiles       -> (forall n. EditState n -> Maybe (FileBrowser n)
_esFileBrowser forall a b. (a -> b) -> a -> b
$ forall n i. MessageInterface n i -> EditState n
_miEditor MessageInterface n i
mi)forall s a. s -> Getting (First a) s a -> Maybe a
^?forall a a'. Traversal (Maybe a) (Maybe a') a a'
_Justforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. SimpleGetter (FileBrowser n) n
fileBrowserNameG
        MessageInterfaceMode
ManageAttachments -> forall a. Maybe a
Nothing
        MessageInterfaceMode
MessageSelect     -> forall a. Maybe a
Nothing
        MessageInterfaceMode
ShowUrlList       -> forall a. Maybe a
Nothing

data MessageInterfaceMode =
    Compose
    -- ^ Composing messages and interacting with the editor
    | MessageSelect
    -- ^ Selecting from messages in the listing
    | ShowUrlList
    -- ^ Show the URL listing
    | SaveAttachment LinkChoice
    -- ^ Show the attachment save UI
    | ManageAttachments
    -- ^ Managing the attachment list
    | BrowseFiles
    -- ^ Browsing the filesystem for attachment files
    deriving (MessageInterfaceMode -> MessageInterfaceMode -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MessageInterfaceMode -> MessageInterfaceMode -> Bool
$c/= :: MessageInterfaceMode -> MessageInterfaceMode -> Bool
== :: MessageInterfaceMode -> MessageInterfaceMode -> Bool
$c== :: MessageInterfaceMode -> MessageInterfaceMode -> Bool
Eq, Int -> MessageInterfaceMode -> ShowS
[MessageInterfaceMode] -> ShowS
MessageInterfaceMode -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MessageInterfaceMode] -> ShowS
$cshowList :: [MessageInterfaceMode] -> ShowS
show :: MessageInterfaceMode -> String
$cshow :: MessageInterfaceMode -> String
showsPrec :: Int -> MessageInterfaceMode -> ShowS
$cshowsPrec :: Int -> MessageInterfaceMode -> ShowS
Show)

data URLListSource =
    FromChannel ChannelId
    | FromThreadIn ChannelId
    deriving (Int -> URLListSource -> ShowS
[URLListSource] -> ShowS
URLListSource -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [URLListSource] -> ShowS
$cshowList :: [URLListSource] -> ShowS
show :: URLListSource -> String
$cshow :: URLListSource -> String
showsPrec :: Int -> URLListSource -> ShowS
$cshowsPrec :: Int -> URLListSource -> ShowS
Show, URLListSource -> URLListSource -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: URLListSource -> URLListSource -> Bool
$c/= :: URLListSource -> URLListSource -> Bool
== :: URLListSource -> URLListSource -> Bool
$c== :: URLListSource -> URLListSource -> Bool
Eq)

data MessageInterfaceTarget =
    MITeamThread TeamId
    | MIChannel ChannelId
    deriving (MessageInterfaceTarget -> MessageInterfaceTarget -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MessageInterfaceTarget -> MessageInterfaceTarget -> Bool
$c/= :: MessageInterfaceTarget -> MessageInterfaceTarget -> Bool
== :: MessageInterfaceTarget -> MessageInterfaceTarget -> Bool
$c== :: MessageInterfaceTarget -> MessageInterfaceTarget -> Bool
Eq, Int -> MessageInterfaceTarget -> ShowS
[MessageInterfaceTarget] -> ShowS
MessageInterfaceTarget -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MessageInterfaceTarget] -> ShowS
$cshowList :: [MessageInterfaceTarget] -> ShowS
show :: MessageInterfaceTarget -> String
$cshow :: MessageInterfaceTarget -> String
showsPrec :: Int -> MessageInterfaceTarget -> ShowS
$cshowsPrec :: Int -> MessageInterfaceTarget -> ShowS
Show)

data URLList n =
    URLList { forall n. URLList n -> List n (Int, LinkChoice)
_ulList :: List n (Int, LinkChoice)
            , forall n. URLList n -> Maybe URLListSource
_ulSource :: Maybe URLListSource
            }

-- | The state of the attachment path window.
data SaveAttachmentDialogState n =
    SaveAttachmentDialogState { forall n. SaveAttachmentDialogState n -> Editor Text n
_attachmentPathEditor :: Editor T.Text n
                              -- ^ The attachment path editor state.
                              , forall n. SaveAttachmentDialogState n -> FocusRing n
_attachmentPathDialogFocus :: FocusRing n
                              -- ^ The window focus state (editor/buttons)
                              }

makeLenses ''MessageInterface
makeLenses ''URLList
makeLenses ''SaveAttachmentDialogState