{-# Language OverloadedStrings #-}
module Client.View.Cert
  ( certViewLines
  ) where
import           Client.Image.PackedImage
import           Client.Image.Palette
import           Client.State
import           Client.State.Focus
import           Client.State.Network
import           Control.Lens
import           Data.Text (Text)
import           Graphics.Vty.Attributes
certViewLines ::
  ClientState -> [Image']
certViewLines st
  | Just network <- currentNetwork st
  , Just cs <- preview (clientConnection network) st
  , let xs = view csCertificate cs
  , not (null xs)
  = text' defAttr <$> xs
  | otherwise = [text' (view palError pal) "No certificate available"]
  where
    pal = clientPalette st
currentNetwork :: ClientState -> Maybe Text
currentNetwork st =
  case view clientFocus st of
    NetworkFocus net   -> Just net
    ChannelFocus net _ -> Just net
    Unfocused          -> Nothing