-- | Pretty printing for safe-money Lovelace values

{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Blockfrost.Pretty.Ada
  ( prettyLovelaces
  , prettyLovelaces'
  , prettyLovelacesDoc
  , prettyLovelacesDoc'
  ) where

import Blockfrost.Pretty.Config
import Blockfrost.Types.Shared.Ada
import Data.Default (Default (def))
import Data.Text (Text)
import qualified Money

import Prettyprinter
import Prettyprinter.Render.Terminal

-- | Pretty print `Lovelaces` using default configuration
prettyLovelaces :: Lovelaces -> Text
prettyLovelaces :: Lovelaces -> Text
prettyLovelaces = PrettyConfig -> Lovelaces -> Text
prettyLovelaces' PrettyConfig
forall a. Default a => a
def

-- | Pretty print `Lovelaces` using supplied `PrettyConfig`
prettyLovelaces' :: PrettyConfig -> Lovelaces -> Text
prettyLovelaces' :: PrettyConfig -> Lovelaces -> Text
prettyLovelaces' PrettyConfig{Bool
DecimalConf
pcLovelaceDecimalConf :: PrettyConfig -> DecimalConf
pcAdaDecimalConf :: PrettyConfig -> DecimalConf
pcUnicode :: PrettyConfig -> Bool
pcTestAda :: PrettyConfig -> Bool
pcLovelaceDecimalConf :: DecimalConf
pcAdaDecimalConf :: DecimalConf
pcUnicode :: Bool
pcTestAda :: Bool
..} Lovelaces
x | Discrete' "ADA" '(1000000, 1)
Lovelaces
x Discrete' "ADA" '(1000000, 1)
-> Discrete' "ADA" '(1000000, 1) -> Bool
forall a. Ord a => a -> a -> Bool
> Integer -> Discrete' "ADA" '(1000000, 1)
forall (scale :: (Nat, Nat)) (currency :: Symbol).
GoodScale scale =>
Integer -> Discrete' currency scale
Money.discrete Integer
1_000_000 =
  DecimalConf -> Approximation -> Dense "ADA" -> Text
forall (currency :: Symbol).
DecimalConf -> Approximation -> Dense currency -> Text
Money.denseToDecimal
    DecimalConf
pcAdaDecimalConf
    Approximation
Money.Round
    (Discrete' "ADA" '(1000000, 1) -> Dense "ADA"
forall (scale :: (Nat, Nat)) (currency :: Symbol).
GoodScale scale =>
Discrete' currency scale -> Dense currency
Money.denseFromDiscrete Discrete' "ADA" '(1000000, 1)
Lovelaces
x)
  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" "
  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Bool -> Text
prefixIfTestAda Bool
pcTestAda
  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (if Bool
pcUnicode then Text
adaSymbol else Text
"ADA")
prettyLovelaces' PrettyConfig{Bool
DecimalConf
pcLovelaceDecimalConf :: DecimalConf
pcAdaDecimalConf :: DecimalConf
pcUnicode :: Bool
pcTestAda :: Bool
pcLovelaceDecimalConf :: PrettyConfig -> DecimalConf
pcAdaDecimalConf :: PrettyConfig -> DecimalConf
pcUnicode :: PrettyConfig -> Bool
pcTestAda :: PrettyConfig -> Bool
..} Lovelaces
x =
  DecimalConf
-> Approximation -> Discrete' "ADA" '(1000000, 1) -> Text
forall (scale :: (Nat, Nat)) (currency :: Symbol).
GoodScale scale =>
DecimalConf -> Approximation -> Discrete' currency scale -> Text
Money.discreteToDecimal
    DecimalConf
pcLovelaceDecimalConf
    Approximation
Money.Round
    Discrete' "ADA" '(1000000, 1)
Lovelaces
x
  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" "
  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Bool -> Text
prefixIfTestAda Bool
pcTestAda
  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"lovelace"
  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (if Discrete' "ADA" '(1000000, 1)
Lovelaces
x Discrete' "ADA" '(1000000, 1)
-> Discrete' "ADA" '(1000000, 1) -> Bool
forall a. Eq a => a -> a -> Bool
== Discrete' "ADA" '(1000000, 1)
1 then Text
forall a. Monoid a => a
mempty else Text
"s")

-- | Pretty print `Lovelaces` to `Doc` using supplied `PrettyConfig`
prettyLovelacesDoc' :: PrettyConfig -> Lovelaces -> Doc AnsiStyle
prettyLovelacesDoc' :: PrettyConfig -> Lovelaces -> Doc AnsiStyle
prettyLovelacesDoc' PrettyConfig
cfg =
    AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle
forall ann. ann -> Doc ann -> Doc ann
annotate (Color -> AnsiStyle
color Color
Magenta)
  (Doc AnsiStyle -> Doc AnsiStyle)
-> (Discrete' "ADA" '(1000000, 1) -> Doc AnsiStyle)
-> Discrete' "ADA" '(1000000, 1)
-> Doc AnsiStyle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Doc AnsiStyle
forall a ann. Pretty a => a -> Doc ann
pretty
  (Text -> Doc AnsiStyle)
-> (Discrete' "ADA" '(1000000, 1) -> Text)
-> Discrete' "ADA" '(1000000, 1)
-> Doc AnsiStyle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrettyConfig -> Lovelaces -> Text
prettyLovelaces' PrettyConfig
cfg

-- | Pretty print `Lovelaces` to `Doc` using default config
prettyLovelacesDoc :: Lovelaces -> Doc AnsiStyle
prettyLovelacesDoc :: Lovelaces -> Doc AnsiStyle
prettyLovelacesDoc = PrettyConfig -> Lovelaces -> Doc AnsiStyle
prettyLovelacesDoc' PrettyConfig
forall a. Default a => a
def

prefixIfTestAda :: Bool -> Text
prefixIfTestAda :: Bool -> Text
prefixIfTestAda Bool
False = Text
forall a. Monoid a => a
mempty
prefixIfTestAda Bool
True  = Text
"t"