{-# LANGUAGE RankNTypes #-}
module Matterhorn.State.UrlSelect
  (
  -- * URL selection mode
    startUrlSelect
  , stopUrlSelect
  , openSelectedURL
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import           Brick.Widgets.List ( listSelectedElement, listReplace )
import qualified Data.Vector as V
import           Lens.Micro.Platform ( (.=), (%=), to, Lens' )

import           Matterhorn.State.Links
import           Matterhorn.Types
import           Matterhorn.Util


startUrlSelect :: Lens' ChatState (MessageInterface n i)
               -> MH ()
startUrlSelect :: forall n i. Lens' ChatState (MessageInterface n i) -> MH ()
startUrlSelect Lens' ChatState (MessageInterface n i)
which = do
    Messages
msgs <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' ChatState (MessageInterface n i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) Messages
miMessages)
    URLListSource
src <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' ChatState (MessageInterface n i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) URLListSource
miUrlListSource)
    let urls :: Vector LinkChoice
urls = forall a. [a] -> Vector a
V.fromList forall a b. (a -> b) -> a -> b
$ Messages -> [LinkChoice]
findUrls Messages
msgs
        urlsWithIndexes :: Vector (Int, LinkChoice)
urlsWithIndexes = forall a. Vector a -> Vector (Int, a)
V.indexed Vector LinkChoice
urls
    Lens' ChatState (MessageInterface n i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) MessageInterfaceMode
miMode forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= MessageInterfaceMode
ShowUrlList
    Lens' ChatState (MessageInterface n i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (URLList n)
miUrlListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n1 n2.
Lens
  (URLList n1)
  (URLList n2)
  (List n1 (Int, LinkChoice))
  (List n2 (Int, LinkChoice))
ulList forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= forall (t :: * -> *) e n.
(Foldable t, Splittable t) =>
t e -> Maybe Int -> GenericList n t e -> GenericList n t e
listReplace Vector (Int, LinkChoice)
urlsWithIndexes (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length Vector LinkChoice
urls forall a. Num a => a -> a -> a
- Int
1)
    Lens' ChatState (MessageInterface n i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (URLList n)
miUrlListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n. Lens' (URLList n) (Maybe URLListSource)
ulSource forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= forall a. a -> Maybe a
Just URLListSource
src

stopUrlSelect :: Lens' ChatState (MessageInterface n i)
              -> MH ()
stopUrlSelect :: forall n i. Lens' ChatState (MessageInterface n i) -> MH ()
stopUrlSelect Lens' ChatState (MessageInterface n i)
which = do
    Lens' ChatState (MessageInterface n i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) MessageInterfaceMode
miMode forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= MessageInterfaceMode
Compose

openSelectedURL :: Lens' ChatState (MessageInterface n i) -> MH ()
openSelectedURL :: forall n i. Lens' ChatState (MessageInterface n i) -> MH ()
openSelectedURL Lens' ChatState (MessageInterface n i)
which = do
    Maybe (Int, (Int, LinkChoice))
selected <- forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' ChatState (MessageInterface n i)
whichforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n i. Lens' (MessageInterface n i) (URLList n)
miUrlListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall n1 n2.
Lens
  (URLList n1)
  (URLList n2)
  (List n1 (Int, LinkChoice))
  (List n2 (Int, LinkChoice))
ulListforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s a. (s -> a) -> SimpleGetter s a
to 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 -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just (Int
_, (Int
_, LinkChoice
link)) -> LinkTarget -> MH ()
openLinkTarget (LinkChoice
linkforall s a. s -> Getting a s a -> a
^.Lens' LinkChoice LinkTarget
linkTarget)
    forall n i. Lens' ChatState (MessageInterface n i) -> MH ()
stopUrlSelect Lens' ChatState (MessageInterface n i)
which

findUrls :: Messages -> [LinkChoice]
findUrls :: Messages -> [LinkChoice]
findUrls Messages
ms =
    let msgs :: Messages
msgs = forall seq a.
SeqDirection seq =>
(a -> Bool) -> DirectionalSeq seq a -> DirectionalSeq seq a
filterMessages (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Message -> Bool
_mDeleted) Messages
ms
    in [LinkChoice] -> [LinkChoice]
removeDuplicates forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Message -> Seq LinkChoice
msgURLs forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Messages
msgs

removeDuplicates :: [LinkChoice] -> [LinkChoice]
removeDuplicates :: [LinkChoice] -> [LinkChoice]
removeDuplicates = forall b a. Ord b => (a -> b) -> [a] -> [a]
nubOn (\ LinkChoice
l -> (LinkChoice
lforall s a. s -> Getting a s a -> a
^.Lens' LinkChoice LinkTarget
linkTarget, LinkChoice
lforall s a. s -> Getting a s a -> a
^.Lens' LinkChoice UserRef
linkUser, LinkChoice
lforall s a. s -> Getting a s a -> a
^.Lens' LinkChoice (Maybe Inlines)
linkLabel))