module TD.Data.SentGift
  (SentGift(..)) 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 TD.Data.UpgradedGift as UpgradedGift

-- | Represents a gift received by a user
data SentGift
  = SentGiftRegular -- ^ Regular gift
    { SentGift -> Maybe Gift
gift :: Maybe Gift.Gift -- ^ The gift
    }
  | SentGiftUpgraded -- ^ Upgraded gift
    { SentGift -> Maybe UpgradedGift
_gift :: Maybe UpgradedGift.UpgradedGift -- ^ The gift
    }
  deriving (SentGift -> SentGift -> Bool
(SentGift -> SentGift -> Bool)
-> (SentGift -> SentGift -> Bool) -> Eq SentGift
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SentGift -> SentGift -> Bool
== :: SentGift -> SentGift -> Bool
$c/= :: SentGift -> SentGift -> Bool
/= :: SentGift -> SentGift -> Bool
Eq, Int -> SentGift -> ShowS
[SentGift] -> ShowS
SentGift -> String
(Int -> SentGift -> ShowS)
-> (SentGift -> String) -> ([SentGift] -> ShowS) -> Show SentGift
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SentGift -> ShowS
showsPrec :: Int -> SentGift -> ShowS
$cshow :: SentGift -> String
show :: SentGift -> String
$cshowList :: [SentGift] -> ShowS
showList :: [SentGift] -> ShowS
Show)

instance I.ShortShow SentGift where
  shortShow :: SentGift -> String
shortShow SentGiftRegular
    { gift :: SentGift -> Maybe Gift
gift = Maybe Gift
gift_
    }
      = String
"SentGiftRegular"
        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_
        ]
  shortShow SentGiftUpgraded
    { _gift :: SentGift -> Maybe UpgradedGift
_gift = Maybe UpgradedGift
_gift_
    }
      = String
"SentGiftUpgraded"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_gift" String -> Maybe UpgradedGift -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe UpgradedGift
_gift_
        ]

instance AT.FromJSON SentGift where
  parseJSON :: Value -> Parser SentGift
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
"sentGiftRegular"  -> Value -> Parser SentGift
parseSentGiftRegular Value
v
      String
"sentGiftUpgraded" -> Value -> Parser SentGift
parseSentGiftUpgraded Value
v
      String
_                  -> Parser SentGift
forall a. Monoid a => a
mempty
    
    where
      parseSentGiftRegular :: A.Value -> AT.Parser SentGift
      parseSentGiftRegular :: Value -> Parser SentGift
parseSentGiftRegular = String -> (Object -> Parser SentGift) -> Value -> Parser SentGift
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"SentGiftRegular" ((Object -> Parser SentGift) -> Value -> Parser SentGift)
-> (Object -> Parser SentGift) -> Value -> Parser SentGift
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"
        SentGift -> Parser SentGift
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SentGift -> Parser SentGift) -> SentGift -> Parser SentGift
forall a b. (a -> b) -> a -> b
$ SentGiftRegular
          { gift :: Maybe Gift
gift = Maybe Gift
gift_
          }
      parseSentGiftUpgraded :: A.Value -> AT.Parser SentGift
      parseSentGiftUpgraded :: Value -> Parser SentGift
parseSentGiftUpgraded = String -> (Object -> Parser SentGift) -> Value -> Parser SentGift
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"SentGiftUpgraded" ((Object -> Parser SentGift) -> Value -> Parser SentGift)
-> (Object -> Parser SentGift) -> Value -> Parser SentGift
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe UpgradedGift
_gift_ <- Object
o Object -> Key -> Parser (Maybe UpgradedGift)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"gift"
        SentGift -> Parser SentGift
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SentGift -> Parser SentGift) -> SentGift -> Parser SentGift
forall a b. (a -> b) -> a -> b
$ SentGiftUpgraded
          { _gift :: Maybe UpgradedGift
_gift = Maybe UpgradedGift
_gift_
          }
  parseJSON Value
_ = Parser SentGift
forall a. Monoid a => a
mempty