module TD.Data.UserTonWalletAddress
  (UserTonWalletAddress(..)) 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
import qualified Data.ByteString as BS

data UserTonWalletAddress
  = UserTonWalletAddress -- ^ Contains TON wallet address of a user
    { UserTonWalletAddress -> Maybe Int
user_id        :: Maybe Int           -- ^ Identifier of the user; 0 if unknown
    , UserTonWalletAddress -> Maybe Text
wallet_address :: Maybe T.Text        -- ^ Address of the user's wallet in the TON blockchain
    , UserTonWalletAddress -> Maybe ByteString
public_key     :: Maybe BS.ByteString -- ^ Public key of the user
    }
  deriving (UserTonWalletAddress -> UserTonWalletAddress -> Bool
(UserTonWalletAddress -> UserTonWalletAddress -> Bool)
-> (UserTonWalletAddress -> UserTonWalletAddress -> Bool)
-> Eq UserTonWalletAddress
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UserTonWalletAddress -> UserTonWalletAddress -> Bool
== :: UserTonWalletAddress -> UserTonWalletAddress -> Bool
$c/= :: UserTonWalletAddress -> UserTonWalletAddress -> Bool
/= :: UserTonWalletAddress -> UserTonWalletAddress -> Bool
Eq, Int -> UserTonWalletAddress -> ShowS
[UserTonWalletAddress] -> ShowS
UserTonWalletAddress -> String
(Int -> UserTonWalletAddress -> ShowS)
-> (UserTonWalletAddress -> String)
-> ([UserTonWalletAddress] -> ShowS)
-> Show UserTonWalletAddress
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UserTonWalletAddress -> ShowS
showsPrec :: Int -> UserTonWalletAddress -> ShowS
$cshow :: UserTonWalletAddress -> String
show :: UserTonWalletAddress -> String
$cshowList :: [UserTonWalletAddress] -> ShowS
showList :: [UserTonWalletAddress] -> ShowS
Show)

instance I.ShortShow UserTonWalletAddress where
  shortShow :: UserTonWalletAddress -> String
shortShow UserTonWalletAddress
    { user_id :: UserTonWalletAddress -> Maybe Int
user_id        = Maybe Int
user_id_
    , wallet_address :: UserTonWalletAddress -> Maybe Text
wallet_address = Maybe Text
wallet_address_
    , public_key :: UserTonWalletAddress -> Maybe ByteString
public_key     = Maybe ByteString
public_key_
    }
      = String
"UserTonWalletAddress"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"user_id"        String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
user_id_
        , String
"wallet_address" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
wallet_address_
        , String
"public_key"     String -> Maybe ByteString -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ByteString
public_key_
        ]

instance AT.FromJSON UserTonWalletAddress where
  parseJSON :: Value -> Parser UserTonWalletAddress
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
"userTonWalletAddress" -> Value -> Parser UserTonWalletAddress
parseUserTonWalletAddress Value
v
      String
_                      -> Parser UserTonWalletAddress
forall a. Monoid a => a
mempty
    
    where
      parseUserTonWalletAddress :: A.Value -> AT.Parser UserTonWalletAddress
      parseUserTonWalletAddress :: Value -> Parser UserTonWalletAddress
parseUserTonWalletAddress = String
-> (Object -> Parser UserTonWalletAddress)
-> Value
-> Parser UserTonWalletAddress
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"UserTonWalletAddress" ((Object -> Parser UserTonWalletAddress)
 -> Value -> Parser UserTonWalletAddress)
-> (Object -> Parser UserTonWalletAddress)
-> Value
-> Parser UserTonWalletAddress
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
user_id_        <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"user_id"
        Maybe Text
wallet_address_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"wallet_address"
        Maybe ByteString
public_key_     <- (String -> ByteString) -> Maybe String -> Maybe ByteString
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> ByteString
I.readBytes (Maybe String -> Maybe ByteString)
-> Parser (Maybe String) -> Parser (Maybe ByteString)
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
"public_key"
        UserTonWalletAddress -> Parser UserTonWalletAddress
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (UserTonWalletAddress -> Parser UserTonWalletAddress)
-> UserTonWalletAddress -> Parser UserTonWalletAddress
forall a b. (a -> b) -> a -> b
$ UserTonWalletAddress
          { user_id :: Maybe Int
user_id        = Maybe Int
user_id_
          , wallet_address :: Maybe Text
wallet_address = Maybe Text
wallet_address_
          , public_key :: Maybe ByteString
public_key     = Maybe ByteString
public_key_
          }
  parseJSON Value
_ = Parser UserTonWalletAddress
forall a. Monoid a => a
mempty