module TD.Data.PaymentForm
  (PaymentForm(..)) 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.PaymentFormType as PaymentFormType
import qualified TD.Data.ProductInfo as ProductInfo

data PaymentForm
  = PaymentForm -- ^ Contains information about an invoice payment form
    { PaymentForm -> Maybe Int
_id                :: Maybe Int                             -- ^ The payment form identifier
    , PaymentForm -> Maybe PaymentFormType
_type              :: Maybe PaymentFormType.PaymentFormType -- ^ Type of the payment form
    , PaymentForm -> Maybe Int
seller_bot_user_id :: Maybe Int                             -- ^ User identifier of the seller bot
    , PaymentForm -> Maybe ProductInfo
product_info       :: Maybe ProductInfo.ProductInfo         -- ^ Information about the product
    }
  deriving (PaymentForm -> PaymentForm -> Bool
(PaymentForm -> PaymentForm -> Bool)
-> (PaymentForm -> PaymentForm -> Bool) -> Eq PaymentForm
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PaymentForm -> PaymentForm -> Bool
== :: PaymentForm -> PaymentForm -> Bool
$c/= :: PaymentForm -> PaymentForm -> Bool
/= :: PaymentForm -> PaymentForm -> Bool
Eq, Int -> PaymentForm -> ShowS
[PaymentForm] -> ShowS
PaymentForm -> String
(Int -> PaymentForm -> ShowS)
-> (PaymentForm -> String)
-> ([PaymentForm] -> ShowS)
-> Show PaymentForm
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PaymentForm -> ShowS
showsPrec :: Int -> PaymentForm -> ShowS
$cshow :: PaymentForm -> String
show :: PaymentForm -> String
$cshowList :: [PaymentForm] -> ShowS
showList :: [PaymentForm] -> ShowS
Show)

instance I.ShortShow PaymentForm where
  shortShow :: PaymentForm -> String
shortShow PaymentForm
    { _id :: PaymentForm -> Maybe Int
_id                = Maybe Int
_id_
    , _type :: PaymentForm -> Maybe PaymentFormType
_type              = Maybe PaymentFormType
_type_
    , seller_bot_user_id :: PaymentForm -> Maybe Int
seller_bot_user_id = Maybe Int
seller_bot_user_id_
    , product_info :: PaymentForm -> Maybe ProductInfo
product_info       = Maybe ProductInfo
product_info_
    }
      = String
"PaymentForm"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"                String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_id_
        , String
"_type"              String -> Maybe PaymentFormType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe PaymentFormType
_type_
        , String
"seller_bot_user_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
seller_bot_user_id_
        , String
"product_info"       String -> Maybe ProductInfo -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ProductInfo
product_info_
        ]

instance AT.FromJSON PaymentForm where
  parseJSON :: Value -> Parser PaymentForm
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
"paymentForm" -> Value -> Parser PaymentForm
parsePaymentForm Value
v
      String
_             -> Parser PaymentForm
forall a. Monoid a => a
mempty
    
    where
      parsePaymentForm :: A.Value -> AT.Parser PaymentForm
      parsePaymentForm :: Value -> Parser PaymentForm
parsePaymentForm = String
-> (Object -> Parser PaymentForm) -> Value -> Parser PaymentForm
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PaymentForm" ((Object -> Parser PaymentForm) -> Value -> Parser PaymentForm)
-> (Object -> Parser PaymentForm) -> Value -> Parser PaymentForm
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_                <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
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
"id"
        Maybe PaymentFormType
_type_              <- Object
o Object -> Key -> Parser (Maybe PaymentFormType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"type"
        Maybe Int
seller_bot_user_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"seller_bot_user_id"
        Maybe ProductInfo
product_info_       <- Object
o Object -> Key -> Parser (Maybe ProductInfo)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"product_info"
        PaymentForm -> Parser PaymentForm
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PaymentForm -> Parser PaymentForm)
-> PaymentForm -> Parser PaymentForm
forall a b. (a -> b) -> a -> b
$ PaymentForm
          { _id :: Maybe Int
_id                = Maybe Int
_id_
          , _type :: Maybe PaymentFormType
_type              = Maybe PaymentFormType
_type_
          , seller_bot_user_id :: Maybe Int
seller_bot_user_id = Maybe Int
seller_bot_user_id_
          , product_info :: Maybe ProductInfo
product_info       = Maybe ProductInfo
product_info_
          }
  parseJSON Value
_ = Parser PaymentForm
forall a. Monoid a => a
mempty