-- | POSIX Milliseconds wrapper

module Blockfrost.Types.Shared.POSIXMillis
  ( POSIXMillis
  , POSIXTime
  , millisecondsToPosix
  , posixToMilliseconds
  , seconds
  ) where

import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.Time.Clock.POSIX
import GHC.Generics
import Servant.Docs (ToSample (..), singleSample)

-- | Convert `Integer` milliseconds to `POSIXTime`
millisecondsToPosix :: Integer -> POSIXTime
millisecondsToPosix :: Integer -> POSIXTime
millisecondsToPosix Integer
n = Integer -> POSIXTime
forall a. Num a => Integer -> a
fromInteger Integer
n POSIXTime -> POSIXTime -> POSIXTime
forall a. Fractional a => a -> a -> a
/ POSIXTime
1000

-- | Convert `POSIXTime` to `Integer` milliseconds
posixToMilliseconds :: POSIXTime -> Integer
posixToMilliseconds :: POSIXTime -> Integer
posixToMilliseconds POSIXTime
t = POSIXTime -> Integer
forall b. Integral b => POSIXTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (POSIXTime
t POSIXTime -> POSIXTime -> POSIXTime
forall a. Num a => a -> a -> a
* POSIXTime
1000)

newtype POSIXMillis = POSIXMillis {
    POSIXMillis -> POSIXTime
unPOSIXMillis :: POSIXTime
  }
  deriving stock (Int -> POSIXMillis -> ShowS
[POSIXMillis] -> ShowS
POSIXMillis -> String
(Int -> POSIXMillis -> ShowS)
-> (POSIXMillis -> String)
-> ([POSIXMillis] -> ShowS)
-> Show POSIXMillis
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> POSIXMillis -> ShowS
showsPrec :: Int -> POSIXMillis -> ShowS
$cshow :: POSIXMillis -> String
show :: POSIXMillis -> String
$cshowList :: [POSIXMillis] -> ShowS
showList :: [POSIXMillis] -> ShowS
Show, POSIXMillis -> POSIXMillis -> Bool
(POSIXMillis -> POSIXMillis -> Bool)
-> (POSIXMillis -> POSIXMillis -> Bool) -> Eq POSIXMillis
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: POSIXMillis -> POSIXMillis -> Bool
== :: POSIXMillis -> POSIXMillis -> Bool
$c/= :: POSIXMillis -> POSIXMillis -> Bool
/= :: POSIXMillis -> POSIXMillis -> Bool
Eq, (forall x. POSIXMillis -> Rep POSIXMillis x)
-> (forall x. Rep POSIXMillis x -> POSIXMillis)
-> Generic POSIXMillis
forall x. Rep POSIXMillis x -> POSIXMillis
forall x. POSIXMillis -> Rep POSIXMillis x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. POSIXMillis -> Rep POSIXMillis x
from :: forall x. POSIXMillis -> Rep POSIXMillis x
$cto :: forall x. Rep POSIXMillis x -> POSIXMillis
to :: forall x. Rep POSIXMillis x -> POSIXMillis
Generic)

seconds :: POSIXTime -> POSIXMillis
seconds :: POSIXTime -> POSIXMillis
seconds = POSIXTime -> POSIXMillis
POSIXMillis

instance FromJSON POSIXMillis where
  parseJSON :: Value -> Parser POSIXMillis
parseJSON Value
t = POSIXTime -> POSIXMillis
POSIXMillis (POSIXTime -> POSIXMillis)
-> (Integer -> POSIXTime) -> Integer -> POSIXMillis
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> POSIXTime
millisecondsToPosix (Integer -> POSIXMillis) -> Parser Integer -> Parser POSIXMillis
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Value -> Parser a
parseJSON @Integer Value
t

instance ToJSON POSIXMillis where
  toJSON :: POSIXMillis -> Value
toJSON (POSIXMillis POSIXTime
t) = Integer -> Value
forall a. ToJSON a => a -> Value
toJSON (Integer -> Value) -> Integer -> Value
forall a b. (a -> b) -> a -> b
$ POSIXTime -> Integer
posixToMilliseconds POSIXTime
t
  toEncoding :: POSIXMillis -> Encoding
toEncoding (POSIXMillis POSIXTime
t) = Integer -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding (Integer -> Encoding) -> Integer -> Encoding
forall a b. (a -> b) -> a -> b
$ POSIXTime -> Integer
posixToMilliseconds POSIXTime
t

instance ToSample POSIXMillis where
    toSamples :: Proxy POSIXMillis -> [(Text, POSIXMillis)]
toSamples Proxy POSIXMillis
_ = POSIXMillis -> [(Text, POSIXMillis)]
forall a. a -> [(Text, a)]
singleSample (POSIXMillis -> [(Text, POSIXMillis)])
-> POSIXMillis -> [(Text, POSIXMillis)]
forall a b. (a -> b) -> a -> b
$ POSIXTime -> POSIXMillis
POSIXMillis (POSIXTime -> POSIXMillis) -> POSIXTime -> POSIXMillis
forall a b. (a -> b) -> a -> b
$ Integer -> POSIXTime
millisecondsToPosix Integer
1603400958947