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

-- | Describes the type of URL linking to an internal Telegram entity
data TMeUrlType
  = TMeUrlTypeUser -- ^ A URL linking to a user
    { TMeUrlType -> Maybe Int
user_id :: Maybe Int -- ^ Identifier of the user
    }
  | TMeUrlTypeSupergroup -- ^ A URL linking to a public supergroup or channel
    { TMeUrlType -> Maybe Int
supergroup_id :: Maybe Int -- ^ Identifier of the supergroup or channel
    }
  | TMeUrlTypeChatInvite -- ^ A chat invite link
    { TMeUrlType -> Maybe ChatInviteLinkInfo
info :: Maybe ChatInviteLinkInfo.ChatInviteLinkInfo -- ^ Information about the chat invite link
    }
  | TMeUrlTypeStickerSet -- ^ A URL linking to a sticker set
    { TMeUrlType -> Maybe Int
sticker_set_id :: Maybe Int -- ^ Identifier of the sticker set
    }
  deriving (TMeUrlType -> TMeUrlType -> Bool
(TMeUrlType -> TMeUrlType -> Bool)
-> (TMeUrlType -> TMeUrlType -> Bool) -> Eq TMeUrlType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TMeUrlType -> TMeUrlType -> Bool
== :: TMeUrlType -> TMeUrlType -> Bool
$c/= :: TMeUrlType -> TMeUrlType -> Bool
/= :: TMeUrlType -> TMeUrlType -> Bool
Eq, Int -> TMeUrlType -> ShowS
[TMeUrlType] -> ShowS
TMeUrlType -> String
(Int -> TMeUrlType -> ShowS)
-> (TMeUrlType -> String)
-> ([TMeUrlType] -> ShowS)
-> Show TMeUrlType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TMeUrlType -> ShowS
showsPrec :: Int -> TMeUrlType -> ShowS
$cshow :: TMeUrlType -> String
show :: TMeUrlType -> String
$cshowList :: [TMeUrlType] -> ShowS
showList :: [TMeUrlType] -> ShowS
Show)

instance I.ShortShow TMeUrlType where
  shortShow :: TMeUrlType -> String
shortShow TMeUrlTypeUser
    { user_id :: TMeUrlType -> Maybe Int
user_id = Maybe Int
user_id_
    }
      = String
"TMeUrlTypeUser"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"user_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
user_id_
        ]
  shortShow TMeUrlTypeSupergroup
    { supergroup_id :: TMeUrlType -> Maybe Int
supergroup_id = Maybe Int
supergroup_id_
    }
      = String
"TMeUrlTypeSupergroup"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"supergroup_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
supergroup_id_
        ]
  shortShow TMeUrlTypeChatInvite
    { info :: TMeUrlType -> Maybe ChatInviteLinkInfo
info = Maybe ChatInviteLinkInfo
info_
    }
      = String
"TMeUrlTypeChatInvite"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"info" String -> Maybe ChatInviteLinkInfo -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ChatInviteLinkInfo
info_
        ]
  shortShow TMeUrlTypeStickerSet
    { sticker_set_id :: TMeUrlType -> Maybe Int
sticker_set_id = Maybe Int
sticker_set_id_
    }
      = String
"TMeUrlTypeStickerSet"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"sticker_set_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
sticker_set_id_
        ]

instance AT.FromJSON TMeUrlType where
  parseJSON :: Value -> Parser TMeUrlType
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
"tMeUrlTypeUser"       -> Value -> Parser TMeUrlType
parseTMeUrlTypeUser Value
v
      String
"tMeUrlTypeSupergroup" -> Value -> Parser TMeUrlType
parseTMeUrlTypeSupergroup Value
v
      String
"tMeUrlTypeChatInvite" -> Value -> Parser TMeUrlType
parseTMeUrlTypeChatInvite Value
v
      String
"tMeUrlTypeStickerSet" -> Value -> Parser TMeUrlType
parseTMeUrlTypeStickerSet Value
v
      String
_                      -> Parser TMeUrlType
forall a. Monoid a => a
mempty
    
    where
      parseTMeUrlTypeUser :: A.Value -> AT.Parser TMeUrlType
      parseTMeUrlTypeUser :: Value -> Parser TMeUrlType
parseTMeUrlTypeUser = String
-> (Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TMeUrlTypeUser" ((Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType)
-> (Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
user_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"user_id"
        TMeUrlType -> Parser TMeUrlType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TMeUrlType -> Parser TMeUrlType)
-> TMeUrlType -> Parser TMeUrlType
forall a b. (a -> b) -> a -> b
$ TMeUrlTypeUser
          { user_id :: Maybe Int
user_id = Maybe Int
user_id_
          }
      parseTMeUrlTypeSupergroup :: A.Value -> AT.Parser TMeUrlType
      parseTMeUrlTypeSupergroup :: Value -> Parser TMeUrlType
parseTMeUrlTypeSupergroup = String
-> (Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TMeUrlTypeSupergroup" ((Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType)
-> (Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
supergroup_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"supergroup_id"
        TMeUrlType -> Parser TMeUrlType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TMeUrlType -> Parser TMeUrlType)
-> TMeUrlType -> Parser TMeUrlType
forall a b. (a -> b) -> a -> b
$ TMeUrlTypeSupergroup
          { supergroup_id :: Maybe Int
supergroup_id = Maybe Int
supergroup_id_
          }
      parseTMeUrlTypeChatInvite :: A.Value -> AT.Parser TMeUrlType
      parseTMeUrlTypeChatInvite :: Value -> Parser TMeUrlType
parseTMeUrlTypeChatInvite = String
-> (Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TMeUrlTypeChatInvite" ((Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType)
-> (Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe ChatInviteLinkInfo
info_ <- Object
o Object -> Key -> Parser (Maybe ChatInviteLinkInfo)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"info"
        TMeUrlType -> Parser TMeUrlType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TMeUrlType -> Parser TMeUrlType)
-> TMeUrlType -> Parser TMeUrlType
forall a b. (a -> b) -> a -> b
$ TMeUrlTypeChatInvite
          { info :: Maybe ChatInviteLinkInfo
info = Maybe ChatInviteLinkInfo
info_
          }
      parseTMeUrlTypeStickerSet :: A.Value -> AT.Parser TMeUrlType
      parseTMeUrlTypeStickerSet :: Value -> Parser TMeUrlType
parseTMeUrlTypeStickerSet = String
-> (Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TMeUrlTypeStickerSet" ((Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType)
-> (Object -> Parser TMeUrlType) -> Value -> Parser TMeUrlType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
sticker_set_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
"sticker_set_id"
        TMeUrlType -> Parser TMeUrlType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TMeUrlType -> Parser TMeUrlType)
-> TMeUrlType -> Parser TMeUrlType
forall a b. (a -> b) -> a -> b
$ TMeUrlTypeStickerSet
          { sticker_set_id :: Maybe Int
sticker_set_id = Maybe Int
sticker_set_id_
          }
  parseJSON Value
_ = Parser TMeUrlType
forall a. Monoid a => a
mempty