module TD.Data.CallId
  (CallId(..)) where

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

data CallId
  = CallId -- ^ Contains the call identifier
    { CallId -> Maybe Int
_id :: Maybe Int -- ^ Call identifier
    }
  deriving (CallId -> CallId -> Bool
(CallId -> CallId -> Bool)
-> (CallId -> CallId -> Bool) -> Eq CallId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CallId -> CallId -> Bool
== :: CallId -> CallId -> Bool
$c/= :: CallId -> CallId -> Bool
/= :: CallId -> CallId -> Bool
Eq, Int -> CallId -> ShowS
[CallId] -> ShowS
CallId -> String
(Int -> CallId -> ShowS)
-> (CallId -> String) -> ([CallId] -> ShowS) -> Show CallId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CallId -> ShowS
showsPrec :: Int -> CallId -> ShowS
$cshow :: CallId -> String
show :: CallId -> String
$cshowList :: [CallId] -> ShowS
showList :: [CallId] -> ShowS
Show)

instance I.ShortShow CallId where
  shortShow :: CallId -> String
shortShow CallId
    { _id :: CallId -> Maybe Int
_id = Maybe Int
_id_
    }
      = String
"CallId"
        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_
        ]

instance AT.FromJSON CallId where
  parseJSON :: Value -> Parser CallId
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
"callId" -> Value -> Parser CallId
parseCallId Value
v
      String
_        -> Parser CallId
forall a. Monoid a => a
mempty
    
    where
      parseCallId :: A.Value -> AT.Parser CallId
      parseCallId :: Value -> Parser CallId
parseCallId = String -> (Object -> Parser CallId) -> Value -> Parser CallId
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"CallId" ((Object -> Parser CallId) -> Value -> Parser CallId)
-> (Object -> Parser CallId) -> Value -> Parser CallId
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"
        CallId -> Parser CallId
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CallId -> Parser CallId) -> CallId -> Parser CallId
forall a b. (a -> b) -> a -> b
$ CallId
          { _id :: Maybe Int
_id = Maybe Int
_id_
          }
  parseJSON Value
_ = Parser CallId
forall a. Monoid a => a
mempty