{-# LANGUAGE BangPatterns #-}
{-|
Module      : Client.Image
Description : UI renderer
Copyright   : (c) Eric Mertens, 2016
License     : ISC
Maintainer  : emertens@gmail.com

This module provides the renderer for the client's UI.

-}
module Client.Image (clientPicture) where

import Client.Image.Layout (drawLayout)
import Client.State (clientHeight, clientScroll, clientTextBoxOffset, ClientState)
import Control.Lens (view, over, set)
import Graphics.Vty (Background (..), Cursor (..),  Picture (..))
import Graphics.Vty.Image (Image)

-- | Generate a 'Picture' for the current client state. The resulting
-- client state is updated for render specific information like scrolling.
clientPicture :: ClientState -> (Picture, ClientState)
clientPicture :: ClientState -> (Picture, ClientState)
clientPicture ClientState
st = (Picture
pic, ClientState
st')
    where
      (Int
row, Int
col, Image
img, ClientState
st') = ClientState -> (Int, Int, Image, ClientState)
clientImage ClientState
st
      pic :: Picture
pic = Picture
              { picCursor :: Cursor
picCursor     = Int -> Int -> Cursor
AbsoluteCursor Int
col (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' ClientState Int
clientHeight ClientState
st forall a. Num a => a -> a -> a
- Int
row)
              , picBackground :: Background
picBackground = Background
ClearBackground
              , picLayers :: [Image]
picLayers     = [Image
img]
              }

-- | Primary UI render logic
clientImage ::
  ClientState               {- ^ client state -} ->
  (Int, Int, Image, ClientState) {- ^ cursor row, cursor col, image, updated state -}
clientImage :: ClientState -> (Int, Int, Image, ClientState)
clientImage ClientState
st = (Int
row, Int
col, Image
img, ClientState
st')
  where
    -- update client state for scroll clamp
    !st' :: ClientState
st' = forall s t a b. ASetter s t a b -> b -> s -> t
set Lens' ClientState Int
clientTextBoxOffset Int
nextOffset
         forall a b. (a -> b) -> a -> b
$ forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over Lens' ClientState Int
clientScroll (forall a. Ord a => a -> a -> a
max Int
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a -> a
subtract Int
overscroll) ClientState
st

    (Int
overscroll, Int
row, Int
col, Int
nextOffset, Image
img) = ClientState -> (Int, Int, Int, Int, Image)
drawLayout ClientState
st