module TD.Data.CallServerType
  (CallServerType(..)) 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
import qualified Data.Text as T

-- | Describes the type of call server
data CallServerType
  = CallServerTypeTelegramReflector -- ^ A Telegram call reflector
    { CallServerType -> Maybe ByteString
peer_tag :: Maybe BS.ByteString -- ^ A peer tag to be used with the reflector
    , CallServerType -> Maybe Bool
is_tcp   :: Maybe Bool          -- ^ True, if the server uses TCP instead of UDP
    }
  | CallServerTypeWebrtc -- ^ A WebRTC server
    { CallServerType -> Maybe Text
username      :: Maybe T.Text -- ^ Username to be used for authentication
    , CallServerType -> Maybe Text
password      :: Maybe T.Text -- ^ Authentication password
    , CallServerType -> Maybe Bool
supports_turn :: Maybe Bool   -- ^ True, if the server supports TURN
    , CallServerType -> Maybe Bool
supports_stun :: Maybe Bool   -- ^ True, if the server supports STUN
    }
  deriving (CallServerType -> CallServerType -> Bool
(CallServerType -> CallServerType -> Bool)
-> (CallServerType -> CallServerType -> Bool) -> Eq CallServerType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CallServerType -> CallServerType -> Bool
== :: CallServerType -> CallServerType -> Bool
$c/= :: CallServerType -> CallServerType -> Bool
/= :: CallServerType -> CallServerType -> Bool
Eq, Int -> CallServerType -> ShowS
[CallServerType] -> ShowS
CallServerType -> String
(Int -> CallServerType -> ShowS)
-> (CallServerType -> String)
-> ([CallServerType] -> ShowS)
-> Show CallServerType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CallServerType -> ShowS
showsPrec :: Int -> CallServerType -> ShowS
$cshow :: CallServerType -> String
show :: CallServerType -> String
$cshowList :: [CallServerType] -> ShowS
showList :: [CallServerType] -> ShowS
Show)

instance I.ShortShow CallServerType where
  shortShow :: CallServerType -> String
shortShow CallServerTypeTelegramReflector
    { peer_tag :: CallServerType -> Maybe ByteString
peer_tag = Maybe ByteString
peer_tag_
    , is_tcp :: CallServerType -> Maybe Bool
is_tcp   = Maybe Bool
is_tcp_
    }
      = String
"CallServerTypeTelegramReflector"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"peer_tag" String -> Maybe ByteString -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ByteString
peer_tag_
        , String
"is_tcp"   String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_tcp_
        ]
  shortShow CallServerTypeWebrtc
    { username :: CallServerType -> Maybe Text
username      = Maybe Text
username_
    , password :: CallServerType -> Maybe Text
password      = Maybe Text
password_
    , supports_turn :: CallServerType -> Maybe Bool
supports_turn = Maybe Bool
supports_turn_
    , supports_stun :: CallServerType -> Maybe Bool
supports_stun = Maybe Bool
supports_stun_
    }
      = String
"CallServerTypeWebrtc"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"username"      String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
username_
        , String
"password"      String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
password_
        , String
"supports_turn" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
supports_turn_
        , String
"supports_stun" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
supports_stun_
        ]

instance AT.FromJSON CallServerType where
  parseJSON :: Value -> Parser CallServerType
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
"callServerTypeTelegramReflector" -> Value -> Parser CallServerType
parseCallServerTypeTelegramReflector Value
v
      String
"callServerTypeWebrtc"            -> Value -> Parser CallServerType
parseCallServerTypeWebrtc Value
v
      String
_                                 -> Parser CallServerType
forall a. Monoid a => a
mempty
    
    where
      parseCallServerTypeTelegramReflector :: A.Value -> AT.Parser CallServerType
      parseCallServerTypeTelegramReflector :: Value -> Parser CallServerType
parseCallServerTypeTelegramReflector = String
-> (Object -> Parser CallServerType)
-> Value
-> Parser CallServerType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"CallServerTypeTelegramReflector" ((Object -> Parser CallServerType)
 -> Value -> Parser CallServerType)
-> (Object -> Parser CallServerType)
-> Value
-> Parser CallServerType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe ByteString
peer_tag_ <- (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
"peer_tag"
        Maybe Bool
is_tcp_   <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"is_tcp"
        CallServerType -> Parser CallServerType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CallServerType -> Parser CallServerType)
-> CallServerType -> Parser CallServerType
forall a b. (a -> b) -> a -> b
$ CallServerTypeTelegramReflector
          { peer_tag :: Maybe ByteString
peer_tag = Maybe ByteString
peer_tag_
          , is_tcp :: Maybe Bool
is_tcp   = Maybe Bool
is_tcp_
          }
      parseCallServerTypeWebrtc :: A.Value -> AT.Parser CallServerType
      parseCallServerTypeWebrtc :: Value -> Parser CallServerType
parseCallServerTypeWebrtc = String
-> (Object -> Parser CallServerType)
-> Value
-> Parser CallServerType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"CallServerTypeWebrtc" ((Object -> Parser CallServerType)
 -> Value -> Parser CallServerType)
-> (Object -> Parser CallServerType)
-> Value
-> Parser CallServerType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
username_      <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"username"
        Maybe Text
password_      <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"password"
        Maybe Bool
supports_turn_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"supports_turn"
        Maybe Bool
supports_stun_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"supports_stun"
        CallServerType -> Parser CallServerType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CallServerType -> Parser CallServerType)
-> CallServerType -> Parser CallServerType
forall a b. (a -> b) -> a -> b
$ CallServerTypeWebrtc
          { username :: Maybe Text
username      = Maybe Text
username_
          , password :: Maybe Text
password      = Maybe Text
password_
          , supports_turn :: Maybe Bool
supports_turn = Maybe Bool
supports_turn_
          , supports_stun :: Maybe Bool
supports_stun = Maybe Bool
supports_stun_
          }
  parseJSON Value
_ = Parser CallServerType
forall a. Monoid a => a
mempty