module TD.Data.ChatTransactionPurpose
  (ChatTransactionPurpose(..)) 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

-- | Describes purpose of a transaction with a supergroup or a channel
data ChatTransactionPurpose
  = ChatTransactionPurposePaidMedia -- ^ Paid media were bought
    { ChatTransactionPurpose -> Maybe Int
message_id :: Maybe Int                   -- ^ Identifier of the corresponding message with paid media; can be 0 or an identifier of a deleted message
    , ChatTransactionPurpose -> Maybe [PaidMedia]
media      :: Maybe [PaidMedia.PaidMedia] -- ^ The bought media if the transaction wasn't refunded
    }
  | ChatTransactionPurposeJoin -- ^ User joined the channel and subscribed to regular payments in Telegram Stars
    { ChatTransactionPurpose -> Maybe Int
period :: Maybe Int -- ^ The number of seconds between consecutive Telegram Star debiting
    }
  | ChatTransactionPurposeReaction -- ^ User paid for a reaction
    { message_id :: Maybe Int -- ^ Identifier of the reacted message; can be 0 or an identifier of a deleted message
    }
  | ChatTransactionPurposeGiveaway -- ^ User received Telegram Stars from a giveaway
    { ChatTransactionPurpose -> Maybe Int
giveaway_message_id :: Maybe Int -- ^ Identifier of the message with giveaway; can be 0 or an identifier of a deleted message
    }
  deriving (ChatTransactionPurpose -> ChatTransactionPurpose -> Bool
(ChatTransactionPurpose -> ChatTransactionPurpose -> Bool)
-> (ChatTransactionPurpose -> ChatTransactionPurpose -> Bool)
-> Eq ChatTransactionPurpose
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChatTransactionPurpose -> ChatTransactionPurpose -> Bool
== :: ChatTransactionPurpose -> ChatTransactionPurpose -> Bool
$c/= :: ChatTransactionPurpose -> ChatTransactionPurpose -> Bool
/= :: ChatTransactionPurpose -> ChatTransactionPurpose -> Bool
Eq, Int -> ChatTransactionPurpose -> ShowS
[ChatTransactionPurpose] -> ShowS
ChatTransactionPurpose -> String
(Int -> ChatTransactionPurpose -> ShowS)
-> (ChatTransactionPurpose -> String)
-> ([ChatTransactionPurpose] -> ShowS)
-> Show ChatTransactionPurpose
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChatTransactionPurpose -> ShowS
showsPrec :: Int -> ChatTransactionPurpose -> ShowS
$cshow :: ChatTransactionPurpose -> String
show :: ChatTransactionPurpose -> String
$cshowList :: [ChatTransactionPurpose] -> ShowS
showList :: [ChatTransactionPurpose] -> ShowS
Show)

instance I.ShortShow ChatTransactionPurpose where
  shortShow :: ChatTransactionPurpose -> String
shortShow ChatTransactionPurposePaidMedia
    { message_id :: ChatTransactionPurpose -> Maybe Int
message_id = Maybe Int
message_id_
    , media :: ChatTransactionPurpose -> Maybe [PaidMedia]
media      = Maybe [PaidMedia]
media_
    }
      = String
"ChatTransactionPurposePaidMedia"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"message_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
message_id_
        , String
"media"      String -> Maybe [PaidMedia] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [PaidMedia]
media_
        ]
  shortShow ChatTransactionPurposeJoin
    { period :: ChatTransactionPurpose -> Maybe Int
period = Maybe Int
period_
    }
      = String
"ChatTransactionPurposeJoin"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"period" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
period_
        ]
  shortShow ChatTransactionPurposeReaction
    { message_id :: ChatTransactionPurpose -> Maybe Int
message_id = Maybe Int
message_id_
    }
      = String
"ChatTransactionPurposeReaction"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"message_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
message_id_
        ]
  shortShow ChatTransactionPurposeGiveaway
    { giveaway_message_id :: ChatTransactionPurpose -> Maybe Int
giveaway_message_id = Maybe Int
giveaway_message_id_
    }
      = String
"ChatTransactionPurposeGiveaway"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"giveaway_message_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
giveaway_message_id_
        ]

instance AT.FromJSON ChatTransactionPurpose where
  parseJSON :: Value -> Parser ChatTransactionPurpose
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
"chatTransactionPurposePaidMedia" -> Value -> Parser ChatTransactionPurpose
parseChatTransactionPurposePaidMedia Value
v
      String
"chatTransactionPurposeJoin"      -> Value -> Parser ChatTransactionPurpose
parseChatTransactionPurposeJoin Value
v
      String
"chatTransactionPurposeReaction"  -> Value -> Parser ChatTransactionPurpose
parseChatTransactionPurposeReaction Value
v
      String
"chatTransactionPurposeGiveaway"  -> Value -> Parser ChatTransactionPurpose
parseChatTransactionPurposeGiveaway Value
v
      String
_                                 -> Parser ChatTransactionPurpose
forall a. Monoid a => a
mempty
    
    where
      parseChatTransactionPurposePaidMedia :: A.Value -> AT.Parser ChatTransactionPurpose
      parseChatTransactionPurposePaidMedia :: Value -> Parser ChatTransactionPurpose
parseChatTransactionPurposePaidMedia = String
-> (Object -> Parser ChatTransactionPurpose)
-> Value
-> Parser ChatTransactionPurpose
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatTransactionPurposePaidMedia" ((Object -> Parser ChatTransactionPurpose)
 -> Value -> Parser ChatTransactionPurpose)
-> (Object -> Parser ChatTransactionPurpose)
-> Value
-> Parser ChatTransactionPurpose
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
message_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"message_id"
        Maybe [PaidMedia]
media_      <- Object
o Object -> Key -> Parser (Maybe [PaidMedia])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"media"
        ChatTransactionPurpose -> Parser ChatTransactionPurpose
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatTransactionPurpose -> Parser ChatTransactionPurpose)
-> ChatTransactionPurpose -> Parser ChatTransactionPurpose
forall a b. (a -> b) -> a -> b
$ ChatTransactionPurposePaidMedia
          { message_id :: Maybe Int
message_id = Maybe Int
message_id_
          , media :: Maybe [PaidMedia]
media      = Maybe [PaidMedia]
media_
          }
      parseChatTransactionPurposeJoin :: A.Value -> AT.Parser ChatTransactionPurpose
      parseChatTransactionPurposeJoin :: Value -> Parser ChatTransactionPurpose
parseChatTransactionPurposeJoin = String
-> (Object -> Parser ChatTransactionPurpose)
-> Value
-> Parser ChatTransactionPurpose
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatTransactionPurposeJoin" ((Object -> Parser ChatTransactionPurpose)
 -> Value -> Parser ChatTransactionPurpose)
-> (Object -> Parser ChatTransactionPurpose)
-> Value
-> Parser ChatTransactionPurpose
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
period_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"period"
        ChatTransactionPurpose -> Parser ChatTransactionPurpose
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatTransactionPurpose -> Parser ChatTransactionPurpose)
-> ChatTransactionPurpose -> Parser ChatTransactionPurpose
forall a b. (a -> b) -> a -> b
$ ChatTransactionPurposeJoin
          { period :: Maybe Int
period = Maybe Int
period_
          }
      parseChatTransactionPurposeReaction :: A.Value -> AT.Parser ChatTransactionPurpose
      parseChatTransactionPurposeReaction :: Value -> Parser ChatTransactionPurpose
parseChatTransactionPurposeReaction = String
-> (Object -> Parser ChatTransactionPurpose)
-> Value
-> Parser ChatTransactionPurpose
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatTransactionPurposeReaction" ((Object -> Parser ChatTransactionPurpose)
 -> Value -> Parser ChatTransactionPurpose)
-> (Object -> Parser ChatTransactionPurpose)
-> Value
-> Parser ChatTransactionPurpose
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
message_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"message_id"
        ChatTransactionPurpose -> Parser ChatTransactionPurpose
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatTransactionPurpose -> Parser ChatTransactionPurpose)
-> ChatTransactionPurpose -> Parser ChatTransactionPurpose
forall a b. (a -> b) -> a -> b
$ ChatTransactionPurposeReaction
          { message_id :: Maybe Int
message_id = Maybe Int
message_id_
          }
      parseChatTransactionPurposeGiveaway :: A.Value -> AT.Parser ChatTransactionPurpose
      parseChatTransactionPurposeGiveaway :: Value -> Parser ChatTransactionPurpose
parseChatTransactionPurposeGiveaway = String
-> (Object -> Parser ChatTransactionPurpose)
-> Value
-> Parser ChatTransactionPurpose
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatTransactionPurposeGiveaway" ((Object -> Parser ChatTransactionPurpose)
 -> Value -> Parser ChatTransactionPurpose)
-> (Object -> Parser ChatTransactionPurpose)
-> Value
-> Parser ChatTransactionPurpose
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
giveaway_message_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"giveaway_message_id"
        ChatTransactionPurpose -> Parser ChatTransactionPurpose
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatTransactionPurpose -> Parser ChatTransactionPurpose)
-> ChatTransactionPurpose -> Parser ChatTransactionPurpose
forall a b. (a -> b) -> a -> b
$ ChatTransactionPurposeGiveaway
          { giveaway_message_id :: Maybe Int
giveaway_message_id = Maybe Int
giveaway_message_id_
          }
  parseJSON Value
_ = Parser ChatTransactionPurpose
forall a. Monoid a => a
mempty