module TD.Data.PaymentReceiptType
  (PaymentReceiptType(..)) 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.Invoice as Invoice
import qualified TD.Data.OrderInfo as OrderInfo
import qualified TD.Data.ShippingOption as ShippingOption
import qualified Data.Text as T

-- | Describes type of successful payment
data PaymentReceiptType
  = PaymentReceiptTypeRegular -- ^ The payment was done using a third-party payment provider
    { PaymentReceiptType -> Maybe Int
payment_provider_user_id :: Maybe Int                           -- ^ User identifier of the payment provider bot
    , PaymentReceiptType -> Maybe Invoice
invoice                  :: Maybe Invoice.Invoice               -- ^ Information about the invoice
    , PaymentReceiptType -> Maybe OrderInfo
order_info               :: Maybe OrderInfo.OrderInfo           -- ^ Order information; may be null
    , PaymentReceiptType -> Maybe ShippingOption
shipping_option          :: Maybe ShippingOption.ShippingOption -- ^ Chosen shipping option; may be null
    , PaymentReceiptType -> Maybe Text
credentials_title        :: Maybe T.Text                        -- ^ Title of the saved credentials chosen by the buyer
    , PaymentReceiptType -> Maybe Int
tip_amount               :: Maybe Int                           -- ^ The amount of tip chosen by the buyer in the smallest units of the currency
    }
  | PaymentReceiptTypeStars -- ^ The payment was done using Telegram Stars
    { PaymentReceiptType -> Maybe Int
star_count     :: Maybe Int    -- ^ Number of Telegram Stars that were paid
    , PaymentReceiptType -> Maybe Text
transaction_id :: Maybe T.Text -- ^ Unique identifier of the transaction that can be used to dispute it
    }
  deriving (PaymentReceiptType -> PaymentReceiptType -> Bool
(PaymentReceiptType -> PaymentReceiptType -> Bool)
-> (PaymentReceiptType -> PaymentReceiptType -> Bool)
-> Eq PaymentReceiptType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PaymentReceiptType -> PaymentReceiptType -> Bool
== :: PaymentReceiptType -> PaymentReceiptType -> Bool
$c/= :: PaymentReceiptType -> PaymentReceiptType -> Bool
/= :: PaymentReceiptType -> PaymentReceiptType -> Bool
Eq, Int -> PaymentReceiptType -> ShowS
[PaymentReceiptType] -> ShowS
PaymentReceiptType -> String
(Int -> PaymentReceiptType -> ShowS)
-> (PaymentReceiptType -> String)
-> ([PaymentReceiptType] -> ShowS)
-> Show PaymentReceiptType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PaymentReceiptType -> ShowS
showsPrec :: Int -> PaymentReceiptType -> ShowS
$cshow :: PaymentReceiptType -> String
show :: PaymentReceiptType -> String
$cshowList :: [PaymentReceiptType] -> ShowS
showList :: [PaymentReceiptType] -> ShowS
Show)

instance I.ShortShow PaymentReceiptType where
  shortShow :: PaymentReceiptType -> String
shortShow PaymentReceiptTypeRegular
    { payment_provider_user_id :: PaymentReceiptType -> Maybe Int
payment_provider_user_id = Maybe Int
payment_provider_user_id_
    , invoice :: PaymentReceiptType -> Maybe Invoice
invoice                  = Maybe Invoice
invoice_
    , order_info :: PaymentReceiptType -> Maybe OrderInfo
order_info               = Maybe OrderInfo
order_info_
    , shipping_option :: PaymentReceiptType -> Maybe ShippingOption
shipping_option          = Maybe ShippingOption
shipping_option_
    , credentials_title :: PaymentReceiptType -> Maybe Text
credentials_title        = Maybe Text
credentials_title_
    , tip_amount :: PaymentReceiptType -> Maybe Int
tip_amount               = Maybe Int
tip_amount_
    }
      = String
"PaymentReceiptTypeRegular"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"payment_provider_user_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
payment_provider_user_id_
        , String
"invoice"                  String -> Maybe Invoice -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Invoice
invoice_
        , String
"order_info"               String -> Maybe OrderInfo -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe OrderInfo
order_info_
        , String
"shipping_option"          String -> Maybe ShippingOption -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ShippingOption
shipping_option_
        , String
"credentials_title"        String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
credentials_title_
        , String
"tip_amount"               String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
tip_amount_
        ]
  shortShow PaymentReceiptTypeStars
    { star_count :: PaymentReceiptType -> Maybe Int
star_count     = Maybe Int
star_count_
    , transaction_id :: PaymentReceiptType -> Maybe Text
transaction_id = Maybe Text
transaction_id_
    }
      = String
"PaymentReceiptTypeStars"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"star_count"     String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
star_count_
        , String
"transaction_id" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
transaction_id_
        ]

instance AT.FromJSON PaymentReceiptType where
  parseJSON :: Value -> Parser PaymentReceiptType
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
"paymentReceiptTypeRegular" -> Value -> Parser PaymentReceiptType
parsePaymentReceiptTypeRegular Value
v
      String
"paymentReceiptTypeStars"   -> Value -> Parser PaymentReceiptType
parsePaymentReceiptTypeStars Value
v
      String
_                           -> Parser PaymentReceiptType
forall a. Monoid a => a
mempty
    
    where
      parsePaymentReceiptTypeRegular :: A.Value -> AT.Parser PaymentReceiptType
      parsePaymentReceiptTypeRegular :: Value -> Parser PaymentReceiptType
parsePaymentReceiptTypeRegular = String
-> (Object -> Parser PaymentReceiptType)
-> Value
-> Parser PaymentReceiptType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PaymentReceiptTypeRegular" ((Object -> Parser PaymentReceiptType)
 -> Value -> Parser PaymentReceiptType)
-> (Object -> Parser PaymentReceiptType)
-> Value
-> Parser PaymentReceiptType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
payment_provider_user_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"payment_provider_user_id"
        Maybe Invoice
invoice_                  <- Object
o Object -> Key -> Parser (Maybe Invoice)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"invoice"
        Maybe OrderInfo
order_info_               <- Object
o Object -> Key -> Parser (Maybe OrderInfo)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"order_info"
        Maybe ShippingOption
shipping_option_          <- Object
o Object -> Key -> Parser (Maybe ShippingOption)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"shipping_option"
        Maybe Text
credentials_title_        <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"credentials_title"
        Maybe Int
tip_amount_               <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"tip_amount"
        PaymentReceiptType -> Parser PaymentReceiptType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PaymentReceiptType -> Parser PaymentReceiptType)
-> PaymentReceiptType -> Parser PaymentReceiptType
forall a b. (a -> b) -> a -> b
$ PaymentReceiptTypeRegular
          { payment_provider_user_id :: Maybe Int
payment_provider_user_id = Maybe Int
payment_provider_user_id_
          , invoice :: Maybe Invoice
invoice                  = Maybe Invoice
invoice_
          , order_info :: Maybe OrderInfo
order_info               = Maybe OrderInfo
order_info_
          , shipping_option :: Maybe ShippingOption
shipping_option          = Maybe ShippingOption
shipping_option_
          , credentials_title :: Maybe Text
credentials_title        = Maybe Text
credentials_title_
          , tip_amount :: Maybe Int
tip_amount               = Maybe Int
tip_amount_
          }
      parsePaymentReceiptTypeStars :: A.Value -> AT.Parser PaymentReceiptType
      parsePaymentReceiptTypeStars :: Value -> Parser PaymentReceiptType
parsePaymentReceiptTypeStars = String
-> (Object -> Parser PaymentReceiptType)
-> Value
-> Parser PaymentReceiptType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PaymentReceiptTypeStars" ((Object -> Parser PaymentReceiptType)
 -> Value -> Parser PaymentReceiptType)
-> (Object -> Parser PaymentReceiptType)
-> Value
-> Parser PaymentReceiptType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
star_count_     <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"star_count"
        Maybe Text
transaction_id_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"transaction_id"
        PaymentReceiptType -> Parser PaymentReceiptType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PaymentReceiptType -> Parser PaymentReceiptType)
-> PaymentReceiptType -> Parser PaymentReceiptType
forall a b. (a -> b) -> a -> b
$ PaymentReceiptTypeStars
          { star_count :: Maybe Int
star_count     = Maybe Int
star_count_
          , transaction_id :: Maybe Text
transaction_id = Maybe Text
transaction_id_
          }
  parseJSON Value
_ = Parser PaymentReceiptType
forall a. Monoid a => a
mempty