module TD.Data.TMeUrl
  (TMeUrl(..)) 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
import qualified TD.Data.TMeUrlType as TMeUrlType

data TMeUrl
  = TMeUrl -- ^ Represents a URL linking to an internal Telegram entity
    { TMeUrl -> Maybe Text
url   :: Maybe T.Text                -- ^ URL
    , TMeUrl -> Maybe TMeUrlType
_type :: Maybe TMeUrlType.TMeUrlType -- ^ Type of the URL
    }
  deriving (TMeUrl -> TMeUrl -> Bool
(TMeUrl -> TMeUrl -> Bool)
-> (TMeUrl -> TMeUrl -> Bool) -> Eq TMeUrl
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TMeUrl -> TMeUrl -> Bool
== :: TMeUrl -> TMeUrl -> Bool
$c/= :: TMeUrl -> TMeUrl -> Bool
/= :: TMeUrl -> TMeUrl -> Bool
Eq, Int -> TMeUrl -> ShowS
[TMeUrl] -> ShowS
TMeUrl -> String
(Int -> TMeUrl -> ShowS)
-> (TMeUrl -> String) -> ([TMeUrl] -> ShowS) -> Show TMeUrl
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TMeUrl -> ShowS
showsPrec :: Int -> TMeUrl -> ShowS
$cshow :: TMeUrl -> String
show :: TMeUrl -> String
$cshowList :: [TMeUrl] -> ShowS
showList :: [TMeUrl] -> ShowS
Show)

instance I.ShortShow TMeUrl where
  shortShow :: TMeUrl -> String
shortShow TMeUrl
    { url :: TMeUrl -> Maybe Text
url   = Maybe Text
url_
    , _type :: TMeUrl -> Maybe TMeUrlType
_type = Maybe TMeUrlType
_type_
    }
      = String
"TMeUrl"
        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
"_type" String -> Maybe TMeUrlType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe TMeUrlType
_type_
        ]

instance AT.FromJSON TMeUrl where
  parseJSON :: Value -> Parser TMeUrl
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
"tMeUrl" -> Value -> Parser TMeUrl
parseTMeUrl Value
v
      String
_        -> Parser TMeUrl
forall a. Monoid a => a
mempty
    
    where
      parseTMeUrl :: A.Value -> AT.Parser TMeUrl
      parseTMeUrl :: Value -> Parser TMeUrl
parseTMeUrl = String -> (Object -> Parser TMeUrl) -> Value -> Parser TMeUrl
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TMeUrl" ((Object -> Parser TMeUrl) -> Value -> Parser TMeUrl)
-> (Object -> Parser TMeUrl) -> Value -> Parser TMeUrl
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 TMeUrlType
_type_ <- Object
o Object -> Key -> Parser (Maybe TMeUrlType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"type"
        TMeUrl -> Parser TMeUrl
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TMeUrl -> Parser TMeUrl) -> TMeUrl -> Parser TMeUrl
forall a b. (a -> b) -> a -> b
$ TMeUrl
          { url :: Maybe Text
url   = Maybe Text
url_
          , _type :: Maybe TMeUrlType
_type = Maybe TMeUrlType
_type_
          }
  parseJSON Value
_ = Parser TMeUrl
forall a. Monoid a => a
mempty