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

data AvailableGift
  = AvailableGift -- ^ Describes a gift that is available for purchase
    { AvailableGift -> Maybe Gift
gift                  :: Maybe Gift.Gift -- ^ The gift
    , AvailableGift -> Maybe Int
resale_count          :: Maybe Int       -- ^ Number of gifts that are available for resale
    , AvailableGift -> Maybe Int
min_resale_star_count :: Maybe Int       -- ^ The minimum price for the gifts available for resale; 0 if there are no such gifts
    , AvailableGift -> Maybe Text
title                 :: Maybe T.Text    -- ^ The title of the upgraded gift; empty if the gift isn't available for resale
    }
  deriving (AvailableGift -> AvailableGift -> Bool
(AvailableGift -> AvailableGift -> Bool)
-> (AvailableGift -> AvailableGift -> Bool) -> Eq AvailableGift
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AvailableGift -> AvailableGift -> Bool
== :: AvailableGift -> AvailableGift -> Bool
$c/= :: AvailableGift -> AvailableGift -> Bool
/= :: AvailableGift -> AvailableGift -> Bool
Eq, Int -> AvailableGift -> ShowS
[AvailableGift] -> ShowS
AvailableGift -> String
(Int -> AvailableGift -> ShowS)
-> (AvailableGift -> String)
-> ([AvailableGift] -> ShowS)
-> Show AvailableGift
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AvailableGift -> ShowS
showsPrec :: Int -> AvailableGift -> ShowS
$cshow :: AvailableGift -> String
show :: AvailableGift -> String
$cshowList :: [AvailableGift] -> ShowS
showList :: [AvailableGift] -> ShowS
Show)

instance I.ShortShow AvailableGift where
  shortShow :: AvailableGift -> String
shortShow AvailableGift
    { gift :: AvailableGift -> Maybe Gift
gift                  = Maybe Gift
gift_
    , resale_count :: AvailableGift -> Maybe Int
resale_count          = Maybe Int
resale_count_
    , min_resale_star_count :: AvailableGift -> Maybe Int
min_resale_star_count = Maybe Int
min_resale_star_count_
    , title :: AvailableGift -> Maybe Text
title                 = Maybe Text
title_
    }
      = String
"AvailableGift"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"gift"                  String -> Maybe Gift -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Gift
gift_
        , String
"resale_count"          String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
resale_count_
        , String
"min_resale_star_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
min_resale_star_count_
        , String
"title"                 String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
title_
        ]

instance AT.FromJSON AvailableGift where
  parseJSON :: Value -> Parser AvailableGift
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
"availableGift" -> Value -> Parser AvailableGift
parseAvailableGift Value
v
      String
_               -> Parser AvailableGift
forall a. Monoid a => a
mempty
    
    where
      parseAvailableGift :: A.Value -> AT.Parser AvailableGift
      parseAvailableGift :: Value -> Parser AvailableGift
parseAvailableGift = String
-> (Object -> Parser AvailableGift)
-> Value
-> Parser AvailableGift
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"AvailableGift" ((Object -> Parser AvailableGift) -> Value -> Parser AvailableGift)
-> (Object -> Parser AvailableGift)
-> Value
-> Parser AvailableGift
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Gift
gift_                  <- Object
o Object -> Key -> Parser (Maybe Gift)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"gift"
        Maybe Int
resale_count_          <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"resale_count"
        Maybe Int
min_resale_star_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"min_resale_star_count"
        Maybe Text
title_                 <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"title"
        AvailableGift -> Parser AvailableGift
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AvailableGift -> Parser AvailableGift)
-> AvailableGift -> Parser AvailableGift
forall a b. (a -> b) -> a -> b
$ AvailableGift
          { gift :: Maybe Gift
gift                  = Maybe Gift
gift_
          , resale_count :: Maybe Int
resale_count          = Maybe Int
resale_count_
          , min_resale_star_count :: Maybe Int
min_resale_star_count = Maybe Int
min_resale_star_count_
          , title :: Maybe Text
title                 = Maybe Text
title_
          }
  parseJSON Value
_ = Parser AvailableGift
forall a. Monoid a => a
mempty