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

-- | Contains result of gift crafting
data CraftGiftResult
  = CraftGiftResultSuccess -- ^ Crafting was successful
    { CraftGiftResult -> Maybe UpgradedGift
gift             :: Maybe UpgradedGift.UpgradedGift -- ^ The created gift
    , CraftGiftResult -> Maybe Text
received_gift_id :: Maybe T.Text                    -- ^ Unique identifier of the received gift for the current user
    }
  | CraftGiftResultTooEarly -- ^ Crafting isn't possible because one of the gifts can't be used for crafting yet
    { CraftGiftResult -> Maybe Int
retry_after :: Maybe Int -- ^ Time left before the gift can be used for crafting
    }
  | CraftGiftResultInvalidGift -- ^ Crafting isn't possible because one of the gifts isn't suitable for crafting
  | CraftGiftResultFail -- ^ Crafting has failed
  deriving (CraftGiftResult -> CraftGiftResult -> Bool
(CraftGiftResult -> CraftGiftResult -> Bool)
-> (CraftGiftResult -> CraftGiftResult -> Bool)
-> Eq CraftGiftResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CraftGiftResult -> CraftGiftResult -> Bool
== :: CraftGiftResult -> CraftGiftResult -> Bool
$c/= :: CraftGiftResult -> CraftGiftResult -> Bool
/= :: CraftGiftResult -> CraftGiftResult -> Bool
Eq, Int -> CraftGiftResult -> ShowS
[CraftGiftResult] -> ShowS
CraftGiftResult -> String
(Int -> CraftGiftResult -> ShowS)
-> (CraftGiftResult -> String)
-> ([CraftGiftResult] -> ShowS)
-> Show CraftGiftResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CraftGiftResult -> ShowS
showsPrec :: Int -> CraftGiftResult -> ShowS
$cshow :: CraftGiftResult -> String
show :: CraftGiftResult -> String
$cshowList :: [CraftGiftResult] -> ShowS
showList :: [CraftGiftResult] -> ShowS
Show)

instance I.ShortShow CraftGiftResult where
  shortShow :: CraftGiftResult -> String
shortShow CraftGiftResultSuccess
    { gift :: CraftGiftResult -> Maybe UpgradedGift
gift             = Maybe UpgradedGift
gift_
    , received_gift_id :: CraftGiftResult -> Maybe Text
received_gift_id = Maybe Text
received_gift_id_
    }
      = String
"CraftGiftResultSuccess"
        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_
        , String
"received_gift_id" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
received_gift_id_
        ]
  shortShow CraftGiftResultTooEarly
    { retry_after :: CraftGiftResult -> Maybe Int
retry_after = Maybe Int
retry_after_
    }
      = String
"CraftGiftResultTooEarly"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"retry_after" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
retry_after_
        ]
  shortShow CraftGiftResult
CraftGiftResultInvalidGift
      = String
"CraftGiftResultInvalidGift"
  shortShow CraftGiftResult
CraftGiftResultFail
      = String
"CraftGiftResultFail"

instance AT.FromJSON CraftGiftResult where
  parseJSON :: Value -> Parser CraftGiftResult
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
"craftGiftResultSuccess"     -> Value -> Parser CraftGiftResult
parseCraftGiftResultSuccess Value
v
      String
"craftGiftResultTooEarly"    -> Value -> Parser CraftGiftResult
parseCraftGiftResultTooEarly Value
v
      String
"craftGiftResultInvalidGift" -> CraftGiftResult -> Parser CraftGiftResult
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CraftGiftResult
CraftGiftResultInvalidGift
      String
"craftGiftResultFail"        -> CraftGiftResult -> Parser CraftGiftResult
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CraftGiftResult
CraftGiftResultFail
      String
_                            -> Parser CraftGiftResult
forall a. Monoid a => a
mempty
    
    where
      parseCraftGiftResultSuccess :: A.Value -> AT.Parser CraftGiftResult
      parseCraftGiftResultSuccess :: Value -> Parser CraftGiftResult
parseCraftGiftResultSuccess = String
-> (Object -> Parser CraftGiftResult)
-> Value
-> Parser CraftGiftResult
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"CraftGiftResultSuccess" ((Object -> Parser CraftGiftResult)
 -> Value -> Parser CraftGiftResult)
-> (Object -> Parser CraftGiftResult)
-> Value
-> Parser CraftGiftResult
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"
        Maybe Text
received_gift_id_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"received_gift_id"
        CraftGiftResult -> Parser CraftGiftResult
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CraftGiftResult -> Parser CraftGiftResult)
-> CraftGiftResult -> Parser CraftGiftResult
forall a b. (a -> b) -> a -> b
$ CraftGiftResultSuccess
          { gift :: Maybe UpgradedGift
gift             = Maybe UpgradedGift
gift_
          , received_gift_id :: Maybe Text
received_gift_id = Maybe Text
received_gift_id_
          }
      parseCraftGiftResultTooEarly :: A.Value -> AT.Parser CraftGiftResult
      parseCraftGiftResultTooEarly :: Value -> Parser CraftGiftResult
parseCraftGiftResultTooEarly = String
-> (Object -> Parser CraftGiftResult)
-> Value
-> Parser CraftGiftResult
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"CraftGiftResultTooEarly" ((Object -> Parser CraftGiftResult)
 -> Value -> Parser CraftGiftResult)
-> (Object -> Parser CraftGiftResult)
-> Value
-> Parser CraftGiftResult
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
retry_after_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"retry_after"
        CraftGiftResult -> Parser CraftGiftResult
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CraftGiftResult -> Parser CraftGiftResult)
-> CraftGiftResult -> Parser CraftGiftResult
forall a b. (a -> b) -> a -> b
$ CraftGiftResultTooEarly
          { retry_after :: Maybe Int
retry_after = Maybe Int
retry_after_
          }
  parseJSON Value
_ = Parser CraftGiftResult
forall a. Monoid a => a
mempty