{-# LANGUAGE RankNTypes #-}
module Matterhorn.Events.UrlSelect where

import           Prelude ()
import           Matterhorn.Prelude

import           Brick.Widgets.List
import qualified Graphics.Vty as Vty
import           Lens.Micro.Platform ( Lens' )

import           Matterhorn.Events.Keybindings
import           Matterhorn.State.UrlSelect
import           Matterhorn.State.SaveAttachmentWindow
import           Matterhorn.Types


onEventUrlSelect :: Lens' ChatState (MessageInterface Name i) -> Vty.Event -> MH Bool
onEventUrlSelect :: Lens' ChatState (MessageInterface Name i) -> Event -> MH Bool
onEventUrlSelect Lens' ChatState (MessageInterface Name i)
which =
    [Event -> MH Bool] -> Event -> MH Bool
handleEventWith [ (KeyConfig -> KeyHandlerMap) -> Event -> MH Bool
handleKeyboardEvent (Lens' ChatState (MessageInterface Name i)
-> KeyConfig -> KeyHandlerMap
forall i.
Lens' ChatState (MessageInterface Name i)
-> KeyConfig -> KeyHandlerMap
urlSelectKeybindings Lens' ChatState (MessageInterface Name i)
which)
                    , \Event
e -> Lens' ChatState (GenericList Name Vector (Int, LinkChoice))
-> (Event
    -> GenericList Name Vector (Int, LinkChoice)
    -> EventM Name (GenericList Name Vector (Int, LinkChoice)))
-> Event
-> MH ()
forall b e.
Lens' ChatState b -> (e -> b -> EventM Name b) -> e -> MH ()
mhHandleEventLensed ((MessageInterface Name i -> f (MessageInterface Name i))
-> ChatState -> f ChatState
Lens' ChatState (MessageInterface Name i)
which((MessageInterface Name i -> f (MessageInterface Name i))
 -> ChatState -> f ChatState)
-> ((GenericList Name Vector (Int, LinkChoice)
     -> f (GenericList Name Vector (Int, LinkChoice)))
    -> MessageInterface Name i -> f (MessageInterface Name i))
-> (GenericList Name Vector (Int, LinkChoice)
    -> f (GenericList Name Vector (Int, LinkChoice)))
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(URLList Name -> f (URLList Name))
-> MessageInterface Name i -> f (MessageInterface Name i)
forall n i. Lens' (MessageInterface n i) (URLList n)
miUrlList((URLList Name -> f (URLList Name))
 -> MessageInterface Name i -> f (MessageInterface Name i))
-> ((GenericList Name Vector (Int, LinkChoice)
     -> f (GenericList Name Vector (Int, LinkChoice)))
    -> URLList Name -> f (URLList Name))
-> (GenericList Name Vector (Int, LinkChoice)
    -> f (GenericList Name Vector (Int, LinkChoice)))
-> MessageInterface Name i
-> f (MessageInterface Name i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(GenericList Name Vector (Int, LinkChoice)
 -> f (GenericList Name Vector (Int, LinkChoice)))
-> URLList Name -> f (URLList Name)
forall n1 n2.
Lens
  (URLList n1)
  (URLList n2)
  (List n1 (Int, LinkChoice))
  (List n2 (Int, LinkChoice))
ulList) Event
-> GenericList Name Vector (Int, LinkChoice)
-> EventM Name (GenericList Name Vector (Int, LinkChoice))
forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
Event -> GenericList n t e -> EventM n (GenericList n t e)
handleListEvent Event
e MH () -> MH Bool -> MH Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> MH Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                    ]

urlSelectKeybindings :: Lens' ChatState (MessageInterface Name i) -> KeyConfig -> KeyHandlerMap
urlSelectKeybindings :: Lens' ChatState (MessageInterface Name i)
-> KeyConfig -> KeyHandlerMap
urlSelectKeybindings Lens' ChatState (MessageInterface Name i)
which = [KeyEventHandler] -> KeyConfig -> KeyHandlerMap
mkKeybindings (Lens' ChatState (MessageInterface Name i) -> [KeyEventHandler]
forall i.
Lens' ChatState (MessageInterface Name i) -> [KeyEventHandler]
urlSelectKeyHandlers Lens' ChatState (MessageInterface Name i)
which)

urlSelectKeyHandlers :: Lens' ChatState (MessageInterface Name i) -> [KeyEventHandler]
urlSelectKeyHandlers :: Lens' ChatState (MessageInterface Name i) -> [KeyEventHandler]
urlSelectKeyHandlers Lens' ChatState (MessageInterface Name i)
which =
    [ Text -> Event -> MH () -> KeyEventHandler
staticKb Text
"Open the selected URL, if any"
         (Key -> [Modifier] -> Event
Vty.EvKey Key
Vty.KEnter []) (MH () -> KeyEventHandler) -> MH () -> KeyEventHandler
forall a b. (a -> b) -> a -> b
$
             Lens' ChatState (MessageInterface Name i) -> MH ()
forall n i. Lens' ChatState (MessageInterface n i) -> MH ()
openSelectedURL Lens' ChatState (MessageInterface Name i)
which

    , KeyEvent -> Text -> MH () -> KeyEventHandler
mkKb KeyEvent
SaveAttachmentEvent Text
"Save the selected attachment" (MH () -> KeyEventHandler) -> MH () -> KeyEventHandler
forall a b. (a -> b) -> a -> b
$
        Lens' ChatState (MessageInterface Name i) -> MH ()
forall i. Lens' ChatState (MessageInterface Name i) -> MH ()
openSaveAttachmentWindow Lens' ChatState (MessageInterface Name i)
which

    , KeyEvent -> Text -> MH () -> KeyEventHandler
mkKb KeyEvent
CancelEvent Text
"Cancel URL selection" (MH () -> KeyEventHandler) -> MH () -> KeyEventHandler
forall a b. (a -> b) -> a -> b
$ Lens' ChatState (MessageInterface Name i) -> MH ()
forall n i. Lens' ChatState (MessageInterface n i) -> MH ()
stopUrlSelect Lens' ChatState (MessageInterface Name i)
which

    , KeyEvent -> Text -> MH () -> KeyEventHandler
mkKb KeyEvent
SelectUpEvent Text
"Move cursor up" (MH () -> KeyEventHandler) -> MH () -> KeyEventHandler
forall a b. (a -> b) -> a -> b
$
        Lens' ChatState (GenericList Name Vector (Int, LinkChoice))
-> (Event
    -> GenericList Name Vector (Int, LinkChoice)
    -> EventM Name (GenericList Name Vector (Int, LinkChoice)))
-> Event
-> MH ()
forall b e.
Lens' ChatState b -> (e -> b -> EventM Name b) -> e -> MH ()
mhHandleEventLensed ((MessageInterface Name i -> f (MessageInterface Name i))
-> ChatState -> f ChatState
Lens' ChatState (MessageInterface Name i)
which((MessageInterface Name i -> f (MessageInterface Name i))
 -> ChatState -> f ChatState)
-> ((GenericList Name Vector (Int, LinkChoice)
     -> f (GenericList Name Vector (Int, LinkChoice)))
    -> MessageInterface Name i -> f (MessageInterface Name i))
-> (GenericList Name Vector (Int, LinkChoice)
    -> f (GenericList Name Vector (Int, LinkChoice)))
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(URLList Name -> f (URLList Name))
-> MessageInterface Name i -> f (MessageInterface Name i)
forall n i. Lens' (MessageInterface n i) (URLList n)
miUrlList((URLList Name -> f (URLList Name))
 -> MessageInterface Name i -> f (MessageInterface Name i))
-> ((GenericList Name Vector (Int, LinkChoice)
     -> f (GenericList Name Vector (Int, LinkChoice)))
    -> URLList Name -> f (URLList Name))
-> (GenericList Name Vector (Int, LinkChoice)
    -> f (GenericList Name Vector (Int, LinkChoice)))
-> MessageInterface Name i
-> f (MessageInterface Name i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(GenericList Name Vector (Int, LinkChoice)
 -> f (GenericList Name Vector (Int, LinkChoice)))
-> URLList Name -> f (URLList Name)
forall n1 n2.
Lens
  (URLList n1)
  (URLList n2)
  (List n1 (Int, LinkChoice))
  (List n2 (Int, LinkChoice))
ulList) Event
-> GenericList Name Vector (Int, LinkChoice)
-> EventM Name (GenericList Name Vector (Int, LinkChoice))
forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
Event -> GenericList n t e -> EventM n (GenericList n t e)
handleListEvent (Key -> [Modifier] -> Event
Vty.EvKey Key
Vty.KUp [])

    , KeyEvent -> Text -> MH () -> KeyEventHandler
mkKb KeyEvent
SelectDownEvent Text
"Move cursor down" (MH () -> KeyEventHandler) -> MH () -> KeyEventHandler
forall a b. (a -> b) -> a -> b
$
        Lens' ChatState (GenericList Name Vector (Int, LinkChoice))
-> (Event
    -> GenericList Name Vector (Int, LinkChoice)
    -> EventM Name (GenericList Name Vector (Int, LinkChoice)))
-> Event
-> MH ()
forall b e.
Lens' ChatState b -> (e -> b -> EventM Name b) -> e -> MH ()
mhHandleEventLensed ((MessageInterface Name i -> f (MessageInterface Name i))
-> ChatState -> f ChatState
Lens' ChatState (MessageInterface Name i)
which((MessageInterface Name i -> f (MessageInterface Name i))
 -> ChatState -> f ChatState)
-> ((GenericList Name Vector (Int, LinkChoice)
     -> f (GenericList Name Vector (Int, LinkChoice)))
    -> MessageInterface Name i -> f (MessageInterface Name i))
-> (GenericList Name Vector (Int, LinkChoice)
    -> f (GenericList Name Vector (Int, LinkChoice)))
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(URLList Name -> f (URLList Name))
-> MessageInterface Name i -> f (MessageInterface Name i)
forall n i. Lens' (MessageInterface n i) (URLList n)
miUrlList((URLList Name -> f (URLList Name))
 -> MessageInterface Name i -> f (MessageInterface Name i))
-> ((GenericList Name Vector (Int, LinkChoice)
     -> f (GenericList Name Vector (Int, LinkChoice)))
    -> URLList Name -> f (URLList Name))
-> (GenericList Name Vector (Int, LinkChoice)
    -> f (GenericList Name Vector (Int, LinkChoice)))
-> MessageInterface Name i
-> f (MessageInterface Name i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(GenericList Name Vector (Int, LinkChoice)
 -> f (GenericList Name Vector (Int, LinkChoice)))
-> URLList Name -> f (URLList Name)
forall n1 n2.
Lens
  (URLList n1)
  (URLList n2)
  (List n1 (Int, LinkChoice))
  (List n2 (Int, LinkChoice))
ulList) Event
-> GenericList Name Vector (Int, LinkChoice)
-> EventM Name (GenericList Name Vector (Int, LinkChoice))
forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
Event -> GenericList n t e -> EventM n (GenericList n t e)
handleListEvent (Key -> [Modifier] -> Event
Vty.EvKey Key
Vty.KDown [])

    , Text -> Event -> MH () -> KeyEventHandler
staticKb Text
"Cancel URL selection"
         (Key -> [Modifier] -> Event
Vty.EvKey (Char -> Key
Vty.KChar Char
'q') []) (MH () -> KeyEventHandler) -> MH () -> KeyEventHandler
forall a b. (a -> b) -> a -> b
$ Lens' ChatState (MessageInterface Name i) -> MH ()
forall n i. Lens' ChatState (MessageInterface n i) -> MH ()
stopUrlSelect Lens' ChatState (MessageInterface Name i)
which

    ]