module TD.Data.DeepLinkInfo
  (DeepLinkInfo(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified TD.Data.FormattedText as FormattedText

data DeepLinkInfo
  = DeepLinkInfo -- ^ Contains information about a tg: deep link
    { DeepLinkInfo -> Maybe FormattedText
text                    :: Maybe FormattedText.FormattedText -- ^ Text to be shown to the user
    , DeepLinkInfo -> Maybe Bool
need_update_application :: Maybe Bool                        -- ^ True, if the user must be asked to update the application
    }
  deriving (DeepLinkInfo -> DeepLinkInfo -> Bool
(DeepLinkInfo -> DeepLinkInfo -> Bool)
-> (DeepLinkInfo -> DeepLinkInfo -> Bool) -> Eq DeepLinkInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DeepLinkInfo -> DeepLinkInfo -> Bool
== :: DeepLinkInfo -> DeepLinkInfo -> Bool
$c/= :: DeepLinkInfo -> DeepLinkInfo -> Bool
/= :: DeepLinkInfo -> DeepLinkInfo -> Bool
Eq, Int -> DeepLinkInfo -> ShowS
[DeepLinkInfo] -> ShowS
DeepLinkInfo -> String
(Int -> DeepLinkInfo -> ShowS)
-> (DeepLinkInfo -> String)
-> ([DeepLinkInfo] -> ShowS)
-> Show DeepLinkInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DeepLinkInfo -> ShowS
showsPrec :: Int -> DeepLinkInfo -> ShowS
$cshow :: DeepLinkInfo -> String
show :: DeepLinkInfo -> String
$cshowList :: [DeepLinkInfo] -> ShowS
showList :: [DeepLinkInfo] -> ShowS
Show)

instance I.ShortShow DeepLinkInfo where
  shortShow :: DeepLinkInfo -> String
shortShow DeepLinkInfo
    { text :: DeepLinkInfo -> Maybe FormattedText
text                    = Maybe FormattedText
text_
    , need_update_application :: DeepLinkInfo -> Maybe Bool
need_update_application = Maybe Bool
need_update_application_
    }
      = String
"DeepLinkInfo"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"text"                    String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
text_
        , String
"need_update_application" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
need_update_application_
        ]

instance AT.FromJSON DeepLinkInfo where
  parseJSON :: Value -> Parser DeepLinkInfo
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
"deepLinkInfo" -> Value -> Parser DeepLinkInfo
parseDeepLinkInfo Value
v
      String
_              -> Parser DeepLinkInfo
forall a. Monoid a => a
mempty
    
    where
      parseDeepLinkInfo :: A.Value -> AT.Parser DeepLinkInfo
      parseDeepLinkInfo :: Value -> Parser DeepLinkInfo
parseDeepLinkInfo = String
-> (Object -> Parser DeepLinkInfo) -> Value -> Parser DeepLinkInfo
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"DeepLinkInfo" ((Object -> Parser DeepLinkInfo) -> Value -> Parser DeepLinkInfo)
-> (Object -> Parser DeepLinkInfo) -> Value -> Parser DeepLinkInfo
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe FormattedText
text_                    <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"text"
        Maybe Bool
need_update_application_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"need_update_application"
        DeepLinkInfo -> Parser DeepLinkInfo
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DeepLinkInfo -> Parser DeepLinkInfo)
-> DeepLinkInfo -> Parser DeepLinkInfo
forall a b. (a -> b) -> a -> b
$ DeepLinkInfo
          { text :: Maybe FormattedText
text                    = Maybe FormattedText
text_
          , need_update_application :: Maybe Bool
need_update_application = Maybe Bool
need_update_application_
          }
  parseJSON Value
_ = Parser DeepLinkInfo
forall a. Monoid a => a
mempty