module TD.Data.UserTransactionPurpose
  (UserTransactionPurpose(..)) 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.Sticker as Sticker
import qualified TD.Data.Gift as Gift

-- | Describes purpose of a transaction with a user
data UserTransactionPurpose
  = UserTransactionPurposeGiftedStars -- ^ A user gifted Telegram Stars
    { UserTransactionPurpose -> Maybe Sticker
sticker :: Maybe Sticker.Sticker -- ^ A sticker to be shown in the transaction information; may be null if unknown
    }
  | UserTransactionPurposeGiftSell -- ^ The current user sold a gift received from another user
    { UserTransactionPurpose -> Maybe Gift
gift :: Maybe Gift.Gift -- ^ The gift
    }
  | UserTransactionPurposeGiftSend -- ^ The current user sent a gift to another user
    { gift :: Maybe Gift.Gift -- ^ The gift
    }
  deriving (UserTransactionPurpose -> UserTransactionPurpose -> Bool
(UserTransactionPurpose -> UserTransactionPurpose -> Bool)
-> (UserTransactionPurpose -> UserTransactionPurpose -> Bool)
-> Eq UserTransactionPurpose
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UserTransactionPurpose -> UserTransactionPurpose -> Bool
== :: UserTransactionPurpose -> UserTransactionPurpose -> Bool
$c/= :: UserTransactionPurpose -> UserTransactionPurpose -> Bool
/= :: UserTransactionPurpose -> UserTransactionPurpose -> Bool
Eq, Int -> UserTransactionPurpose -> ShowS
[UserTransactionPurpose] -> ShowS
UserTransactionPurpose -> String
(Int -> UserTransactionPurpose -> ShowS)
-> (UserTransactionPurpose -> String)
-> ([UserTransactionPurpose] -> ShowS)
-> Show UserTransactionPurpose
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UserTransactionPurpose -> ShowS
showsPrec :: Int -> UserTransactionPurpose -> ShowS
$cshow :: UserTransactionPurpose -> String
show :: UserTransactionPurpose -> String
$cshowList :: [UserTransactionPurpose] -> ShowS
showList :: [UserTransactionPurpose] -> ShowS
Show)

instance I.ShortShow UserTransactionPurpose where
  shortShow :: UserTransactionPurpose -> String
shortShow UserTransactionPurposeGiftedStars
    { sticker :: UserTransactionPurpose -> Maybe Sticker
sticker = Maybe Sticker
sticker_
    }
      = String
"UserTransactionPurposeGiftedStars"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"sticker" String -> Maybe Sticker -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Sticker
sticker_
        ]
  shortShow UserTransactionPurposeGiftSell
    { gift :: UserTransactionPurpose -> Maybe Gift
gift = Maybe Gift
gift_
    }
      = String
"UserTransactionPurposeGiftSell"
        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 UserTransactionPurposeGiftSend
    { gift :: UserTransactionPurpose -> Maybe Gift
gift = Maybe Gift
gift_
    }
      = String
"UserTransactionPurposeGiftSend"
        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_
        ]

instance AT.FromJSON UserTransactionPurpose where
  parseJSON :: Value -> Parser UserTransactionPurpose
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
"userTransactionPurposeGiftedStars" -> Value -> Parser UserTransactionPurpose
parseUserTransactionPurposeGiftedStars Value
v
      String
"userTransactionPurposeGiftSell"    -> Value -> Parser UserTransactionPurpose
parseUserTransactionPurposeGiftSell Value
v
      String
"userTransactionPurposeGiftSend"    -> Value -> Parser UserTransactionPurpose
parseUserTransactionPurposeGiftSend Value
v
      String
_                                   -> Parser UserTransactionPurpose
forall a. Monoid a => a
mempty
    
    where
      parseUserTransactionPurposeGiftedStars :: A.Value -> AT.Parser UserTransactionPurpose
      parseUserTransactionPurposeGiftedStars :: Value -> Parser UserTransactionPurpose
parseUserTransactionPurposeGiftedStars = String
-> (Object -> Parser UserTransactionPurpose)
-> Value
-> Parser UserTransactionPurpose
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"UserTransactionPurposeGiftedStars" ((Object -> Parser UserTransactionPurpose)
 -> Value -> Parser UserTransactionPurpose)
-> (Object -> Parser UserTransactionPurpose)
-> Value
-> Parser UserTransactionPurpose
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Sticker
sticker_ <- Object
o Object -> Key -> Parser (Maybe Sticker)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"sticker"
        UserTransactionPurpose -> Parser UserTransactionPurpose
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (UserTransactionPurpose -> Parser UserTransactionPurpose)
-> UserTransactionPurpose -> Parser UserTransactionPurpose
forall a b. (a -> b) -> a -> b
$ UserTransactionPurposeGiftedStars
          { sticker :: Maybe Sticker
sticker = Maybe Sticker
sticker_
          }
      parseUserTransactionPurposeGiftSell :: A.Value -> AT.Parser UserTransactionPurpose
      parseUserTransactionPurposeGiftSell :: Value -> Parser UserTransactionPurpose
parseUserTransactionPurposeGiftSell = String
-> (Object -> Parser UserTransactionPurpose)
-> Value
-> Parser UserTransactionPurpose
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"UserTransactionPurposeGiftSell" ((Object -> Parser UserTransactionPurpose)
 -> Value -> Parser UserTransactionPurpose)
-> (Object -> Parser UserTransactionPurpose)
-> Value
-> Parser UserTransactionPurpose
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"
        UserTransactionPurpose -> Parser UserTransactionPurpose
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (UserTransactionPurpose -> Parser UserTransactionPurpose)
-> UserTransactionPurpose -> Parser UserTransactionPurpose
forall a b. (a -> b) -> a -> b
$ UserTransactionPurposeGiftSell
          { gift :: Maybe Gift
gift = Maybe Gift
gift_
          }
      parseUserTransactionPurposeGiftSend :: A.Value -> AT.Parser UserTransactionPurpose
      parseUserTransactionPurposeGiftSend :: Value -> Parser UserTransactionPurpose
parseUserTransactionPurposeGiftSend = String
-> (Object -> Parser UserTransactionPurpose)
-> Value
-> Parser UserTransactionPurpose
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"UserTransactionPurposeGiftSend" ((Object -> Parser UserTransactionPurpose)
 -> Value -> Parser UserTransactionPurpose)
-> (Object -> Parser UserTransactionPurpose)
-> Value
-> Parser UserTransactionPurpose
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"
        UserTransactionPurpose -> Parser UserTransactionPurpose
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (UserTransactionPurpose -> Parser UserTransactionPurpose)
-> UserTransactionPurpose -> Parser UserTransactionPurpose
forall a b. (a -> b) -> a -> b
$ UserTransactionPurposeGiftSend
          { gift :: Maybe Gift
gift = Maybe Gift
gift_
          }
  parseJSON Value
_ = Parser UserTransactionPurpose
forall a. Monoid a => a
mempty