module Bytor.Event where

import qualified Brick as B
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import qualified Graphics.Vty as Vty
import           Lens.Micro.Platform

import           Bytor.Types

handleEvent :: State -> B.BrickEvent Cursor Event -> B.EventM Cursor (B.Next State)
handleEvent st (B.VtyEvent (Vty.EvKey (Vty.KChar c) mods)) = do
  let kc = KeyCode c $
        case mods of
          [Vty.MMeta] -> Meta
          [Vty.MCtrl] -> Ctrl
          _           -> None
  case Map.lookup kc (st^.stConfiguration.confKeymap) of
    Just action -> runAction st action
    Nothing     -> B.continue (st & stMessage .~ T.pack (show (mods, c)))
handleEvent st _ = B.continue st

runAction :: State -> Action -> B.EventM Cursor (B.Next State)
runAction st Quit = B.halt st
