module TD.Data.CollectibleItemInfo
  (CollectibleItemInfo(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.Text as T

data CollectibleItemInfo
  = CollectibleItemInfo -- ^ Contains information about a collectible item and its last purchase
    { CollectibleItemInfo -> Maybe Int
purchase_date         :: Maybe Int    -- ^ Point in time (Unix timestamp) when the item was purchased
    , CollectibleItemInfo -> Maybe Text
currency              :: Maybe T.Text -- ^ Currency for the paid amount
    , CollectibleItemInfo -> Maybe Int
amount                :: Maybe Int    -- ^ The paid amount, in the smallest units of the currency
    , CollectibleItemInfo -> Maybe Text
cryptocurrency        :: Maybe T.Text -- ^ Cryptocurrency used to pay for the item
    , CollectibleItemInfo -> Maybe Int
cryptocurrency_amount :: Maybe Int    -- ^ The paid amount, in the smallest units of the cryptocurrency
    , CollectibleItemInfo -> Maybe Text
url                   :: Maybe T.Text -- ^ Individual URL for the item on https://fragment.com
    }
  deriving (CollectibleItemInfo -> CollectibleItemInfo -> Bool
(CollectibleItemInfo -> CollectibleItemInfo -> Bool)
-> (CollectibleItemInfo -> CollectibleItemInfo -> Bool)
-> Eq CollectibleItemInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CollectibleItemInfo -> CollectibleItemInfo -> Bool
== :: CollectibleItemInfo -> CollectibleItemInfo -> Bool
$c/= :: CollectibleItemInfo -> CollectibleItemInfo -> Bool
/= :: CollectibleItemInfo -> CollectibleItemInfo -> Bool
Eq, Int -> CollectibleItemInfo -> ShowS
[CollectibleItemInfo] -> ShowS
CollectibleItemInfo -> String
(Int -> CollectibleItemInfo -> ShowS)
-> (CollectibleItemInfo -> String)
-> ([CollectibleItemInfo] -> ShowS)
-> Show CollectibleItemInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CollectibleItemInfo -> ShowS
showsPrec :: Int -> CollectibleItemInfo -> ShowS
$cshow :: CollectibleItemInfo -> String
show :: CollectibleItemInfo -> String
$cshowList :: [CollectibleItemInfo] -> ShowS
showList :: [CollectibleItemInfo] -> ShowS
Show)

instance I.ShortShow CollectibleItemInfo where
  shortShow :: CollectibleItemInfo -> String
shortShow CollectibleItemInfo
    { purchase_date :: CollectibleItemInfo -> Maybe Int
purchase_date         = Maybe Int
purchase_date_
    , currency :: CollectibleItemInfo -> Maybe Text
currency              = Maybe Text
currency_
    , amount :: CollectibleItemInfo -> Maybe Int
amount                = Maybe Int
amount_
    , cryptocurrency :: CollectibleItemInfo -> Maybe Text
cryptocurrency        = Maybe Text
cryptocurrency_
    , cryptocurrency_amount :: CollectibleItemInfo -> Maybe Int
cryptocurrency_amount = Maybe Int
cryptocurrency_amount_
    , url :: CollectibleItemInfo -> Maybe Text
url                   = Maybe Text
url_
    }
      = String
"CollectibleItemInfo"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"purchase_date"         String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
purchase_date_
        , String
"currency"              String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
currency_
        , String
"amount"                String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
amount_
        , String
"cryptocurrency"        String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
cryptocurrency_
        , String
"cryptocurrency_amount" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
cryptocurrency_amount_
        , String
"url"                   String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
url_
        ]

instance AT.FromJSON CollectibleItemInfo where
  parseJSON :: Value -> Parser CollectibleItemInfo
parseJSON v :: Value
v@(AT.Object Object
obj) = do
    String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String

    case String
t of
      String
"collectibleItemInfo" -> Value -> Parser CollectibleItemInfo
parseCollectibleItemInfo Value
v
      String
_                     -> Parser CollectibleItemInfo
forall a. Monoid a => a
mempty
    
    where
      parseCollectibleItemInfo :: A.Value -> AT.Parser CollectibleItemInfo
      parseCollectibleItemInfo :: Value -> Parser CollectibleItemInfo
parseCollectibleItemInfo = String
-> (Object -> Parser CollectibleItemInfo)
-> Value
-> Parser CollectibleItemInfo
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"CollectibleItemInfo" ((Object -> Parser CollectibleItemInfo)
 -> Value -> Parser CollectibleItemInfo)
-> (Object -> Parser CollectibleItemInfo)
-> Value
-> Parser CollectibleItemInfo
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
purchase_date_         <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"purchase_date"
        Maybe Text
currency_              <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"currency"
        Maybe Int
amount_                <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"amount"
        Maybe Text
cryptocurrency_        <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"cryptocurrency"
        Maybe Int
cryptocurrency_amount_ <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"cryptocurrency_amount"
        Maybe Text
url_                   <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"url"
        CollectibleItemInfo -> Parser CollectibleItemInfo
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CollectibleItemInfo -> Parser CollectibleItemInfo)
-> CollectibleItemInfo -> Parser CollectibleItemInfo
forall a b. (a -> b) -> a -> b
$ CollectibleItemInfo
          { purchase_date :: Maybe Int
purchase_date         = Maybe Int
purchase_date_
          , currency :: Maybe Text
currency              = Maybe Text
currency_
          , amount :: Maybe Int
amount                = Maybe Int
amount_
          , cryptocurrency :: Maybe Text
cryptocurrency        = Maybe Text
cryptocurrency_
          , cryptocurrency_amount :: Maybe Int
cryptocurrency_amount = Maybe Int
cryptocurrency_amount_
          , url :: Maybe Text
url                   = Maybe Text
url_
          }
  parseJSON Value
_ = Parser CollectibleItemInfo
forall a. Monoid a => a
mempty