{-# LANGUAGE OverloadedStrings #-}

module CoinbasePro.WebSocketFeed.Channel.Full.Received
    ( Received (..)
    ) where

import           Data.Aeson        (FromJSON (..), withObject, (.:), (.:?))
import           Data.Time.Clock   (UTCTime)
import           Data.UUID         (fromString)

import           CoinbasePro.Types (ClientOrderId (..), Funds, OrderId,
                                    OrderType, Price, ProductId, ProfileId,
                                    Sequence, Side, Size, UserId)


data Received = Received
    { Received -> UTCTime
time      :: UTCTime
    , Received -> ProductId
productId :: ProductId
    , Received -> Sequence
sequence  :: Sequence
    , Received -> OrderId
orderId   :: OrderId
    , Received -> Maybe ClientOrderId
clientOid :: Maybe ClientOrderId
    , Received -> Maybe Size
size      :: Maybe Size
    , Received -> Maybe Price
price     :: Maybe Price
    , Received -> Maybe Funds
funds     :: Maybe Funds
    , Received -> Side
side      :: Side
    , Received -> OrderType
orderType :: OrderType
    , Received -> Maybe UserId
userId    :: Maybe UserId
    , Received -> Maybe UserId
profileId :: Maybe ProfileId
    } deriving (Received -> Received -> Bool
(Received -> Received -> Bool)
-> (Received -> Received -> Bool) -> Eq Received
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Received -> Received -> Bool
$c/= :: Received -> Received -> Bool
== :: Received -> Received -> Bool
$c== :: Received -> Received -> Bool
Eq, Eq Received
Eq Received
-> (Received -> Received -> Ordering)
-> (Received -> Received -> Bool)
-> (Received -> Received -> Bool)
-> (Received -> Received -> Bool)
-> (Received -> Received -> Bool)
-> (Received -> Received -> Received)
-> (Received -> Received -> Received)
-> Ord Received
Received -> Received -> Bool
Received -> Received -> Ordering
Received -> Received -> Received
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Received -> Received -> Received
$cmin :: Received -> Received -> Received
max :: Received -> Received -> Received
$cmax :: Received -> Received -> Received
>= :: Received -> Received -> Bool
$c>= :: Received -> Received -> Bool
> :: Received -> Received -> Bool
$c> :: Received -> Received -> Bool
<= :: Received -> Received -> Bool
$c<= :: Received -> Received -> Bool
< :: Received -> Received -> Bool
$c< :: Received -> Received -> Bool
compare :: Received -> Received -> Ordering
$ccompare :: Received -> Received -> Ordering
$cp1Ord :: Eq Received
Ord, Sequence -> Received -> ShowS
[Received] -> ShowS
Received -> String
(Sequence -> Received -> ShowS)
-> (Received -> String) -> ([Received] -> ShowS) -> Show Received
forall a.
(Sequence -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Received] -> ShowS
$cshowList :: [Received] -> ShowS
show :: Received -> String
$cshow :: Received -> String
showsPrec :: Sequence -> Received -> ShowS
$cshowsPrec :: Sequence -> Received -> ShowS
Show)


instance FromJSON Received where
    parseJSON :: Value -> Parser Received
parseJSON = String -> (Object -> Parser Received) -> Value -> Parser Received
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"received" ((Object -> Parser Received) -> Value -> Parser Received)
-> (Object -> Parser Received) -> Value -> Parser Received
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        UTCTime
t     <- Object
o Object -> UserId -> Parser UTCTime
forall a. FromJSON a => Object -> UserId -> Parser a
.: UserId
"time"
        ProductId
prd   <- Object
o Object -> UserId -> Parser ProductId
forall a. FromJSON a => Object -> UserId -> Parser a
.: UserId
"product_id"
        Sequence
sq    <- Object
o Object -> UserId -> Parser Sequence
forall a. FromJSON a => Object -> UserId -> Parser a
.: UserId
"sequence"
        OrderId
oid   <- Object
o Object -> UserId -> Parser OrderId
forall a. FromJSON a => Object -> UserId -> Parser a
.: UserId
"order_id"
        Maybe ClientOrderId
cloid <- (UUID -> ClientOrderId
ClientOrderId (UUID -> ClientOrderId) -> Maybe UUID -> Maybe ClientOrderId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) (Maybe UUID -> Maybe ClientOrderId)
-> (String -> Maybe UUID) -> String -> Maybe ClientOrderId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe UUID
fromString (String -> Maybe ClientOrderId)
-> Parser String -> Parser (Maybe ClientOrderId)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Object
o Object -> UserId -> Parser String
forall a. FromJSON a => Object -> UserId -> Parser a
.: UserId
"client_oid")
        Maybe Size
sz    <- Object
o Object -> UserId -> Parser (Maybe Size)
forall a. FromJSON a => Object -> UserId -> Parser (Maybe a)
.:? UserId
"size"
        Maybe Price
px    <- Object
o Object -> UserId -> Parser (Maybe Price)
forall a. FromJSON a => Object -> UserId -> Parser (Maybe a)
.:? UserId
"price"
        Maybe Funds
fs    <- Object
o Object -> UserId -> Parser (Maybe Funds)
forall a. FromJSON a => Object -> UserId -> Parser (Maybe a)
.:? UserId
"funds"
        Side
sd    <- Object
o Object -> UserId -> Parser Side
forall a. FromJSON a => Object -> UserId -> Parser a
.: UserId
"side"
        OrderType
ot    <- Object
o Object -> UserId -> Parser OrderType
forall a. FromJSON a => Object -> UserId -> Parser a
.: UserId
"order_type"
        Maybe UserId
ui    <- Object
o Object -> UserId -> Parser (Maybe UserId)
forall a. FromJSON a => Object -> UserId -> Parser (Maybe a)
.:? UserId
"user_id"
        Maybe UserId
pfid  <- Object
o Object -> UserId -> Parser (Maybe UserId)
forall a. FromJSON a => Object -> UserId -> Parser (Maybe a)
.:? UserId
"profile_id"
        Received -> Parser Received
forall (m :: * -> *) a. Monad m => a -> m a
return (Received -> Parser Received) -> Received -> Parser Received
forall a b. (a -> b) -> a -> b
$ UTCTime
-> ProductId
-> Sequence
-> OrderId
-> Maybe ClientOrderId
-> Maybe Size
-> Maybe Price
-> Maybe Funds
-> Side
-> OrderType
-> Maybe UserId
-> Maybe UserId
-> Received
Received UTCTime
t ProductId
prd Sequence
sq OrderId
oid Maybe ClientOrderId
cloid Maybe Size
sz Maybe Price
px Maybe Funds
fs Side
sd OrderType
ot Maybe UserId
ui Maybe UserId
pfid