module TD.Data.InputInvoice
  (InputInvoice(..)) 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.TelegramPaymentPurpose as TelegramPaymentPurpose

-- | Describes an invoice to process
data InputInvoice
  = InputInvoiceMessage -- ^ An invoice from a message of the type messageInvoice or paid media purchase from messagePaidMedia
    { InputInvoice -> Maybe Int
chat_id    :: Maybe Int -- ^ Chat identifier of the message
    , InputInvoice -> Maybe Int
message_id :: Maybe Int -- ^ Message identifier. Use messageProperties.can_be_paid to check whether the message can be used in the method
    }
  | InputInvoiceName -- ^ An invoice from a link of the type internalLinkTypeInvoice
    { InputInvoice -> Maybe Text
name :: Maybe T.Text -- ^ Name of the invoice
    }
  | InputInvoiceTelegram -- ^ An invoice for a payment toward Telegram; must not be used in the in-store apps
    { InputInvoice -> Maybe TelegramPaymentPurpose
purpose :: Maybe TelegramPaymentPurpose.TelegramPaymentPurpose -- ^ Transaction purpose
    }
  deriving (InputInvoice -> InputInvoice -> Bool
(InputInvoice -> InputInvoice -> Bool)
-> (InputInvoice -> InputInvoice -> Bool) -> Eq InputInvoice
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InputInvoice -> InputInvoice -> Bool
== :: InputInvoice -> InputInvoice -> Bool
$c/= :: InputInvoice -> InputInvoice -> Bool
/= :: InputInvoice -> InputInvoice -> Bool
Eq, Int -> InputInvoice -> ShowS
[InputInvoice] -> ShowS
InputInvoice -> String
(Int -> InputInvoice -> ShowS)
-> (InputInvoice -> String)
-> ([InputInvoice] -> ShowS)
-> Show InputInvoice
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InputInvoice -> ShowS
showsPrec :: Int -> InputInvoice -> ShowS
$cshow :: InputInvoice -> String
show :: InputInvoice -> String
$cshowList :: [InputInvoice] -> ShowS
showList :: [InputInvoice] -> ShowS
Show)

instance I.ShortShow InputInvoice where
  shortShow :: InputInvoice -> String
shortShow InputInvoiceMessage
    { chat_id :: InputInvoice -> Maybe Int
chat_id    = Maybe Int
chat_id_
    , message_id :: InputInvoice -> Maybe Int
message_id = Maybe Int
message_id_
    }
      = String
"InputInvoiceMessage"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"chat_id"    String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
chat_id_
        , String
"message_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
message_id_
        ]
  shortShow InputInvoiceName
    { name :: InputInvoice -> Maybe Text
name = Maybe Text
name_
    }
      = String
"InputInvoiceName"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"name" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
name_
        ]
  shortShow InputInvoiceTelegram
    { purpose :: InputInvoice -> Maybe TelegramPaymentPurpose
purpose = Maybe TelegramPaymentPurpose
purpose_
    }
      = String
"InputInvoiceTelegram"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"purpose" String -> Maybe TelegramPaymentPurpose -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe TelegramPaymentPurpose
purpose_
        ]

instance AT.FromJSON InputInvoice where
  parseJSON :: Value -> Parser InputInvoice
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
"inputInvoiceMessage"  -> Value -> Parser InputInvoice
parseInputInvoiceMessage Value
v
      String
"inputInvoiceName"     -> Value -> Parser InputInvoice
parseInputInvoiceName Value
v
      String
"inputInvoiceTelegram" -> Value -> Parser InputInvoice
parseInputInvoiceTelegram Value
v
      String
_                      -> Parser InputInvoice
forall a. Monoid a => a
mempty
    
    where
      parseInputInvoiceMessage :: A.Value -> AT.Parser InputInvoice
      parseInputInvoiceMessage :: Value -> Parser InputInvoice
parseInputInvoiceMessage = String
-> (Object -> Parser InputInvoice) -> Value -> Parser InputInvoice
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputInvoiceMessage" ((Object -> Parser InputInvoice) -> Value -> Parser InputInvoice)
-> (Object -> Parser InputInvoice) -> Value -> Parser InputInvoice
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
chat_id_    <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"chat_id"
        Maybe Int
message_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"message_id"
        InputInvoice -> Parser InputInvoice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputInvoice -> Parser InputInvoice)
-> InputInvoice -> Parser InputInvoice
forall a b. (a -> b) -> a -> b
$ InputInvoiceMessage
          { chat_id :: Maybe Int
chat_id    = Maybe Int
chat_id_
          , message_id :: Maybe Int
message_id = Maybe Int
message_id_
          }
      parseInputInvoiceName :: A.Value -> AT.Parser InputInvoice
      parseInputInvoiceName :: Value -> Parser InputInvoice
parseInputInvoiceName = String
-> (Object -> Parser InputInvoice) -> Value -> Parser InputInvoice
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputInvoiceName" ((Object -> Parser InputInvoice) -> Value -> Parser InputInvoice)
-> (Object -> Parser InputInvoice) -> Value -> Parser InputInvoice
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
name_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"name"
        InputInvoice -> Parser InputInvoice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputInvoice -> Parser InputInvoice)
-> InputInvoice -> Parser InputInvoice
forall a b. (a -> b) -> a -> b
$ InputInvoiceName
          { name :: Maybe Text
name = Maybe Text
name_
          }
      parseInputInvoiceTelegram :: A.Value -> AT.Parser InputInvoice
      parseInputInvoiceTelegram :: Value -> Parser InputInvoice
parseInputInvoiceTelegram = String
-> (Object -> Parser InputInvoice) -> Value -> Parser InputInvoice
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputInvoiceTelegram" ((Object -> Parser InputInvoice) -> Value -> Parser InputInvoice)
-> (Object -> Parser InputInvoice) -> Value -> Parser InputInvoice
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe TelegramPaymentPurpose
purpose_ <- Object
o Object -> Key -> Parser (Maybe TelegramPaymentPurpose)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"purpose"
        InputInvoice -> Parser InputInvoice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputInvoice -> Parser InputInvoice)
-> InputInvoice -> Parser InputInvoice
forall a b. (a -> b) -> a -> b
$ InputInvoiceTelegram
          { purpose :: Maybe TelegramPaymentPurpose
purpose = Maybe TelegramPaymentPurpose
purpose_
          }
  parseJSON Value
_ = Parser InputInvoice
forall a. Monoid a => a
mempty

instance AT.ToJSON InputInvoice where
  toJSON :: InputInvoice -> Value
toJSON InputInvoiceMessage
    { chat_id :: InputInvoice -> Maybe Int
chat_id    = Maybe Int
chat_id_
    , message_id :: InputInvoice -> Maybe Int
message_id = Maybe Int
message_id_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"      Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"inputInvoiceMessage"
        , Key
"chat_id"    Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
chat_id_
        , Key
"message_id" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
message_id_
        ]
  toJSON InputInvoiceName
    { name :: InputInvoice -> Maybe Text
name = Maybe Text
name_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"inputInvoiceName"
        , Key
"name"  Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
name_
        ]
  toJSON InputInvoiceTelegram
    { purpose :: InputInvoice -> Maybe TelegramPaymentPurpose
purpose = Maybe TelegramPaymentPurpose
purpose_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"   Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"inputInvoiceTelegram"
        , Key
"purpose" Key -> Maybe TelegramPaymentPurpose -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe TelegramPaymentPurpose
purpose_
        ]