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

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 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
is_for_birthday         :: Maybe Bool            -- ^ True, if the gift is a birthday gift
    , Gift -> Maybe Int
remaining_count         :: Maybe Int             -- ^ Number of remaining times the gift can be purchased; 0 if not limited or the gift was sold out
    , Gift -> Maybe Int
total_count             :: Maybe Int             -- ^ Number of total times the gift can be purchased; 0 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_
    , 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_
    , is_for_birthday :: Gift -> Maybe Bool
is_for_birthday         = Maybe Bool
is_for_birthday_
    , remaining_count :: Gift -> Maybe Int
remaining_count         = Maybe Int
remaining_count_
    , total_count :: Gift -> Maybe Int
total_count             = Maybe Int
total_count_
    , 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
"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
"is_for_birthday"         String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_for_birthday_
        , String
"remaining_count"         String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
remaining_count_
        , String
"total_count"             String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
total_count_
        , 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 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
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 Int
remaining_count_         <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"remaining_count"
        Maybe Int
total_count_             <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"total_count"
        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_
          , 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_
          , is_for_birthday :: Maybe Bool
is_for_birthday         = Maybe Bool
is_for_birthday_
          , remaining_count :: Maybe Int
remaining_count         = Maybe Int
remaining_count_
          , total_count :: Maybe Int
total_count             = Maybe Int
total_count_
          , 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