module TD.Data.WebProxyCarrier
  (WebProxyCarrier(..)) 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 WebProxyCarrier
  = WebProxyCarrier -- ^ Describes a web proxy carrier: an HTTPS relay page, which carries traffic of a web proxy and must be loaded by the application as the top-level document of a hidden web view. Before loading the page, the application must arrange for an object named carrier.bridge_object_name to be defined in the top-level document whenever its origin is exactly carrier.origin, and in no other document or frame. The object must be read-only and must have two members: a method postMessage(value), which passes an ArrayBuffer or a typed array to processWebProxyCarrierMessage as webProxyCarrierMessageBinary, passes a string as webProxyCarrierMessageText, and rejects any other value; and a property onmessage, which the page sets to a function. The application delivers data to the page by calling that function with an object {data: value}, where value is an ArrayBuffer for binary data and a string for text. Each carrier needs its own web view
    { WebProxyCarrier -> Maybe Int
_id                :: Maybe Int    -- ^ Unique identifier of the carrier
    , WebProxyCarrier -> Maybe Int
proxy_id           :: Maybe Int    -- ^ Identifier of the added proxy that uses the carrier; 0 if the carrier is used by pingProxy or testProxy
    , WebProxyCarrier -> Maybe Text
url                :: Maybe T.Text -- ^ The HTTPS URL to load. The URL contains the capability for the proxy and a fragment, which must be kept; the URL must never be logged or shown to the user
    , WebProxyCarrier -> Maybe Text
origin             :: Maybe T.Text -- ^ Origin of the relay page, for example, "https://proxy.example.com". The web view must not be allowed to navigate to another origin
    , WebProxyCarrier -> Maybe Text
path_prefix        :: Maybe T.Text -- ^ The only path prefix on carrier.origin from which the page may load resources or to which it may send requests, for example, "/" or "/relay/"
    , WebProxyCarrier -> Maybe Text
bridge_object_name :: Maybe T.Text -- ^ Name of the object to define in the top-level document; currently, always "TelegramWebProxy"
    , WebProxyCarrier -> Maybe Bool
is_temporary       :: Maybe Bool   -- ^ True, if the carrier is needed only to test the proxy and will be closed after the test. The application may refuse to open such carriers, for example, to avoid creating a web view for every saved proxy
    }
  deriving (WebProxyCarrier -> WebProxyCarrier -> Bool
(WebProxyCarrier -> WebProxyCarrier -> Bool)
-> (WebProxyCarrier -> WebProxyCarrier -> Bool)
-> Eq WebProxyCarrier
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WebProxyCarrier -> WebProxyCarrier -> Bool
== :: WebProxyCarrier -> WebProxyCarrier -> Bool
$c/= :: WebProxyCarrier -> WebProxyCarrier -> Bool
/= :: WebProxyCarrier -> WebProxyCarrier -> Bool
Eq, Int -> WebProxyCarrier -> ShowS
[WebProxyCarrier] -> ShowS
WebProxyCarrier -> String
(Int -> WebProxyCarrier -> ShowS)
-> (WebProxyCarrier -> String)
-> ([WebProxyCarrier] -> ShowS)
-> Show WebProxyCarrier
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WebProxyCarrier -> ShowS
showsPrec :: Int -> WebProxyCarrier -> ShowS
$cshow :: WebProxyCarrier -> String
show :: WebProxyCarrier -> String
$cshowList :: [WebProxyCarrier] -> ShowS
showList :: [WebProxyCarrier] -> ShowS
Show)

instance I.ShortShow WebProxyCarrier where
  shortShow :: WebProxyCarrier -> String
shortShow WebProxyCarrier
    { _id :: WebProxyCarrier -> Maybe Int
_id                = Maybe Int
_id_
    , proxy_id :: WebProxyCarrier -> Maybe Int
proxy_id           = Maybe Int
proxy_id_
    , url :: WebProxyCarrier -> Maybe Text
url                = Maybe Text
url_
    , origin :: WebProxyCarrier -> Maybe Text
origin             = Maybe Text
origin_
    , path_prefix :: WebProxyCarrier -> Maybe Text
path_prefix        = Maybe Text
path_prefix_
    , bridge_object_name :: WebProxyCarrier -> Maybe Text
bridge_object_name = Maybe Text
bridge_object_name_
    , is_temporary :: WebProxyCarrier -> Maybe Bool
is_temporary       = Maybe Bool
is_temporary_
    }
      = String
"WebProxyCarrier"
        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
"proxy_id"           String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
proxy_id_
        , String
"url"                String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
url_
        , String
"origin"             String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
origin_
        , String
"path_prefix"        String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
path_prefix_
        , String
"bridge_object_name" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
bridge_object_name_
        , String
"is_temporary"       String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_temporary_
        ]

instance AT.FromJSON WebProxyCarrier where
  parseJSON :: Value -> Parser WebProxyCarrier
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
"webProxyCarrier" -> Value -> Parser WebProxyCarrier
parseWebProxyCarrier Value
v
      String
_                 -> Parser WebProxyCarrier
forall a. Monoid a => a
mempty
    
    where
      parseWebProxyCarrier :: A.Value -> AT.Parser WebProxyCarrier
      parseWebProxyCarrier :: Value -> Parser WebProxyCarrier
parseWebProxyCarrier = String
-> (Object -> Parser WebProxyCarrier)
-> Value
-> Parser WebProxyCarrier
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"WebProxyCarrier" ((Object -> Parser WebProxyCarrier)
 -> Value -> Parser WebProxyCarrier)
-> (Object -> Parser WebProxyCarrier)
-> Value
-> Parser WebProxyCarrier
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
proxy_id_           <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"proxy_id"
        Maybe Text
url_                <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"url"
        Maybe Text
origin_             <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"origin"
        Maybe Text
path_prefix_        <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"path_prefix"
        Maybe Text
bridge_object_name_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"bridge_object_name"
        Maybe Bool
is_temporary_       <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_temporary"
        WebProxyCarrier -> Parser WebProxyCarrier
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WebProxyCarrier -> Parser WebProxyCarrier)
-> WebProxyCarrier -> Parser WebProxyCarrier
forall a b. (a -> b) -> a -> b
$ WebProxyCarrier
          { _id :: Maybe Int
_id                = Maybe Int
_id_
          , proxy_id :: Maybe Int
proxy_id           = Maybe Int
proxy_id_
          , url :: Maybe Text
url                = Maybe Text
url_
          , origin :: Maybe Text
origin             = Maybe Text
origin_
          , path_prefix :: Maybe Text
path_prefix        = Maybe Text
path_prefix_
          , bridge_object_name :: Maybe Text
bridge_object_name = Maybe Text
bridge_object_name_
          , is_temporary :: Maybe Bool
is_temporary       = Maybe Bool
is_temporary_
          }
  parseJSON Value
_ = Parser WebProxyCarrier
forall a. Monoid a => a
mempty