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

data Gift
  = Gift -- ^ Describes a gift that can be sent to another user or channel chat
    { Gift -> Maybe Int
_id                     :: Maybe Int                                   -- ^ Unique identifier of the gift
    , Gift -> Maybe Int
publisher_chat_id       :: Maybe Int                                   -- ^ Identifier of the chat that published the gift; 0 if none
    , Gift -> Maybe Sticker
sticker                 :: Maybe Sticker.Sticker                       -- ^ The sticker representing the gift
    , Gift -> Maybe Int
star_count              :: Maybe Int                                   -- ^ Number of Telegram Stars that must be paid for the gift
    , Gift -> Maybe Int
default_sell_star_count :: Maybe Int                                   -- ^ Number of Telegram Stars that can be claimed by the receiver instead of the regular gift by default. If the gift was paid with just bought Telegram Stars, then full value can be claimed
    , Gift -> Maybe Int
upgrade_star_count      :: Maybe Int                                   -- ^ Number of Telegram Stars that must be paid to upgrade the gift; 0 if upgrade isn't possible
    , Gift -> Maybe Bool
has_colors              :: Maybe Bool                                  -- ^ True, if the gift can be used to customize the user's name, and backgrounds of profile photo, reply header, and link preview
    , Gift -> Maybe Bool
is_for_birthday         :: Maybe Bool                                  -- ^ True, if the gift is a birthday gift
    , Gift -> Maybe Bool
is_premium              :: Maybe Bool                                  -- ^ True, if the gift can be bought only by Telegram Premium subscribers
    , Gift -> Maybe Int
next_send_date          :: Maybe Int                                   -- ^ Point in time (Unix timestamp) when the gift can be sent next time by the current user; can be 0 or a date in the past. If the date is in the future, then call canSendGift to get the reason, why the gift can't be sent now
    , Gift -> Maybe GiftPurchaseLimits
user_limits             :: Maybe GiftPurchaseLimits.GiftPurchaseLimits -- ^ Number of times the gift can be purchased by the current user; may be null if not limited
    , Gift -> Maybe GiftPurchaseLimits
overall_limits          :: Maybe GiftPurchaseLimits.GiftPurchaseLimits -- ^ Number of times the gift can be purchased all users; may be null if not limited
    , Gift -> Maybe Int
first_send_date         :: Maybe Int                                   -- ^ Point in time (Unix timestamp) when the gift was send for the first time; for sold out gifts only
    , Gift -> Maybe Int
last_send_date          :: Maybe Int                                   -- ^ Point in time (Unix timestamp) when the gift was send for the last time; for sold out gifts only
    }
  deriving (Gift -> Gift -> Bool
(Gift -> Gift -> Bool) -> (Gift -> Gift -> Bool) -> Eq Gift
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Gift -> Gift -> Bool
== :: Gift -> Gift -> Bool
$c/= :: Gift -> Gift -> Bool
/= :: Gift -> Gift -> Bool
Eq, Int -> Gift -> ShowS
[Gift] -> ShowS
Gift -> String
(Int -> Gift -> ShowS)
-> (Gift -> String) -> ([Gift] -> ShowS) -> Show Gift
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Gift -> ShowS
showsPrec :: Int -> Gift -> ShowS
$cshow :: Gift -> String
show :: Gift -> String
$cshowList :: [Gift] -> ShowS
showList :: [Gift] -> ShowS
Show)

instance I.ShortShow Gift where
  shortShow :: Gift -> String
shortShow Gift
    { _id :: Gift -> Maybe Int
_id                     = Maybe Int
_id_
    , publisher_chat_id :: Gift -> Maybe Int
publisher_chat_id       = Maybe Int
publisher_chat_id_
    , sticker :: Gift -> Maybe Sticker
sticker                 = Maybe Sticker
sticker_
    , star_count :: Gift -> Maybe Int
star_count              = Maybe Int
star_count_
    , default_sell_star_count :: Gift -> Maybe Int
default_sell_star_count = Maybe Int
default_sell_star_count_
    , upgrade_star_count :: Gift -> Maybe Int
upgrade_star_count      = Maybe Int
upgrade_star_count_
    , has_colors :: Gift -> Maybe Bool
has_colors              = Maybe Bool
has_colors_
    , is_for_birthday :: Gift -> Maybe Bool
is_for_birthday         = Maybe Bool
is_for_birthday_
    , is_premium :: Gift -> Maybe Bool
is_premium              = Maybe Bool
is_premium_
    , next_send_date :: Gift -> Maybe Int
next_send_date          = Maybe Int
next_send_date_
    , user_limits :: Gift -> Maybe GiftPurchaseLimits
user_limits             = Maybe GiftPurchaseLimits
user_limits_
    , overall_limits :: Gift -> Maybe GiftPurchaseLimits
overall_limits          = Maybe GiftPurchaseLimits
overall_limits_
    , first_send_date :: Gift -> Maybe Int
first_send_date         = Maybe Int
first_send_date_
    , last_send_date :: Gift -> Maybe Int
last_send_date          = Maybe Int
last_send_date_
    }
      = String
"Gift"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"                     String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_id_
        , String
"publisher_chat_id"       String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
publisher_chat_id_
        , String
"sticker"                 String -> Maybe Sticker -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Sticker
sticker_
        , String
"star_count"              String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
star_count_
        , String
"default_sell_star_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
default_sell_star_count_
        , String
"upgrade_star_count"      String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
upgrade_star_count_
        , String
"has_colors"              String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
has_colors_
        , String
"is_for_birthday"         String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_for_birthday_
        , String
"is_premium"              String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_premium_
        , String
"next_send_date"          String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
next_send_date_
        , String
"user_limits"             String -> Maybe GiftPurchaseLimits -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe GiftPurchaseLimits
user_limits_
        , String
"overall_limits"          String -> Maybe GiftPurchaseLimits -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe GiftPurchaseLimits
overall_limits_
        , String
"first_send_date"         String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
first_send_date_
        , String
"last_send_date"          String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
last_send_date_
        ]

instance AT.FromJSON Gift where
  parseJSON :: Value -> Parser Gift
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
"gift" -> Value -> Parser Gift
parseGift Value
v
      String
_      -> Parser Gift
forall a. Monoid a => a
mempty
    
    where
      parseGift :: A.Value -> AT.Parser Gift
      parseGift :: Value -> Parser Gift
parseGift = String -> (Object -> Parser Gift) -> Value -> Parser Gift
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Gift" ((Object -> Parser Gift) -> Value -> Parser Gift)
-> (Object -> Parser Gift) -> Value -> Parser Gift
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_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
"id"
        Maybe Int
publisher_chat_id_       <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"publisher_chat_id"
        Maybe Sticker
sticker_                 <- Object
o Object -> Key -> Parser (Maybe Sticker)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"sticker"
        Maybe Int
star_count_              <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"star_count"
        Maybe Int
default_sell_star_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"default_sell_star_count"
        Maybe Int
upgrade_star_count_      <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"upgrade_star_count"
        Maybe Bool
has_colors_              <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"has_colors"
        Maybe Bool
is_for_birthday_         <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"is_for_birthday"
        Maybe Bool
is_premium_              <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"is_premium"
        Maybe Int
next_send_date_          <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"next_send_date"
        Maybe GiftPurchaseLimits
user_limits_             <- Object
o Object -> Key -> Parser (Maybe GiftPurchaseLimits)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"user_limits"
        Maybe GiftPurchaseLimits
overall_limits_          <- Object
o Object -> Key -> Parser (Maybe GiftPurchaseLimits)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"overall_limits"
        Maybe Int
first_send_date_         <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"first_send_date"
        Maybe Int
last_send_date_          <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"last_send_date"
        Gift -> Parser Gift
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Gift -> Parser Gift) -> Gift -> Parser Gift
forall a b. (a -> b) -> a -> b
$ Gift
          { _id :: Maybe Int
_id                     = Maybe Int
_id_
          , publisher_chat_id :: Maybe Int
publisher_chat_id       = Maybe Int
publisher_chat_id_
          , sticker :: Maybe Sticker
sticker                 = Maybe Sticker
sticker_
          , star_count :: Maybe Int
star_count              = Maybe Int
star_count_
          , default_sell_star_count :: Maybe Int
default_sell_star_count = Maybe Int
default_sell_star_count_
          , upgrade_star_count :: Maybe Int
upgrade_star_count      = Maybe Int
upgrade_star_count_
          , has_colors :: Maybe Bool
has_colors              = Maybe Bool
has_colors_
          , is_for_birthday :: Maybe Bool
is_for_birthday         = Maybe Bool
is_for_birthday_
          , is_premium :: Maybe Bool
is_premium              = Maybe Bool
is_premium_
          , next_send_date :: Maybe Int
next_send_date          = Maybe Int
next_send_date_
          , user_limits :: Maybe GiftPurchaseLimits
user_limits             = Maybe GiftPurchaseLimits
user_limits_
          , overall_limits :: Maybe GiftPurchaseLimits
overall_limits          = Maybe GiftPurchaseLimits
overall_limits_
          , first_send_date :: Maybe Int
first_send_date         = Maybe Int
first_send_date_
          , last_send_date :: Maybe Int
last_send_date          = Maybe Int
last_send_date_
          }
  parseJSON Value
_ = Parser Gift
forall a. Monoid a => a
mempty