module TD.Data.GiftResaleResult
  (GiftResaleResult(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.Text as T
import qualified TD.Data.GiftResalePrice as GiftResalePrice

-- | Describes result of sending a resold gift
data GiftResaleResult
  = GiftResaleResultOk -- ^ Operation was successfully completed
    { GiftResaleResult -> Maybe Text
received_gift_id :: Maybe T.Text -- ^ Unique identifier of the received gift; only for the gifts sent to the current user
    }
  | GiftResaleResultPriceIncreased -- ^ Operation has failed, because price has increased. If the price has decreased, then the buying will succeed anyway
    { GiftResaleResult -> Maybe GiftResalePrice
price :: Maybe GiftResalePrice.GiftResalePrice -- ^ New price for the gift
    }
  deriving (GiftResaleResult -> GiftResaleResult -> Bool
(GiftResaleResult -> GiftResaleResult -> Bool)
-> (GiftResaleResult -> GiftResaleResult -> Bool)
-> Eq GiftResaleResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GiftResaleResult -> GiftResaleResult -> Bool
== :: GiftResaleResult -> GiftResaleResult -> Bool
$c/= :: GiftResaleResult -> GiftResaleResult -> Bool
/= :: GiftResaleResult -> GiftResaleResult -> Bool
Eq, Int -> GiftResaleResult -> ShowS
[GiftResaleResult] -> ShowS
GiftResaleResult -> String
(Int -> GiftResaleResult -> ShowS)
-> (GiftResaleResult -> String)
-> ([GiftResaleResult] -> ShowS)
-> Show GiftResaleResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GiftResaleResult -> ShowS
showsPrec :: Int -> GiftResaleResult -> ShowS
$cshow :: GiftResaleResult -> String
show :: GiftResaleResult -> String
$cshowList :: [GiftResaleResult] -> ShowS
showList :: [GiftResaleResult] -> ShowS
Show)

instance I.ShortShow GiftResaleResult where
  shortShow :: GiftResaleResult -> String
shortShow GiftResaleResultOk
    { received_gift_id :: GiftResaleResult -> Maybe Text
received_gift_id = Maybe Text
received_gift_id_
    }
      = String
"GiftResaleResultOk"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"received_gift_id" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
received_gift_id_
        ]
  shortShow GiftResaleResultPriceIncreased
    { price :: GiftResaleResult -> Maybe GiftResalePrice
price = Maybe GiftResalePrice
price_
    }
      = String
"GiftResaleResultPriceIncreased"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"price" String -> Maybe GiftResalePrice -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe GiftResalePrice
price_
        ]

instance AT.FromJSON GiftResaleResult where
  parseJSON :: Value -> Parser GiftResaleResult
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
"giftResaleResultOk"             -> Value -> Parser GiftResaleResult
parseGiftResaleResultOk Value
v
      String
"giftResaleResultPriceIncreased" -> Value -> Parser GiftResaleResult
parseGiftResaleResultPriceIncreased Value
v
      String
_                                -> Parser GiftResaleResult
forall a. Monoid a => a
mempty
    
    where
      parseGiftResaleResultOk :: A.Value -> AT.Parser GiftResaleResult
      parseGiftResaleResultOk :: Value -> Parser GiftResaleResult
parseGiftResaleResultOk = String
-> (Object -> Parser GiftResaleResult)
-> Value
-> Parser GiftResaleResult
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"GiftResaleResultOk" ((Object -> Parser GiftResaleResult)
 -> Value -> Parser GiftResaleResult)
-> (Object -> Parser GiftResaleResult)
-> Value
-> Parser GiftResaleResult
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        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"
        GiftResaleResult -> Parser GiftResaleResult
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GiftResaleResult -> Parser GiftResaleResult)
-> GiftResaleResult -> Parser GiftResaleResult
forall a b. (a -> b) -> a -> b
$ GiftResaleResultOk
          { received_gift_id :: Maybe Text
received_gift_id = Maybe Text
received_gift_id_
          }
      parseGiftResaleResultPriceIncreased :: A.Value -> AT.Parser GiftResaleResult
      parseGiftResaleResultPriceIncreased :: Value -> Parser GiftResaleResult
parseGiftResaleResultPriceIncreased = String
-> (Object -> Parser GiftResaleResult)
-> Value
-> Parser GiftResaleResult
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"GiftResaleResultPriceIncreased" ((Object -> Parser GiftResaleResult)
 -> Value -> Parser GiftResaleResult)
-> (Object -> Parser GiftResaleResult)
-> Value
-> Parser GiftResaleResult
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe GiftResalePrice
price_ <- Object
o Object -> Key -> Parser (Maybe GiftResalePrice)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"price"
        GiftResaleResult -> Parser GiftResaleResult
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GiftResaleResult -> Parser GiftResaleResult)
-> GiftResaleResult -> Parser GiftResaleResult
forall a b. (a -> b) -> a -> b
$ GiftResaleResultPriceIncreased
          { price :: Maybe GiftResalePrice
price = Maybe GiftResalePrice
price_
          }
  parseJSON Value
_ = Parser GiftResaleResult
forall a. Monoid a => a
mempty