module TD.Data.CallDiscardReason
  (CallDiscardReason(..)) where

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

-- | Describes the reason why a call was discarded
data CallDiscardReason
  = CallDiscardReasonEmpty -- ^ The call wasn't discarded, or the reason is unknown
  | CallDiscardReasonMissed -- ^ The call was ended before the conversation started. It was canceled by the caller or missed by the other party
  | CallDiscardReasonDeclined -- ^ The call was ended before the conversation started. It was declined by the other party
  | CallDiscardReasonDisconnected -- ^ The call was ended during the conversation because the users were disconnected
  | CallDiscardReasonHungUp -- ^ The call was ended because one of the parties hung up
  | CallDiscardReasonAllowGroupCall -- ^ The call was ended because it has been used successfully to transfer private encryption key for the associated group call
    { CallDiscardReason -> Maybe ByteString
encrypted_group_call_key :: Maybe BS.ByteString -- ^ Encrypted using the call private key encryption key for the associated group call
    }
  deriving (CallDiscardReason -> CallDiscardReason -> Bool
(CallDiscardReason -> CallDiscardReason -> Bool)
-> (CallDiscardReason -> CallDiscardReason -> Bool)
-> Eq CallDiscardReason
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CallDiscardReason -> CallDiscardReason -> Bool
== :: CallDiscardReason -> CallDiscardReason -> Bool
$c/= :: CallDiscardReason -> CallDiscardReason -> Bool
/= :: CallDiscardReason -> CallDiscardReason -> Bool
Eq, Int -> CallDiscardReason -> ShowS
[CallDiscardReason] -> ShowS
CallDiscardReason -> String
(Int -> CallDiscardReason -> ShowS)
-> (CallDiscardReason -> String)
-> ([CallDiscardReason] -> ShowS)
-> Show CallDiscardReason
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CallDiscardReason -> ShowS
showsPrec :: Int -> CallDiscardReason -> ShowS
$cshow :: CallDiscardReason -> String
show :: CallDiscardReason -> String
$cshowList :: [CallDiscardReason] -> ShowS
showList :: [CallDiscardReason] -> ShowS
Show)

instance I.ShortShow CallDiscardReason where
  shortShow :: CallDiscardReason -> String
shortShow CallDiscardReason
CallDiscardReasonEmpty
      = String
"CallDiscardReasonEmpty"
  shortShow CallDiscardReason
CallDiscardReasonMissed
      = String
"CallDiscardReasonMissed"
  shortShow CallDiscardReason
CallDiscardReasonDeclined
      = String
"CallDiscardReasonDeclined"
  shortShow CallDiscardReason
CallDiscardReasonDisconnected
      = String
"CallDiscardReasonDisconnected"
  shortShow CallDiscardReason
CallDiscardReasonHungUp
      = String
"CallDiscardReasonHungUp"
  shortShow CallDiscardReasonAllowGroupCall
    { encrypted_group_call_key :: CallDiscardReason -> Maybe ByteString
encrypted_group_call_key = Maybe ByteString
encrypted_group_call_key_
    }
      = String
"CallDiscardReasonAllowGroupCall"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"encrypted_group_call_key" String -> Maybe ByteString -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ByteString
encrypted_group_call_key_
        ]

instance AT.FromJSON CallDiscardReason where
  parseJSON :: Value -> Parser CallDiscardReason
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
"callDiscardReasonEmpty"          -> CallDiscardReason -> Parser CallDiscardReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CallDiscardReason
CallDiscardReasonEmpty
      String
"callDiscardReasonMissed"         -> CallDiscardReason -> Parser CallDiscardReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CallDiscardReason
CallDiscardReasonMissed
      String
"callDiscardReasonDeclined"       -> CallDiscardReason -> Parser CallDiscardReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CallDiscardReason
CallDiscardReasonDeclined
      String
"callDiscardReasonDisconnected"   -> CallDiscardReason -> Parser CallDiscardReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CallDiscardReason
CallDiscardReasonDisconnected
      String
"callDiscardReasonHungUp"         -> CallDiscardReason -> Parser CallDiscardReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CallDiscardReason
CallDiscardReasonHungUp
      String
"callDiscardReasonAllowGroupCall" -> Value -> Parser CallDiscardReason
parseCallDiscardReasonAllowGroupCall Value
v
      String
_                                 -> Parser CallDiscardReason
forall a. Monoid a => a
mempty
    
    where
      parseCallDiscardReasonAllowGroupCall :: A.Value -> AT.Parser CallDiscardReason
      parseCallDiscardReasonAllowGroupCall :: Value -> Parser CallDiscardReason
parseCallDiscardReasonAllowGroupCall = String
-> (Object -> Parser CallDiscardReason)
-> Value
-> Parser CallDiscardReason
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"CallDiscardReasonAllowGroupCall" ((Object -> Parser CallDiscardReason)
 -> Value -> Parser CallDiscardReason)
-> (Object -> Parser CallDiscardReason)
-> Value
-> Parser CallDiscardReason
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe ByteString
encrypted_group_call_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
"encrypted_group_call_key"
        CallDiscardReason -> Parser CallDiscardReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CallDiscardReason -> Parser CallDiscardReason)
-> CallDiscardReason -> Parser CallDiscardReason
forall a b. (a -> b) -> a -> b
$ CallDiscardReasonAllowGroupCall
          { encrypted_group_call_key :: Maybe ByteString
encrypted_group_call_key = Maybe ByteString
encrypted_group_call_key_
          }
  parseJSON Value
_ = Parser CallDiscardReason
forall a. Monoid a => a
mempty