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

data UserGift
  = UserGift -- ^ Represents a gift received by a user
    { UserGift -> Maybe Int
sender_user_id  :: Maybe Int                         -- ^ Identifier of the user that sent the gift; 0 if unknown
    , UserGift -> Maybe FormattedText
text            :: Maybe FormattedText.FormattedText -- ^ Message added to the gift
    , UserGift -> Maybe Bool
is_private      :: Maybe Bool                        -- ^ True, if the sender and gift text are shown only to the gift receiver; otherwise, everyone are able to see them
    , UserGift -> Maybe Bool
is_saved        :: Maybe Bool                        -- ^ True, if the gift is displayed on the user's profile page; may be false only for the receiver of the gift
    , UserGift -> Maybe Int
date            :: Maybe Int                         -- ^ Point in time (Unix timestamp) when the gift was sent
    , UserGift -> Maybe Gift
gift            :: Maybe Gift.Gift                   -- ^ The gift
    , UserGift -> Maybe Int
message_id      :: Maybe Int                         -- ^ Identifier of the message with the gift in the chat with the sender of the gift; can be 0 or an identifier of a deleted message; only for the gift receiver
    , UserGift -> Maybe Int
sell_star_count :: Maybe Int                         -- ^ Number of Telegram Stars that can be claimed by the receiver instead of the gift; only for the gift receiver
    }
  deriving (UserGift -> UserGift -> Bool
(UserGift -> UserGift -> Bool)
-> (UserGift -> UserGift -> Bool) -> Eq UserGift
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UserGift -> UserGift -> Bool
== :: UserGift -> UserGift -> Bool
$c/= :: UserGift -> UserGift -> Bool
/= :: UserGift -> UserGift -> Bool
Eq, Int -> UserGift -> ShowS
[UserGift] -> ShowS
UserGift -> String
(Int -> UserGift -> ShowS)
-> (UserGift -> String) -> ([UserGift] -> ShowS) -> Show UserGift
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UserGift -> ShowS
showsPrec :: Int -> UserGift -> ShowS
$cshow :: UserGift -> String
show :: UserGift -> String
$cshowList :: [UserGift] -> ShowS
showList :: [UserGift] -> ShowS
Show)

instance I.ShortShow UserGift where
  shortShow :: UserGift -> String
shortShow UserGift
    { sender_user_id :: UserGift -> Maybe Int
sender_user_id  = Maybe Int
sender_user_id_
    , text :: UserGift -> Maybe FormattedText
text            = Maybe FormattedText
text_
    , is_private :: UserGift -> Maybe Bool
is_private      = Maybe Bool
is_private_
    , is_saved :: UserGift -> Maybe Bool
is_saved        = Maybe Bool
is_saved_
    , date :: UserGift -> Maybe Int
date            = Maybe Int
date_
    , gift :: UserGift -> Maybe Gift
gift            = Maybe Gift
gift_
    , message_id :: UserGift -> Maybe Int
message_id      = Maybe Int
message_id_
    , sell_star_count :: UserGift -> Maybe Int
sell_star_count = Maybe Int
sell_star_count_
    }
      = String
"UserGift"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"sender_user_id"  String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
sender_user_id_
        , String
"text"            String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
text_
        , String
"is_private"      String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_private_
        , String
"is_saved"        String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_saved_
        , String
"date"            String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
        , String
"gift"            String -> Maybe Gift -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Gift
gift_
        , String
"message_id"      String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
message_id_
        , String
"sell_star_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
sell_star_count_
        ]

instance AT.FromJSON UserGift where
  parseJSON :: Value -> Parser UserGift
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
"userGift" -> Value -> Parser UserGift
parseUserGift Value
v
      String
_          -> Parser UserGift
forall a. Monoid a => a
mempty
    
    where
      parseUserGift :: A.Value -> AT.Parser UserGift
      parseUserGift :: Value -> Parser UserGift
parseUserGift = String -> (Object -> Parser UserGift) -> Value -> Parser UserGift
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"UserGift" ((Object -> Parser UserGift) -> Value -> Parser UserGift)
-> (Object -> Parser UserGift) -> Value -> Parser UserGift
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
sender_user_id_  <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sender_user_id"
        Maybe FormattedText
text_            <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"text"
        Maybe Bool
is_private_      <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_private"
        Maybe Bool
is_saved_        <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_saved"
        Maybe Int
date_            <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"date"
        Maybe Gift
gift_            <- Object
o Object -> Key -> Parser (Maybe Gift)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"gift"
        Maybe Int
message_id_      <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"message_id"
        Maybe Int
sell_star_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sell_star_count"
        UserGift -> Parser UserGift
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (UserGift -> Parser UserGift) -> UserGift -> Parser UserGift
forall a b. (a -> b) -> a -> b
$ UserGift
          { sender_user_id :: Maybe Int
sender_user_id  = Maybe Int
sender_user_id_
          , text :: Maybe FormattedText
text            = Maybe FormattedText
text_
          , is_private :: Maybe Bool
is_private      = Maybe Bool
is_private_
          , is_saved :: Maybe Bool
is_saved        = Maybe Bool
is_saved_
          , date :: Maybe Int
date            = Maybe Int
date_
          , gift :: Maybe Gift
gift            = Maybe Gift
gift_
          , message_id :: Maybe Int
message_id      = Maybe Int
message_id_
          , sell_star_count :: Maybe Int
sell_star_count = Maybe Int
sell_star_count_
          }
  parseJSON Value
_ = Parser UserGift
forall a. Monoid a => a
mempty