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

data TonConnectSession
  = TonConnectSession -- ^ Represents a TON Connect session. See https://github.com/ton-blockchain/ton-connect/tree/main/spec for full TON Connect protocol documentation
    { TonConnectSession -> Maybe Int
_id              :: Maybe Int                                           -- ^ Unique identifier of the session
    , TonConnectSession -> Maybe Text
dapp_client_id   :: Maybe T.Text                                        -- ^ Hex-encoded public X25519 key of the dApp
    , TonConnectSession -> Maybe Text
wallet_client_id :: Maybe T.Text                                        -- ^ Hex-encoded public X25519 key of the current user's application; may be empty if none
    , TonConnectSession -> Maybe Int
creation_date    :: Maybe Int                                           -- ^ Point in time (Unix timestamp) when the session was created
    , TonConnectSession -> Maybe TonConnectManifest
manifest         :: Maybe TonConnectManifest.TonConnectManifest         -- ^ Manifest of the dApp
    , TonConnectSession -> Maybe ByteString
nonce            :: Maybe BS.ByteString                                 -- ^ Nonce used for the session
    , TonConnectSession -> Maybe TonConnectSessionState
state            :: Maybe TonConnectSessionState.TonConnectSessionState -- ^ Session state
    }
  deriving (TonConnectSession -> TonConnectSession -> Bool
(TonConnectSession -> TonConnectSession -> Bool)
-> (TonConnectSession -> TonConnectSession -> Bool)
-> Eq TonConnectSession
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TonConnectSession -> TonConnectSession -> Bool
== :: TonConnectSession -> TonConnectSession -> Bool
$c/= :: TonConnectSession -> TonConnectSession -> Bool
/= :: TonConnectSession -> TonConnectSession -> Bool
Eq, Int -> TonConnectSession -> ShowS
[TonConnectSession] -> ShowS
TonConnectSession -> String
(Int -> TonConnectSession -> ShowS)
-> (TonConnectSession -> String)
-> ([TonConnectSession] -> ShowS)
-> Show TonConnectSession
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TonConnectSession -> ShowS
showsPrec :: Int -> TonConnectSession -> ShowS
$cshow :: TonConnectSession -> String
show :: TonConnectSession -> String
$cshowList :: [TonConnectSession] -> ShowS
showList :: [TonConnectSession] -> ShowS
Show)

instance I.ShortShow TonConnectSession where
  shortShow :: TonConnectSession -> String
shortShow TonConnectSession
    { _id :: TonConnectSession -> Maybe Int
_id              = Maybe Int
_id_
    , dapp_client_id :: TonConnectSession -> Maybe Text
dapp_client_id   = Maybe Text
dapp_client_id_
    , wallet_client_id :: TonConnectSession -> Maybe Text
wallet_client_id = Maybe Text
wallet_client_id_
    , creation_date :: TonConnectSession -> Maybe Int
creation_date    = Maybe Int
creation_date_
    , manifest :: TonConnectSession -> Maybe TonConnectManifest
manifest         = Maybe TonConnectManifest
manifest_
    , nonce :: TonConnectSession -> Maybe ByteString
nonce            = Maybe ByteString
nonce_
    , state :: TonConnectSession -> Maybe TonConnectSessionState
state            = Maybe TonConnectSessionState
state_
    }
      = String
"TonConnectSession"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"              String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_id_
        , String
"dapp_client_id"   String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
dapp_client_id_
        , String
"wallet_client_id" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
wallet_client_id_
        , String
"creation_date"    String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
creation_date_
        , String
"manifest"         String -> Maybe TonConnectManifest -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe TonConnectManifest
manifest_
        , String
"nonce"            String -> Maybe ByteString -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ByteString
nonce_
        , String
"state"            String -> Maybe TonConnectSessionState -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe TonConnectSessionState
state_
        ]

instance AT.FromJSON TonConnectSession where
  parseJSON :: Value -> Parser TonConnectSession
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
"tonConnectSession" -> Value -> Parser TonConnectSession
parseTonConnectSession Value
v
      String
_                   -> Parser TonConnectSession
forall a. Monoid a => a
mempty
    
    where
      parseTonConnectSession :: A.Value -> AT.Parser TonConnectSession
      parseTonConnectSession :: Value -> Parser TonConnectSession
parseTonConnectSession = String
-> (Object -> Parser TonConnectSession)
-> Value
-> Parser TonConnectSession
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TonConnectSession" ((Object -> Parser TonConnectSession)
 -> Value -> Parser TonConnectSession)
-> (Object -> Parser TonConnectSession)
-> Value
-> Parser TonConnectSession
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_              <- (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
"id"
        Maybe Text
dapp_client_id_   <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"dapp_client_id"
        Maybe Text
wallet_client_id_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"wallet_client_id"
        Maybe Int
creation_date_    <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"creation_date"
        Maybe TonConnectManifest
manifest_         <- Object
o Object -> Key -> Parser (Maybe TonConnectManifest)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"manifest"
        Maybe ByteString
nonce_            <- (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
"nonce"
        Maybe TonConnectSessionState
state_            <- Object
o Object -> Key -> Parser (Maybe TonConnectSessionState)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"state"
        TonConnectSession -> Parser TonConnectSession
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TonConnectSession -> Parser TonConnectSession)
-> TonConnectSession -> Parser TonConnectSession
forall a b. (a -> b) -> a -> b
$ TonConnectSession
          { _id :: Maybe Int
_id              = Maybe Int
_id_
          , dapp_client_id :: Maybe Text
dapp_client_id   = Maybe Text
dapp_client_id_
          , wallet_client_id :: Maybe Text
wallet_client_id = Maybe Text
wallet_client_id_
          , creation_date :: Maybe Int
creation_date    = Maybe Int
creation_date_
          , manifest :: Maybe TonConnectManifest
manifest         = Maybe TonConnectManifest
manifest_
          , nonce :: Maybe ByteString
nonce            = Maybe ByteString
nonce_
          , state :: Maybe TonConnectSessionState
state            = Maybe TonConnectSessionState
state_
          }
  parseJSON Value
_ = Parser TonConnectSession
forall a. Monoid a => a
mempty