module TD.Data.Call
  (Call(..)) 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.CallState as CallState

data Call
  = Call -- ^ Describes a call
    { Call -> Maybe Int
_id         :: Maybe Int                 -- ^ Call identifier, not persistent
    , Call -> Maybe Int
user_id     :: Maybe Int                 -- ^ User identifier of the other call participant
    , Call -> Maybe Bool
is_outgoing :: Maybe Bool                -- ^ True, if the call is outgoing
    , Call -> Maybe Bool
is_video    :: Maybe Bool                -- ^ True, if the call is a video call
    , Call -> Maybe CallState
state       :: Maybe CallState.CallState -- ^ Call state
    }
  deriving (Call -> Call -> Bool
(Call -> Call -> Bool) -> (Call -> Call -> Bool) -> Eq Call
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Call -> Call -> Bool
== :: Call -> Call -> Bool
$c/= :: Call -> Call -> Bool
/= :: Call -> Call -> Bool
Eq, Int -> Call -> ShowS
[Call] -> ShowS
Call -> String
(Int -> Call -> ShowS)
-> (Call -> String) -> ([Call] -> ShowS) -> Show Call
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Call -> ShowS
showsPrec :: Int -> Call -> ShowS
$cshow :: Call -> String
show :: Call -> String
$cshowList :: [Call] -> ShowS
showList :: [Call] -> ShowS
Show)

instance I.ShortShow Call where
  shortShow :: Call -> String
shortShow Call
    { _id :: Call -> Maybe Int
_id         = Maybe Int
_id_
    , user_id :: Call -> Maybe Int
user_id     = Maybe Int
user_id_
    , is_outgoing :: Call -> Maybe Bool
is_outgoing = Maybe Bool
is_outgoing_
    , is_video :: Call -> Maybe Bool
is_video    = Maybe Bool
is_video_
    , state :: Call -> Maybe CallState
state       = Maybe CallState
state_
    }
      = String
"Call"
        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
"is_outgoing" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_outgoing_
        , String
"is_video"    String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_video_
        , String
"state"       String -> Maybe CallState -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe CallState
state_
        ]

instance AT.FromJSON Call where
  parseJSON :: Value -> Parser Call
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
"call" -> Value -> Parser Call
parseCall Value
v
      String
_      -> Parser Call
forall a. Monoid a => a
mempty
    
    where
      parseCall :: A.Value -> AT.Parser Call
      parseCall :: Value -> Parser Call
parseCall = String -> (Object -> Parser Call) -> Value -> Parser Call
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Call" ((Object -> Parser Call) -> Value -> Parser Call)
-> (Object -> Parser Call) -> Value -> Parser Call
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 Bool
is_outgoing_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_outgoing"
        Maybe Bool
is_video_    <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_video"
        Maybe CallState
state_       <- Object
o Object -> Key -> Parser (Maybe CallState)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"state"
        Call -> Parser Call
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Call -> Parser Call) -> Call -> Parser Call
forall a b. (a -> b) -> a -> b
$ Call
          { _id :: Maybe Int
_id         = Maybe Int
_id_
          , user_id :: Maybe Int
user_id     = Maybe Int
user_id_
          , is_outgoing :: Maybe Bool
is_outgoing = Maybe Bool
is_outgoing_
          , is_video :: Maybe Bool
is_video    = Maybe Bool
is_video_
          , state :: Maybe CallState
state       = Maybe CallState
state_
          }
  parseJSON Value
_ = Parser Call
forall a. Monoid a => a
mempty