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
    { 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 gift by default. If the gift was paid with just bought Telegram Stars, then full value can be claimed
    , Gift -> Maybe Int
remaining_count         :: Maybe Int             -- ^ Number of remaining times the gift can be purchased by all users; 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 by all users; 0 if not limited
    }
  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_
    , remaining_count :: Gift -> Maybe Int
remaining_count         = Maybe Int
remaining_count_
    , total_count :: Gift -> Maybe Int
total_count             = Maybe Int
total_count_
    }
      = 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
"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_
        ]

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
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"
        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_
          , remaining_count :: Maybe Int
remaining_count         = Maybe Int
remaining_count_
          , total_count :: Maybe Int
total_count             = Maybe Int
total_count_
          }
  parseJSON Value
_ = Parser Gift
forall a. Monoid a => a
mempty