{-# LANGUAGE RankNTypes #-}
module Matterhorn.State.SaveAttachmentWindow
  ( openSaveAttachmentWindow
  , closeSaveAttachmentWindow
  )
where

import           Prelude ()
import           Matterhorn.Prelude
import           Brick ( getName )
import           Brick.Widgets.List ( listSelectedElement )

import           Lens.Micro.Platform ( Lens', (.=), to )

import           Network.Mattermost.Types ( fileInfoName )
import           Network.Mattermost.Endpoints ( mmGetMetadataForFile )

import           Matterhorn.Types
import           Matterhorn.State.Common
import           Matterhorn.State.Teams ( newSaveAttachmentDialog )


-- | If the currently selected link in the URL list is for an
-- attachment, open a window to get the user to provide a path to which
-- to save the attachment. If the URL list is empty or if the selected
-- entry is not for an attachment, this returns to the Main mode but
-- otherwise does nothing.
openSaveAttachmentWindow :: Lens' ChatState (MessageInterface Name i) -> MH ()
openSaveAttachmentWindow :: forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
openSaveAttachmentWindow Lens' ChatState (MessageInterface Name i)
which = do
    Maybe (Int, (Int, LinkChoice))
selected <- Getting
  (Maybe (Int, (Int, LinkChoice)))
  ChatState
  (Maybe (Int, (Int, LinkChoice)))
-> MH (Maybe (Int, (Int, LinkChoice)))
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use ((MessageInterface Name i
 -> Const
      (Maybe (Int, (Int, LinkChoice))) (MessageInterface Name i))
-> ChatState -> Const (Maybe (Int, (Int, LinkChoice))) ChatState
Lens' ChatState (MessageInterface Name i)
which((MessageInterface Name i
  -> Const
       (Maybe (Int, (Int, LinkChoice))) (MessageInterface Name i))
 -> ChatState -> Const (Maybe (Int, (Int, LinkChoice))) ChatState)
-> ((Maybe (Int, (Int, LinkChoice))
     -> Const
          (Maybe (Int, (Int, LinkChoice))) (Maybe (Int, (Int, LinkChoice))))
    -> MessageInterface Name i
    -> Const
         (Maybe (Int, (Int, LinkChoice))) (MessageInterface Name i))
-> Getting
     (Maybe (Int, (Int, LinkChoice)))
     ChatState
     (Maybe (Int, (Int, LinkChoice)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(URLList Name
 -> Const (Maybe (Int, (Int, LinkChoice))) (URLList Name))
-> MessageInterface Name i
-> Const (Maybe (Int, (Int, LinkChoice))) (MessageInterface Name i)
forall n i (f :: * -> *).
Functor f =>
(URLList n -> f (URLList n))
-> MessageInterface n i -> f (MessageInterface n i)
miUrlList((URLList Name
  -> Const (Maybe (Int, (Int, LinkChoice))) (URLList Name))
 -> MessageInterface Name i
 -> Const
      (Maybe (Int, (Int, LinkChoice))) (MessageInterface Name i))
-> ((Maybe (Int, (Int, LinkChoice))
     -> Const
          (Maybe (Int, (Int, LinkChoice))) (Maybe (Int, (Int, LinkChoice))))
    -> URLList Name
    -> Const (Maybe (Int, (Int, LinkChoice))) (URLList Name))
-> (Maybe (Int, (Int, LinkChoice))
    -> Const
         (Maybe (Int, (Int, LinkChoice))) (Maybe (Int, (Int, LinkChoice))))
-> MessageInterface Name i
-> Const (Maybe (Int, (Int, LinkChoice))) (MessageInterface Name i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(List Name (Int, LinkChoice)
 -> Const
      (Maybe (Int, (Int, LinkChoice))) (List Name (Int, LinkChoice)))
-> URLList Name
-> Const (Maybe (Int, (Int, LinkChoice))) (URLList Name)
forall n1 n2 (f :: * -> *).
Functor f =>
(List n1 (Int, LinkChoice) -> f (List n2 (Int, LinkChoice)))
-> URLList n1 -> f (URLList n2)
ulList((List Name (Int, LinkChoice)
  -> Const
       (Maybe (Int, (Int, LinkChoice))) (List Name (Int, LinkChoice)))
 -> URLList Name
 -> Const (Maybe (Int, (Int, LinkChoice))) (URLList Name))
-> ((Maybe (Int, (Int, LinkChoice))
     -> Const
          (Maybe (Int, (Int, LinkChoice))) (Maybe (Int, (Int, LinkChoice))))
    -> List Name (Int, LinkChoice)
    -> Const
         (Maybe (Int, (Int, LinkChoice))) (List Name (Int, LinkChoice)))
-> (Maybe (Int, (Int, LinkChoice))
    -> Const
         (Maybe (Int, (Int, LinkChoice))) (Maybe (Int, (Int, LinkChoice))))
-> URLList Name
-> Const (Maybe (Int, (Int, LinkChoice))) (URLList Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(List Name (Int, LinkChoice) -> Maybe (Int, (Int, LinkChoice)))
-> SimpleGetter
     (List Name (Int, LinkChoice)) (Maybe (Int, (Int, LinkChoice)))
forall s a. (s -> a) -> SimpleGetter s a
to List Name (Int, LinkChoice) -> Maybe (Int, (Int, LinkChoice))
forall (t :: * -> *) e n.
(Splittable t, Traversable t, Semigroup (t e)) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement)
    case Maybe (Int, (Int, LinkChoice))
selected of
        Maybe (Int, (Int, LinkChoice))
Nothing -> () -> MH ()
forall a. a -> MH a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just (Int
_, (Int
_, LinkChoice
link)) ->
            case LinkChoice
linkLinkChoice
-> Getting LinkTarget LinkChoice LinkTarget -> LinkTarget
forall s a. s -> Getting a s a -> a
^.Getting LinkTarget LinkChoice LinkTarget
Lens' LinkChoice LinkTarget
linkTarget of
                LinkFileId FileId
fId -> do
                    Session
session <- MH Session
getSession
                    AsyncPriority -> IO (Maybe (MH ())) -> MH ()
doAsyncWith AsyncPriority
Normal (IO (Maybe (MH ())) -> MH ()) -> IO (Maybe (MH ())) -> MH ()
forall a b. (a -> b) -> a -> b
$ do
                        FileInfo
info <- FileId -> Session -> IO FileInfo
mmGetMetadataForFile FileId
fId Session
session
                        Maybe (MH ()) -> IO (Maybe (MH ()))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (MH ()) -> IO (Maybe (MH ())))
-> Maybe (MH ()) -> IO (Maybe (MH ()))
forall a b. (a -> b) -> a -> b
$ MH () -> Maybe (MH ())
forall a. a -> Maybe a
Just (MH () -> Maybe (MH ())) -> MH () -> Maybe (MH ())
forall a b. (a -> b) -> a -> b
$ do
                            -- Use the message interface's URL list name
                            -- as a unique basis for the names of the UI
                            -- elements in the attachment dialog
                            Name
listName <- List Name (Int, LinkChoice) -> Name
forall a n. Named a n => a -> n
getName (List Name (Int, LinkChoice) -> Name)
-> MH (List Name (Int, LinkChoice)) -> MH Name
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Getting
  (List Name (Int, LinkChoice))
  ChatState
  (List Name (Int, LinkChoice))
-> MH (List Name (Int, LinkChoice))
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use ((MessageInterface Name i
 -> Const (List Name (Int, LinkChoice)) (MessageInterface Name i))
-> ChatState -> Const (List Name (Int, LinkChoice)) ChatState
Lens' ChatState (MessageInterface Name i)
which((MessageInterface Name i
  -> Const (List Name (Int, LinkChoice)) (MessageInterface Name i))
 -> ChatState -> Const (List Name (Int, LinkChoice)) ChatState)
-> ((List Name (Int, LinkChoice)
     -> Const
          (List Name (Int, LinkChoice)) (List Name (Int, LinkChoice)))
    -> MessageInterface Name i
    -> Const (List Name (Int, LinkChoice)) (MessageInterface Name i))
-> Getting
     (List Name (Int, LinkChoice))
     ChatState
     (List Name (Int, LinkChoice))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(URLList Name
 -> Const (List Name (Int, LinkChoice)) (URLList Name))
-> MessageInterface Name i
-> Const (List Name (Int, LinkChoice)) (MessageInterface Name i)
forall n i (f :: * -> *).
Functor f =>
(URLList n -> f (URLList n))
-> MessageInterface n i -> f (MessageInterface n i)
miUrlList((URLList Name
  -> Const (List Name (Int, LinkChoice)) (URLList Name))
 -> MessageInterface Name i
 -> Const (List Name (Int, LinkChoice)) (MessageInterface Name i))
-> ((List Name (Int, LinkChoice)
     -> Const
          (List Name (Int, LinkChoice)) (List Name (Int, LinkChoice)))
    -> URLList Name
    -> Const (List Name (Int, LinkChoice)) (URLList Name))
-> (List Name (Int, LinkChoice)
    -> Const
         (List Name (Int, LinkChoice)) (List Name (Int, LinkChoice)))
-> MessageInterface Name i
-> Const (List Name (Int, LinkChoice)) (MessageInterface Name i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(List Name (Int, LinkChoice)
 -> Const
      (List Name (Int, LinkChoice)) (List Name (Int, LinkChoice)))
-> URLList Name
-> Const (List Name (Int, LinkChoice)) (URLList Name)
forall n1 n2 (f :: * -> *).
Functor f =>
(List n1 (Int, LinkChoice) -> f (List n2 (Int, LinkChoice)))
-> URLList n1 -> f (URLList n2)
ulList)
                            (MessageInterface Name i -> Identity (MessageInterface Name i))
-> ChatState -> Identity ChatState
Lens' ChatState (MessageInterface Name i)
which((MessageInterface Name i -> Identity (MessageInterface Name i))
 -> ChatState -> Identity ChatState)
-> ((SaveAttachmentDialogState Name
     -> Identity (SaveAttachmentDialogState Name))
    -> MessageInterface Name i -> Identity (MessageInterface Name i))
-> (SaveAttachmentDialogState Name
    -> Identity (SaveAttachmentDialogState Name))
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SaveAttachmentDialogState Name
 -> Identity (SaveAttachmentDialogState Name))
-> MessageInterface Name i -> Identity (MessageInterface Name i)
forall n i (f :: * -> *).
Functor f =>
(SaveAttachmentDialogState n -> f (SaveAttachmentDialogState n))
-> MessageInterface n i -> f (MessageInterface n i)
miSaveAttachmentDialog ((SaveAttachmentDialogState Name
  -> Identity (SaveAttachmentDialogState Name))
 -> ChatState -> Identity ChatState)
-> SaveAttachmentDialogState Name -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= Name -> Text -> SaveAttachmentDialogState Name
newSaveAttachmentDialog Name
listName (FileInfo -> Text
fileInfoName FileInfo
info)
                            (MessageInterface Name i -> Identity (MessageInterface Name i))
-> ChatState -> Identity ChatState
Lens' ChatState (MessageInterface Name i)
which((MessageInterface Name i -> Identity (MessageInterface Name i))
 -> ChatState -> Identity ChatState)
-> ((MessageInterfaceMode -> Identity MessageInterfaceMode)
    -> MessageInterface Name i -> Identity (MessageInterface Name i))
-> (MessageInterfaceMode -> Identity MessageInterfaceMode)
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(MessageInterfaceMode -> Identity MessageInterfaceMode)
-> MessageInterface Name i -> Identity (MessageInterface Name i)
forall n i (f :: * -> *).
Functor f =>
(MessageInterfaceMode -> f MessageInterfaceMode)
-> MessageInterface n i -> f (MessageInterface n i)
miMode ((MessageInterfaceMode -> Identity MessageInterfaceMode)
 -> ChatState -> Identity ChatState)
-> MessageInterfaceMode -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= LinkChoice -> MessageInterfaceMode
SaveAttachment LinkChoice
link
                LinkTarget
_ ->
                    -- The selected link is not for an attachment.
                    () -> MH ()
forall a. a -> MH a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

closeSaveAttachmentWindow :: Lens' ChatState (MessageInterface n i)
                          -> MH ()
closeSaveAttachmentWindow :: forall n i. Lens' ChatState (MessageInterface n i) -> MH ()
closeSaveAttachmentWindow Lens' ChatState (MessageInterface n i)
which =
    (MessageInterface n i -> Identity (MessageInterface n i))
-> ChatState -> Identity ChatState
Lens' ChatState (MessageInterface n i)
which((MessageInterface n i -> Identity (MessageInterface n i))
 -> ChatState -> Identity ChatState)
-> ((MessageInterfaceMode -> Identity MessageInterfaceMode)
    -> MessageInterface n i -> Identity (MessageInterface n i))
-> (MessageInterfaceMode -> Identity MessageInterfaceMode)
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(MessageInterfaceMode -> Identity MessageInterfaceMode)
-> MessageInterface n i -> Identity (MessageInterface n i)
forall n i (f :: * -> *).
Functor f =>
(MessageInterfaceMode -> f MessageInterfaceMode)
-> MessageInterface n i -> f (MessageInterface n i)
miMode ((MessageInterfaceMode -> Identity MessageInterfaceMode)
 -> ChatState -> Identity ChatState)
-> MessageInterfaceMode -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= MessageInterfaceMode
Compose