module TD.Data.CallProtocol
  ( CallProtocol(..)    
  , defaultCallProtocol 
  ) 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

data CallProtocol
  = CallProtocol -- ^ Specifies the supported call protocols
    { CallProtocol -> Maybe Bool
udp_p2p          :: Maybe Bool     -- ^ True, if UDP peer-to-peer connections are supported
    , CallProtocol -> Maybe Bool
udp_reflector    :: Maybe Bool     -- ^ True, if connection through UDP reflectors is supported
    , CallProtocol -> Maybe Int
min_layer        :: Maybe Int      -- ^ The minimum supported API layer; use 65
    , CallProtocol -> Maybe Int
max_layer        :: Maybe Int      -- ^ The maximum supported API layer; use 92
    , CallProtocol -> Maybe [Text]
library_versions :: Maybe [T.Text] -- ^ List of supported tgcalls versions
    }
  deriving (CallProtocol -> CallProtocol -> Bool
(CallProtocol -> CallProtocol -> Bool)
-> (CallProtocol -> CallProtocol -> Bool) -> Eq CallProtocol
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CallProtocol -> CallProtocol -> Bool
== :: CallProtocol -> CallProtocol -> Bool
$c/= :: CallProtocol -> CallProtocol -> Bool
/= :: CallProtocol -> CallProtocol -> Bool
Eq, Int -> CallProtocol -> ShowS
[CallProtocol] -> ShowS
CallProtocol -> String
(Int -> CallProtocol -> ShowS)
-> (CallProtocol -> String)
-> ([CallProtocol] -> ShowS)
-> Show CallProtocol
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CallProtocol -> ShowS
showsPrec :: Int -> CallProtocol -> ShowS
$cshow :: CallProtocol -> String
show :: CallProtocol -> String
$cshowList :: [CallProtocol] -> ShowS
showList :: [CallProtocol] -> ShowS
Show)

instance I.ShortShow CallProtocol where
  shortShow :: CallProtocol -> String
shortShow CallProtocol
    { udp_p2p :: CallProtocol -> Maybe Bool
udp_p2p          = Maybe Bool
udp_p2p_
    , udp_reflector :: CallProtocol -> Maybe Bool
udp_reflector    = Maybe Bool
udp_reflector_
    , min_layer :: CallProtocol -> Maybe Int
min_layer        = Maybe Int
min_layer_
    , max_layer :: CallProtocol -> Maybe Int
max_layer        = Maybe Int
max_layer_
    , library_versions :: CallProtocol -> Maybe [Text]
library_versions = Maybe [Text]
library_versions_
    }
      = String
"CallProtocol"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"udp_p2p"          String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
udp_p2p_
        , String
"udp_reflector"    String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
udp_reflector_
        , String
"min_layer"        String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
min_layer_
        , String
"max_layer"        String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
max_layer_
        , String
"library_versions" String -> Maybe [Text] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Text]
library_versions_
        ]

instance AT.FromJSON CallProtocol where
  parseJSON :: Value -> Parser CallProtocol
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
"callProtocol" -> Value -> Parser CallProtocol
parseCallProtocol Value
v
      String
_              -> Parser CallProtocol
forall a. Monoid a => a
mempty
    
    where
      parseCallProtocol :: A.Value -> AT.Parser CallProtocol
      parseCallProtocol :: Value -> Parser CallProtocol
parseCallProtocol = String
-> (Object -> Parser CallProtocol) -> Value -> Parser CallProtocol
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"CallProtocol" ((Object -> Parser CallProtocol) -> Value -> Parser CallProtocol)
-> (Object -> Parser CallProtocol) -> Value -> Parser CallProtocol
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
udp_p2p_          <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"udp_p2p"
        Maybe Bool
udp_reflector_    <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"udp_reflector"
        Maybe Int
min_layer_        <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"min_layer"
        Maybe Int
max_layer_        <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"max_layer"
        Maybe [Text]
library_versions_ <- Object
o Object -> Key -> Parser (Maybe [Text])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"library_versions"
        CallProtocol -> Parser CallProtocol
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CallProtocol -> Parser CallProtocol)
-> CallProtocol -> Parser CallProtocol
forall a b. (a -> b) -> a -> b
$ CallProtocol
          { udp_p2p :: Maybe Bool
udp_p2p          = Maybe Bool
udp_p2p_
          , udp_reflector :: Maybe Bool
udp_reflector    = Maybe Bool
udp_reflector_
          , min_layer :: Maybe Int
min_layer        = Maybe Int
min_layer_
          , max_layer :: Maybe Int
max_layer        = Maybe Int
max_layer_
          , library_versions :: Maybe [Text]
library_versions = Maybe [Text]
library_versions_
          }
  parseJSON Value
_ = Parser CallProtocol
forall a. Monoid a => a
mempty

instance AT.ToJSON CallProtocol where
  toJSON :: CallProtocol -> Value
toJSON CallProtocol
    { udp_p2p :: CallProtocol -> Maybe Bool
udp_p2p          = Maybe Bool
udp_p2p_
    , udp_reflector :: CallProtocol -> Maybe Bool
udp_reflector    = Maybe Bool
udp_reflector_
    , min_layer :: CallProtocol -> Maybe Int
min_layer        = Maybe Int
min_layer_
    , max_layer :: CallProtocol -> Maybe Int
max_layer        = Maybe Int
max_layer_
    , library_versions :: CallProtocol -> Maybe [Text]
library_versions = Maybe [Text]
library_versions_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"            Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"callProtocol"
        , Key
"udp_p2p"          Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
udp_p2p_
        , Key
"udp_reflector"    Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
udp_reflector_
        , Key
"min_layer"        Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
min_layer_
        , Key
"max_layer"        Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
max_layer_
        , Key
"library_versions" Key -> Maybe [Text] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe [Text]
library_versions_
        ]

defaultCallProtocol :: CallProtocol
defaultCallProtocol :: CallProtocol
defaultCallProtocol =
  CallProtocol
    { udp_p2p :: Maybe Bool
udp_p2p          = Maybe Bool
forall a. Maybe a
Nothing
    , udp_reflector :: Maybe Bool
udp_reflector    = Maybe Bool
forall a. Maybe a
Nothing
    , min_layer :: Maybe Int
min_layer        = Maybe Int
forall a. Maybe a
Nothing
    , max_layer :: Maybe Int
max_layer        = Maybe Int
forall a. Maybe a
Nothing
    , library_versions :: Maybe [Text]
library_versions = Maybe [Text]
forall a. Maybe a
Nothing
    }