module TD.Data.WebAppInfo
  (WebAppInfo(..)) 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 WebAppInfo
  = WebAppInfo -- ^ Contains information about a Web App
    { WebAppInfo -> Maybe Int
launch_id :: Maybe Int    -- ^ Unique identifier for the Web App launch
    , WebAppInfo -> Maybe Text
url       :: Maybe T.Text -- ^ A Web App URL to open in a web view
    }
  deriving (WebAppInfo -> WebAppInfo -> Bool
(WebAppInfo -> WebAppInfo -> Bool)
-> (WebAppInfo -> WebAppInfo -> Bool) -> Eq WebAppInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WebAppInfo -> WebAppInfo -> Bool
== :: WebAppInfo -> WebAppInfo -> Bool
$c/= :: WebAppInfo -> WebAppInfo -> Bool
/= :: WebAppInfo -> WebAppInfo -> Bool
Eq, Int -> WebAppInfo -> ShowS
[WebAppInfo] -> ShowS
WebAppInfo -> String
(Int -> WebAppInfo -> ShowS)
-> (WebAppInfo -> String)
-> ([WebAppInfo] -> ShowS)
-> Show WebAppInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WebAppInfo -> ShowS
showsPrec :: Int -> WebAppInfo -> ShowS
$cshow :: WebAppInfo -> String
show :: WebAppInfo -> String
$cshowList :: [WebAppInfo] -> ShowS
showList :: [WebAppInfo] -> ShowS
Show)

instance I.ShortShow WebAppInfo where
  shortShow :: WebAppInfo -> String
shortShow WebAppInfo
    { launch_id :: WebAppInfo -> Maybe Int
launch_id = Maybe Int
launch_id_
    , url :: WebAppInfo -> Maybe Text
url       = Maybe Text
url_
    }
      = String
"WebAppInfo"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"launch_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
launch_id_
        , String
"url"       String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
url_
        ]

instance AT.FromJSON WebAppInfo where
  parseJSON :: Value -> Parser WebAppInfo
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
"webAppInfo" -> Value -> Parser WebAppInfo
parseWebAppInfo Value
v
      String
_            -> Parser WebAppInfo
forall a. Monoid a => a
mempty
    
    where
      parseWebAppInfo :: A.Value -> AT.Parser WebAppInfo
      parseWebAppInfo :: Value -> Parser WebAppInfo
parseWebAppInfo = String
-> (Object -> Parser WebAppInfo) -> Value -> Parser WebAppInfo
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"WebAppInfo" ((Object -> Parser WebAppInfo) -> Value -> Parser WebAppInfo)
-> (Object -> Parser WebAppInfo) -> Value -> Parser WebAppInfo
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
launch_id_ <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
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
"launch_id"
        Maybe Text
url_       <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"url"
        WebAppInfo -> Parser WebAppInfo
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WebAppInfo -> Parser WebAppInfo)
-> WebAppInfo -> Parser WebAppInfo
forall a b. (a -> b) -> a -> b
$ WebAppInfo
          { launch_id :: Maybe Int
launch_id = Maybe Int
launch_id_
          , url :: Maybe Text
url       = Maybe Text
url_
          }
  parseJSON Value
_ = Parser WebAppInfo
forall a. Monoid a => a
mempty