module TD.Data.SecretChat
  (SecretChat(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified TD.Data.SecretChatState as SecretChatState
import qualified Data.ByteString as BS

data SecretChat
  = SecretChat -- ^ Represents a secret chat
    { SecretChat -> Maybe Int
_id         :: Maybe Int                             -- ^ Secret chat identifier
    , SecretChat -> Maybe Int
user_id     :: Maybe Int                             -- ^ Identifier of the chat partner
    , SecretChat -> Maybe SecretChatState
state       :: Maybe SecretChatState.SecretChatState -- ^ State of the secret chat
    , SecretChat -> Maybe Bool
is_outbound :: Maybe Bool                            -- ^ True, if the chat was created by the current user; false otherwise
    , SecretChat -> Maybe ByteString
key_hash    :: Maybe BS.ByteString                   -- ^ Hash of the currently used key for comparison with the hash of the chat partner's key. This is a string of 36 little-endian bytes, which must be split into groups of 2 bits, each denoting a pixel of one of 4 colors FFFFFF, D5E6F3, 2D5775, and 2F99C9. The pixels must be used to make a 12x12 square image filled from left to right, top to bottom. Alternatively, the first 32 bytes of the hash can be converted to the hexadecimal format and printed as 32 2-digit hex numbers
    , SecretChat -> Maybe Int
layer       :: Maybe Int                             -- ^ Secret chat layer; determines features supported by the chat partner's application. Nested text entities and underline and strikethrough entities are supported if the layer >= 101, files bigger than 2000MB are supported if the layer >= 143, spoiler and custom emoji text entities are supported if the layer >= 144
    }
  deriving (SecretChat -> SecretChat -> Bool
(SecretChat -> SecretChat -> Bool)
-> (SecretChat -> SecretChat -> Bool) -> Eq SecretChat
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SecretChat -> SecretChat -> Bool
== :: SecretChat -> SecretChat -> Bool
$c/= :: SecretChat -> SecretChat -> Bool
/= :: SecretChat -> SecretChat -> Bool
Eq, Int -> SecretChat -> ShowS
[SecretChat] -> ShowS
SecretChat -> String
(Int -> SecretChat -> ShowS)
-> (SecretChat -> String)
-> ([SecretChat] -> ShowS)
-> Show SecretChat
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SecretChat -> ShowS
showsPrec :: Int -> SecretChat -> ShowS
$cshow :: SecretChat -> String
show :: SecretChat -> String
$cshowList :: [SecretChat] -> ShowS
showList :: [SecretChat] -> ShowS
Show)

instance I.ShortShow SecretChat where
  shortShow :: SecretChat -> String
shortShow SecretChat
    { _id :: SecretChat -> Maybe Int
_id         = Maybe Int
_id_
    , user_id :: SecretChat -> Maybe Int
user_id     = Maybe Int
user_id_
    , state :: SecretChat -> Maybe SecretChatState
state       = Maybe SecretChatState
state_
    , is_outbound :: SecretChat -> Maybe Bool
is_outbound = Maybe Bool
is_outbound_
    , key_hash :: SecretChat -> Maybe ByteString
key_hash    = Maybe ByteString
key_hash_
    , layer :: SecretChat -> Maybe Int
layer       = Maybe Int
layer_
    }
      = String
"SecretChat"
        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
"user_id"     String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
user_id_
        , String
"state"       String -> Maybe SecretChatState -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe SecretChatState
state_
        , String
"is_outbound" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_outbound_
        , String
"key_hash"    String -> Maybe ByteString -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ByteString
key_hash_
        , String
"layer"       String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
layer_
        ]

instance AT.FromJSON SecretChat where
  parseJSON :: Value -> Parser SecretChat
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
"secretChat" -> Value -> Parser SecretChat
parseSecretChat Value
v
      String
_            -> Parser SecretChat
forall a. Monoid a => a
mempty
    
    where
      parseSecretChat :: A.Value -> AT.Parser SecretChat
      parseSecretChat :: Value -> Parser SecretChat
parseSecretChat = String
-> (Object -> Parser SecretChat) -> Value -> Parser SecretChat
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"SecretChat" ((Object -> Parser SecretChat) -> Value -> Parser SecretChat)
-> (Object -> Parser SecretChat) -> Value -> Parser SecretChat
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_         <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"id"
        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 SecretChatState
state_       <- Object
o Object -> Key -> Parser (Maybe SecretChatState)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"state"
        Maybe Bool
is_outbound_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"is_outbound"
        Maybe ByteString
key_hash_    <- (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
"key_hash"
        Maybe Int
layer_       <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"layer"
        SecretChat -> Parser SecretChat
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SecretChat -> Parser SecretChat)
-> SecretChat -> Parser SecretChat
forall a b. (a -> b) -> a -> b
$ SecretChat
          { _id :: Maybe Int
_id         = Maybe Int
_id_
          , user_id :: Maybe Int
user_id     = Maybe Int
user_id_
          , state :: Maybe SecretChatState
state       = Maybe SecretChatState
state_
          , is_outbound :: Maybe Bool
is_outbound = Maybe Bool
is_outbound_
          , key_hash :: Maybe ByteString
key_hash    = Maybe ByteString
key_hash_
          , layer :: Maybe Int
layer       = Maybe Int
layer_
          }
  parseJSON Value
_ = Parser SecretChat
forall a. Monoid a => a
mempty