module TD.Data.BotTransactionPurpose
  (BotTransactionPurpose(..)) 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.PaidMedia as PaidMedia
import qualified Data.Text as T
import qualified TD.Data.ProductInfo as ProductInfo
import qualified Data.ByteString as BS

-- | Describes purpose of a transaction with a bot
data BotTransactionPurpose
  = BotTransactionPurposePaidMedia -- ^ Paid media were bought
    { BotTransactionPurpose -> Maybe [PaidMedia]
media   :: Maybe [PaidMedia.PaidMedia] -- ^ The bought media if the transaction wasn't refunded
    , BotTransactionPurpose -> Maybe Text
payload :: Maybe T.Text                -- ^ Bot-provided payload; for bots only
    }
  | BotTransactionPurposeInvoicePayment -- ^ User bought a product from the bot
    { BotTransactionPurpose -> Maybe ProductInfo
product_info    :: Maybe ProductInfo.ProductInfo -- ^ Information about the bought product; may be null if not applicable
    , BotTransactionPurpose -> Maybe ByteString
invoice_payload :: Maybe BS.ByteString           -- ^ Invoice payload; for bots only
    }
  deriving (BotTransactionPurpose -> BotTransactionPurpose -> Bool
(BotTransactionPurpose -> BotTransactionPurpose -> Bool)
-> (BotTransactionPurpose -> BotTransactionPurpose -> Bool)
-> Eq BotTransactionPurpose
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BotTransactionPurpose -> BotTransactionPurpose -> Bool
== :: BotTransactionPurpose -> BotTransactionPurpose -> Bool
$c/= :: BotTransactionPurpose -> BotTransactionPurpose -> Bool
/= :: BotTransactionPurpose -> BotTransactionPurpose -> Bool
Eq, Int -> BotTransactionPurpose -> ShowS
[BotTransactionPurpose] -> ShowS
BotTransactionPurpose -> String
(Int -> BotTransactionPurpose -> ShowS)
-> (BotTransactionPurpose -> String)
-> ([BotTransactionPurpose] -> ShowS)
-> Show BotTransactionPurpose
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BotTransactionPurpose -> ShowS
showsPrec :: Int -> BotTransactionPurpose -> ShowS
$cshow :: BotTransactionPurpose -> String
show :: BotTransactionPurpose -> String
$cshowList :: [BotTransactionPurpose] -> ShowS
showList :: [BotTransactionPurpose] -> ShowS
Show)

instance I.ShortShow BotTransactionPurpose where
  shortShow :: BotTransactionPurpose -> String
shortShow BotTransactionPurposePaidMedia
    { media :: BotTransactionPurpose -> Maybe [PaidMedia]
media   = Maybe [PaidMedia]
media_
    , payload :: BotTransactionPurpose -> Maybe Text
payload = Maybe Text
payload_
    }
      = String
"BotTransactionPurposePaidMedia"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"media"   String -> Maybe [PaidMedia] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [PaidMedia]
media_
        , String
"payload" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
payload_
        ]
  shortShow BotTransactionPurposeInvoicePayment
    { product_info :: BotTransactionPurpose -> Maybe ProductInfo
product_info    = Maybe ProductInfo
product_info_
    , invoice_payload :: BotTransactionPurpose -> Maybe ByteString
invoice_payload = Maybe ByteString
invoice_payload_
    }
      = String
"BotTransactionPurposeInvoicePayment"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"product_info"    String -> Maybe ProductInfo -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ProductInfo
product_info_
        , String
"invoice_payload" String -> Maybe ByteString -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ByteString
invoice_payload_
        ]

instance AT.FromJSON BotTransactionPurpose where
  parseJSON :: Value -> Parser BotTransactionPurpose
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
"botTransactionPurposePaidMedia"      -> Value -> Parser BotTransactionPurpose
parseBotTransactionPurposePaidMedia Value
v
      String
"botTransactionPurposeInvoicePayment" -> Value -> Parser BotTransactionPurpose
parseBotTransactionPurposeInvoicePayment Value
v
      String
_                                     -> Parser BotTransactionPurpose
forall a. Monoid a => a
mempty
    
    where
      parseBotTransactionPurposePaidMedia :: A.Value -> AT.Parser BotTransactionPurpose
      parseBotTransactionPurposePaidMedia :: Value -> Parser BotTransactionPurpose
parseBotTransactionPurposePaidMedia = String
-> (Object -> Parser BotTransactionPurpose)
-> Value
-> Parser BotTransactionPurpose
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BotTransactionPurposePaidMedia" ((Object -> Parser BotTransactionPurpose)
 -> Value -> Parser BotTransactionPurpose)
-> (Object -> Parser BotTransactionPurpose)
-> Value
-> Parser BotTransactionPurpose
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [PaidMedia]
media_   <- Object
o Object -> Key -> Parser (Maybe [PaidMedia])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"media"
        Maybe Text
payload_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"payload"
        BotTransactionPurpose -> Parser BotTransactionPurpose
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BotTransactionPurpose -> Parser BotTransactionPurpose)
-> BotTransactionPurpose -> Parser BotTransactionPurpose
forall a b. (a -> b) -> a -> b
$ BotTransactionPurposePaidMedia
          { media :: Maybe [PaidMedia]
media   = Maybe [PaidMedia]
media_
          , payload :: Maybe Text
payload = Maybe Text
payload_
          }
      parseBotTransactionPurposeInvoicePayment :: A.Value -> AT.Parser BotTransactionPurpose
      parseBotTransactionPurposeInvoicePayment :: Value -> Parser BotTransactionPurpose
parseBotTransactionPurposeInvoicePayment = String
-> (Object -> Parser BotTransactionPurpose)
-> Value
-> Parser BotTransactionPurpose
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BotTransactionPurposeInvoicePayment" ((Object -> Parser BotTransactionPurpose)
 -> Value -> Parser BotTransactionPurpose)
-> (Object -> Parser BotTransactionPurpose)
-> Value
-> Parser BotTransactionPurpose
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe ProductInfo
product_info_    <- Object
o Object -> Key -> Parser (Maybe ProductInfo)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"product_info"
        Maybe ByteString
invoice_payload_ <- (String -> ByteString) -> Maybe String -> Maybe ByteString
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> ByteString
I.readBytes (Maybe String -> Maybe ByteString)
-> Parser (Maybe String) -> Parser (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"invoice_payload"
        BotTransactionPurpose -> Parser BotTransactionPurpose
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BotTransactionPurpose -> Parser BotTransactionPurpose)
-> BotTransactionPurpose -> Parser BotTransactionPurpose
forall a b. (a -> b) -> a -> b
$ BotTransactionPurposeInvoicePayment
          { product_info :: Maybe ProductInfo
product_info    = Maybe ProductInfo
product_info_
          , invoice_payload :: Maybe ByteString
invoice_payload = Maybe ByteString
invoice_payload_
          }
  parseJSON Value
_ = Parser BotTransactionPurpose
forall a. Monoid a => a
mempty