module BtcLsp.Text
  ( toHex,
    toQr,
    displayRational,
    mkHtmlUuid,
  )
where

import BtcLsp.Import.External
import qualified Codec.QRCode as QR
  ( ErrorLevel (L),
    TextEncoding (Iso8859_1OrUtf8WithoutECI),
    defaultQRCodeOptions,
    encodeAutomatic,
  )
import qualified Codec.QRCode.JuicyPixels as JP
  ( toPngDataUrlT,
  )
import qualified Data.ByteString.Base16 as B16
import qualified Data.Text.Format.Numbers as F
import qualified Data.UUID as UUID
import qualified Data.UUID.V4 as UUID
import qualified Language.Haskell.TH.Syntax as TH

toHex :: ByteString -> Text
toHex :: ByteString -> Text
toHex =
  ByteString -> Text
forall a b. ConvertUtf8 a b => b -> a
decodeUtf8
    (ByteString -> Text)
-> (ByteString -> ByteString) -> ByteString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B16.encode

toQr :: Text -> Maybe Text
toQr :: Text -> Maybe Text
toQr =
  (Text -> Text
toStrict (Text -> Text) -> (QRImage -> Text) -> QRImage -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> QRImage -> Text
JP.toPngDataUrlT Int
4 Int
5 (QRImage -> Text) -> Maybe QRImage -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>)
    (Maybe QRImage -> Maybe Text)
-> (Text -> Maybe QRImage) -> Text -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QRCodeOptions -> TextEncoding -> Text -> Maybe QRImage
forall a.
ToText a =>
QRCodeOptions -> TextEncoding -> a -> Maybe QRImage
QR.encodeAutomatic
      (ErrorLevel -> QRCodeOptions
QR.defaultQRCodeOptions ErrorLevel
QR.L)
      TextEncoding
QR.Iso8859_1OrUtf8WithoutECI

displayRational :: Int -> Rational -> Text
displayRational :: Int -> Rational -> Text
displayRational Int
len =
  PrettyCfg -> Rational -> Text
forall i. RealFrac i => PrettyCfg -> i -> Text
F.prettyF
    PrettyCfg :: Int -> Maybe Char -> Char -> PrettyCfg
F.PrettyCfg
      { pc_decimals :: Int
F.pc_decimals = Int
len,
        pc_thousandsSep :: Maybe Char
F.pc_thousandsSep = Char -> Maybe Char
forall a. a -> Maybe a
Just Char
',',
        pc_decimalSep :: Char
F.pc_decimalSep = Char
'.'
      }

mkHtmlUuid :: TH.Q TH.Exp
mkHtmlUuid :: Q Exp
mkHtmlUuid =
  Text -> Q Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
TH.lift
    (Text -> Q Exp) -> (UUID -> Text) -> UUID -> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
"uuid-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>)
    (Text -> Text) -> (UUID -> Text) -> UUID -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UUID -> Text
UUID.toText
    (UUID -> Q Exp) -> Q UUID -> Q Exp
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO UUID -> Q UUID
forall a. IO a -> Q a
TH.runIO IO UUID
UUID.nextRandom