module TD.Data.Gifts
  (Gifts(..)) 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

data Gifts
  = Gifts -- ^ Contains a list of gifts that can be sent to another user
    { Gifts -> Maybe [Gift]
gifts :: Maybe [Gift.Gift] -- ^ The list of gifts
    }
  deriving (Gifts -> Gifts -> Bool
(Gifts -> Gifts -> Bool) -> (Gifts -> Gifts -> Bool) -> Eq Gifts
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Gifts -> Gifts -> Bool
== :: Gifts -> Gifts -> Bool
$c/= :: Gifts -> Gifts -> Bool
/= :: Gifts -> Gifts -> Bool
Eq, Int -> Gifts -> ShowS
[Gifts] -> ShowS
Gifts -> String
(Int -> Gifts -> ShowS)
-> (Gifts -> String) -> ([Gifts] -> ShowS) -> Show Gifts
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Gifts -> ShowS
showsPrec :: Int -> Gifts -> ShowS
$cshow :: Gifts -> String
show :: Gifts -> String
$cshowList :: [Gifts] -> ShowS
showList :: [Gifts] -> ShowS
Show)

instance I.ShortShow Gifts where
  shortShow :: Gifts -> String
shortShow Gifts
    { gifts :: Gifts -> Maybe [Gift]
gifts = Maybe [Gift]
gifts_
    }
      = String
"Gifts"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"gifts" String -> Maybe [Gift] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Gift]
gifts_
        ]

instance AT.FromJSON Gifts where
  parseJSON :: Value -> Parser Gifts
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
"gifts" -> Value -> Parser Gifts
parseGifts Value
v
      String
_       -> Parser Gifts
forall a. Monoid a => a
mempty
    
    where
      parseGifts :: A.Value -> AT.Parser Gifts
      parseGifts :: Value -> Parser Gifts
parseGifts = String -> (Object -> Parser Gifts) -> Value -> Parser Gifts
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Gifts" ((Object -> Parser Gifts) -> Value -> Parser Gifts)
-> (Object -> Parser Gifts) -> Value -> Parser Gifts
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [Gift]
gifts_ <- Object
o Object -> Key -> Parser (Maybe [Gift])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"gifts"
        Gifts -> Parser Gifts
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Gifts -> Parser Gifts) -> Gifts -> Parser Gifts
forall a b. (a -> b) -> a -> b
$ Gifts
          { gifts :: Maybe [Gift]
gifts = Maybe [Gift]
gifts_
          }
  parseJSON Value
_ = Parser Gifts
forall a. Monoid a => a
mempty