module TD.Data.WebAppUrl
  (WebAppUrl(..)) 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 WebAppUrl
  = WebAppUrl -- ^ Contains information about a Web App URL
    { WebAppUrl -> Maybe Text
url                 :: Maybe T.Text -- ^ The Web App URL to open in a web view
    , WebAppUrl -> Maybe Bool
require_same_origin :: Maybe Bool   -- ^ True, if events from the Web App must be accepted only from the same origin as the URL
    }
  deriving (WebAppUrl -> WebAppUrl -> Bool
(WebAppUrl -> WebAppUrl -> Bool)
-> (WebAppUrl -> WebAppUrl -> Bool) -> Eq WebAppUrl
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WebAppUrl -> WebAppUrl -> Bool
== :: WebAppUrl -> WebAppUrl -> Bool
$c/= :: WebAppUrl -> WebAppUrl -> Bool
/= :: WebAppUrl -> WebAppUrl -> Bool
Eq, Int -> WebAppUrl -> ShowS
[WebAppUrl] -> ShowS
WebAppUrl -> String
(Int -> WebAppUrl -> ShowS)
-> (WebAppUrl -> String)
-> ([WebAppUrl] -> ShowS)
-> Show WebAppUrl
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WebAppUrl -> ShowS
showsPrec :: Int -> WebAppUrl -> ShowS
$cshow :: WebAppUrl -> String
show :: WebAppUrl -> String
$cshowList :: [WebAppUrl] -> ShowS
showList :: [WebAppUrl] -> ShowS
Show)

instance I.ShortShow WebAppUrl where
  shortShow :: WebAppUrl -> String
shortShow WebAppUrl
    { url :: WebAppUrl -> Maybe Text
url                 = Maybe Text
url_
    , require_same_origin :: WebAppUrl -> Maybe Bool
require_same_origin = Maybe Bool
require_same_origin_
    }
      = String
"WebAppUrl"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"url"                 String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
url_
        , String
"require_same_origin" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
require_same_origin_
        ]

instance AT.FromJSON WebAppUrl where
  parseJSON :: Value -> Parser WebAppUrl
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
"webAppUrl" -> Value -> Parser WebAppUrl
parseWebAppUrl Value
v
      String
_           -> Parser WebAppUrl
forall a. Monoid a => a
mempty
    
    where
      parseWebAppUrl :: A.Value -> AT.Parser WebAppUrl
      parseWebAppUrl :: Value -> Parser WebAppUrl
parseWebAppUrl = String -> (Object -> Parser WebAppUrl) -> Value -> Parser WebAppUrl
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"WebAppUrl" ((Object -> Parser WebAppUrl) -> Value -> Parser WebAppUrl)
-> (Object -> Parser WebAppUrl) -> Value -> Parser WebAppUrl
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
url_                 <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"url"
        Maybe Bool
require_same_origin_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"require_same_origin"
        WebAppUrl -> Parser WebAppUrl
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WebAppUrl -> Parser WebAppUrl) -> WebAppUrl -> Parser WebAppUrl
forall a b. (a -> b) -> a -> b
$ WebAppUrl
          { url :: Maybe Text
url                 = Maybe Text
url_
          , require_same_origin :: Maybe Bool
require_same_origin = Maybe Bool
require_same_origin_
          }
  parseJSON Value
_ = Parser WebAppUrl
forall a. Monoid a => a
mempty