{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}

module View
  ( viewState
  , viewQuestion
  , viewContext
  , viewSuggestion
  , viewMessage
  ) where

import           Brick
import           Brick.Widgets.List
import           Brick.Widgets.WrappedText
import           Data.Text (Text)
import qualified Data.Text as T
import qualified Hledger as HL

-- hledger-lib 1.17 will switch showTransaction to ISO date format, which means
-- that ISO dates yyyy-mm-dd will be added to the journal instead of yyyy/mm/dd.
--
-- Thus, for hledger-lib >=1.17, we also show the ISO format in the UI
#if !MIN_VERSION_hledger_lib(1,16,99)
import           Data.Time hiding (parseTime)
#endif

import           Model

viewState :: Step -> Widget n
viewState :: forall n. Step -> Widget n
viewState (DateQuestion Text
comment) = forall n. Text -> Widget n
txt forall a b. (a -> b) -> a -> b
$
  if Text -> Bool
T.null Text
comment then Text
" " else Text -> Text
viewComment Text
comment
viewState (DescriptionQuestion Day
date Text
comment) = forall n. Text -> Widget n
txt forall a b. (a -> b) -> a -> b
$
#if MIN_VERSION_hledger_lib(1,16,99)
  String -> Text
T.pack (forall a. Show a => a -> String
show Day
date)
#else
  T.pack (formatTime defaultTimeLocale "%Y/%m/%d" date)
#endif
  forall a. Semigroup a => a -> a -> a
<> Text -> Text
viewComment Text
comment
viewState (AccountQuestion Transaction
trans Text
comment) = forall n. Text -> Widget n
txt forall a b. (a -> b) -> a -> b
$
  Transaction -> Text
showTransaction Transaction
trans forall a. Semigroup a => a -> a -> a
<> Text -> Text
viewComment Text
comment
viewState (AmountQuestion Text
acc Transaction
trans Text
comment) = forall n. Text -> Widget n
txt forall a b. (a -> b) -> a -> b
$
  Transaction -> Text
showTransaction Transaction
trans forall a. Semigroup a => a -> a -> a
<> Text
"\n    " forall a. Semigroup a => a -> a -> a
<> Text
acc forall a. Semigroup a => a -> a -> a
<> Text -> Text
viewComment Text
comment
viewState (FinalQuestion Transaction
trans Bool
_) = forall n. Text -> Widget n
txt forall a b. (a -> b) -> a -> b
$
  Transaction -> Text
showTransaction Transaction
trans

viewQuestion :: Step -> Widget n
viewQuestion :: forall n. Step -> Widget n
viewQuestion (DateQuestion Text
_) = forall n. Text -> Widget n
txt Text
"Date"
viewQuestion (DescriptionQuestion Day
_ Text
_) = forall n. Text -> Widget n
txt Text
"Description"
viewQuestion (AccountQuestion Transaction
trans Text
_) = forall n. String -> Widget n
str forall a b. (a -> b) -> a -> b
$
  String
"Account " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Transaction -> Int
numPostings Transaction
trans forall a. Num a => a -> a -> a
+ Int
1)
viewQuestion (AmountQuestion Text
_ Transaction
trans Text
_) = forall n. String -> Widget n
str forall a b. (a -> b) -> a -> b
$
  String
"Amount " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Transaction -> Int
numPostings Transaction
trans forall a. Num a => a -> a -> a
+ Int
1)
viewQuestion (FinalQuestion Transaction
_ Bool
duplicate) = forall n. Text -> Widget n
txt forall a b. (a -> b) -> a -> b
$
  Text
"Add this transaction to the journal?"
  forall a. Semigroup a => a -> a -> a
<> (if Bool
duplicate then Text
" (warning: duplicate)" else Text
"") -- TODO Add better UI for duplicates
  forall a. Semigroup a => a -> a -> a
<> Text
" Y/n"

viewContext :: (Ord n, Show n) => List n Text -> Widget n
viewContext :: forall n. (Ord n, Show n) => List n Text -> Widget n
viewContext = forall (t :: * -> *) n e.
(Traversable t, Splittable t, Ord n, Show n) =>
(Bool -> e -> Widget n) -> Bool -> GenericList n t e -> Widget n
renderList forall n. Bool -> Text -> Widget n
renderItem Bool
True

viewSuggestion :: Maybe Text -> Widget n
viewSuggestion :: forall n. Maybe Text -> Widget n
viewSuggestion Maybe Text
Nothing = forall n. Text -> Widget n
txt Text
""
viewSuggestion (Just Text
t) = forall n. Text -> Widget n
txt forall a b. (a -> b) -> a -> b
$ Text
" (" forall a. Semigroup a => a -> a -> a
<> Text
t forall a. Semigroup a => a -> a -> a
<> Text
")"

renderItem :: Bool -> Text -> Widget n
renderItem :: forall n. Bool -> Text -> Widget n
renderItem Bool
True = forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
listSelectedAttr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. Padding -> Widget n -> Widget n
padRight Padding
Max forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. Text -> Widget n
txt
renderItem Bool
False = forall n. Text -> Widget n
txt

numPostings :: HL.Transaction -> Int
numPostings :: Transaction -> Int
numPostings = forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> [Posting]
HL.tpostings

-- TODO Adding " " to an empty message isn't required for vty >= 5.14
--      => Remove this, once 5.14 becomes lower bound
viewMessage :: Text -> Widget n
viewMessage :: forall n. Text -> Widget n
viewMessage Text
msg = forall n. Text -> Widget n
wrappedText (if Text -> Bool
T.null Text
msg then Text
" " else Text
msg)

viewComment :: Text -> Text
viewComment :: Text -> Text
viewComment Text
comment
  | Text -> Bool
T.null Text
comment = Text
""
  | Bool
otherwise      = [Text] -> Text
T.unlines forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (Text
"  ; " forall a. Semigroup a => a -> a -> a
<>) forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines Text
comment


showTransaction :: HL.Transaction -> Text
showTransaction :: Transaction -> Text
showTransaction = Text -> Text
T.stripEnd forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Text
HL.showTransaction