module TD.Data.ChatRevenueTransactionType
  (ChatRevenueTransactionType(..)) 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.RevenueWithdrawalState as RevenueWithdrawalState

-- | Describes type of transaction for revenue earned from sponsored messages in a chat
data ChatRevenueTransactionType
  = ChatRevenueTransactionTypeEarnings -- ^ Describes earnings from sponsored messages in a chat in some time frame
    { ChatRevenueTransactionType -> Maybe Int
start_date :: Maybe Int -- ^ Point in time (Unix timestamp) when the earnings started
    , ChatRevenueTransactionType -> Maybe Int
end_date   :: Maybe Int -- ^ Point in time (Unix timestamp) when the earnings ended
    }
  | ChatRevenueTransactionTypeWithdrawal -- ^ Describes a withdrawal of earnings
    { ChatRevenueTransactionType -> Maybe Int
withdrawal_date :: Maybe Int                                           -- ^ Point in time (Unix timestamp) when the earnings withdrawal started
    , ChatRevenueTransactionType -> Maybe Text
provider        :: Maybe T.Text                                        -- ^ Name of the payment provider
    , ChatRevenueTransactionType -> Maybe RevenueWithdrawalState
state           :: Maybe RevenueWithdrawalState.RevenueWithdrawalState -- ^ State of the withdrawal
    }
  | ChatRevenueTransactionTypeRefund -- ^ Describes a refund for failed withdrawal of earnings
    { ChatRevenueTransactionType -> Maybe Int
refund_date :: Maybe Int    -- ^ Point in time (Unix timestamp) when the transaction was refunded
    , provider    :: Maybe T.Text -- ^ Name of the payment provider
    }
  deriving (ChatRevenueTransactionType -> ChatRevenueTransactionType -> Bool
(ChatRevenueTransactionType -> ChatRevenueTransactionType -> Bool)
-> (ChatRevenueTransactionType
    -> ChatRevenueTransactionType -> Bool)
-> Eq ChatRevenueTransactionType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChatRevenueTransactionType -> ChatRevenueTransactionType -> Bool
== :: ChatRevenueTransactionType -> ChatRevenueTransactionType -> Bool
$c/= :: ChatRevenueTransactionType -> ChatRevenueTransactionType -> Bool
/= :: ChatRevenueTransactionType -> ChatRevenueTransactionType -> Bool
Eq, Int -> ChatRevenueTransactionType -> ShowS
[ChatRevenueTransactionType] -> ShowS
ChatRevenueTransactionType -> String
(Int -> ChatRevenueTransactionType -> ShowS)
-> (ChatRevenueTransactionType -> String)
-> ([ChatRevenueTransactionType] -> ShowS)
-> Show ChatRevenueTransactionType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChatRevenueTransactionType -> ShowS
showsPrec :: Int -> ChatRevenueTransactionType -> ShowS
$cshow :: ChatRevenueTransactionType -> String
show :: ChatRevenueTransactionType -> String
$cshowList :: [ChatRevenueTransactionType] -> ShowS
showList :: [ChatRevenueTransactionType] -> ShowS
Show)

instance I.ShortShow ChatRevenueTransactionType where
  shortShow :: ChatRevenueTransactionType -> String
shortShow ChatRevenueTransactionTypeEarnings
    { start_date :: ChatRevenueTransactionType -> Maybe Int
start_date = Maybe Int
start_date_
    , end_date :: ChatRevenueTransactionType -> Maybe Int
end_date   = Maybe Int
end_date_
    }
      = String
"ChatRevenueTransactionTypeEarnings"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"start_date" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
start_date_
        , String
"end_date"   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
end_date_
        ]
  shortShow ChatRevenueTransactionTypeWithdrawal
    { withdrawal_date :: ChatRevenueTransactionType -> Maybe Int
withdrawal_date = Maybe Int
withdrawal_date_
    , provider :: ChatRevenueTransactionType -> Maybe Text
provider        = Maybe Text
provider_
    , state :: ChatRevenueTransactionType -> Maybe RevenueWithdrawalState
state           = Maybe RevenueWithdrawalState
state_
    }
      = String
"ChatRevenueTransactionTypeWithdrawal"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"withdrawal_date" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
withdrawal_date_
        , String
"provider"        String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
provider_
        , String
"state"           String -> Maybe RevenueWithdrawalState -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe RevenueWithdrawalState
state_
        ]
  shortShow ChatRevenueTransactionTypeRefund
    { refund_date :: ChatRevenueTransactionType -> Maybe Int
refund_date = Maybe Int
refund_date_
    , provider :: ChatRevenueTransactionType -> Maybe Text
provider    = Maybe Text
provider_
    }
      = String
"ChatRevenueTransactionTypeRefund"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"refund_date" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
refund_date_
        , String
"provider"    String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
provider_
        ]

instance AT.FromJSON ChatRevenueTransactionType where
  parseJSON :: Value -> Parser ChatRevenueTransactionType
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
"chatRevenueTransactionTypeEarnings"   -> Value -> Parser ChatRevenueTransactionType
parseChatRevenueTransactionTypeEarnings Value
v
      String
"chatRevenueTransactionTypeWithdrawal" -> Value -> Parser ChatRevenueTransactionType
parseChatRevenueTransactionTypeWithdrawal Value
v
      String
"chatRevenueTransactionTypeRefund"     -> Value -> Parser ChatRevenueTransactionType
parseChatRevenueTransactionTypeRefund Value
v
      String
_                                      -> Parser ChatRevenueTransactionType
forall a. Monoid a => a
mempty
    
    where
      parseChatRevenueTransactionTypeEarnings :: A.Value -> AT.Parser ChatRevenueTransactionType
      parseChatRevenueTransactionTypeEarnings :: Value -> Parser ChatRevenueTransactionType
parseChatRevenueTransactionTypeEarnings = String
-> (Object -> Parser ChatRevenueTransactionType)
-> Value
-> Parser ChatRevenueTransactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatRevenueTransactionTypeEarnings" ((Object -> Parser ChatRevenueTransactionType)
 -> Value -> Parser ChatRevenueTransactionType)
-> (Object -> Parser ChatRevenueTransactionType)
-> Value
-> Parser ChatRevenueTransactionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
start_date_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"start_date"
        Maybe Int
end_date_   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"end_date"
        ChatRevenueTransactionType -> Parser ChatRevenueTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatRevenueTransactionType -> Parser ChatRevenueTransactionType)
-> ChatRevenueTransactionType -> Parser ChatRevenueTransactionType
forall a b. (a -> b) -> a -> b
$ ChatRevenueTransactionTypeEarnings
          { start_date :: Maybe Int
start_date = Maybe Int
start_date_
          , end_date :: Maybe Int
end_date   = Maybe Int
end_date_
          }
      parseChatRevenueTransactionTypeWithdrawal :: A.Value -> AT.Parser ChatRevenueTransactionType
      parseChatRevenueTransactionTypeWithdrawal :: Value -> Parser ChatRevenueTransactionType
parseChatRevenueTransactionTypeWithdrawal = String
-> (Object -> Parser ChatRevenueTransactionType)
-> Value
-> Parser ChatRevenueTransactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatRevenueTransactionTypeWithdrawal" ((Object -> Parser ChatRevenueTransactionType)
 -> Value -> Parser ChatRevenueTransactionType)
-> (Object -> Parser ChatRevenueTransactionType)
-> Value
-> Parser ChatRevenueTransactionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
withdrawal_date_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"withdrawal_date"
        Maybe Text
provider_        <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"provider"
        Maybe RevenueWithdrawalState
state_           <- Object
o Object -> Key -> Parser (Maybe RevenueWithdrawalState)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"state"
        ChatRevenueTransactionType -> Parser ChatRevenueTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatRevenueTransactionType -> Parser ChatRevenueTransactionType)
-> ChatRevenueTransactionType -> Parser ChatRevenueTransactionType
forall a b. (a -> b) -> a -> b
$ ChatRevenueTransactionTypeWithdrawal
          { withdrawal_date :: Maybe Int
withdrawal_date = Maybe Int
withdrawal_date_
          , provider :: Maybe Text
provider        = Maybe Text
provider_
          , state :: Maybe RevenueWithdrawalState
state           = Maybe RevenueWithdrawalState
state_
          }
      parseChatRevenueTransactionTypeRefund :: A.Value -> AT.Parser ChatRevenueTransactionType
      parseChatRevenueTransactionTypeRefund :: Value -> Parser ChatRevenueTransactionType
parseChatRevenueTransactionTypeRefund = String
-> (Object -> Parser ChatRevenueTransactionType)
-> Value
-> Parser ChatRevenueTransactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatRevenueTransactionTypeRefund" ((Object -> Parser ChatRevenueTransactionType)
 -> Value -> Parser ChatRevenueTransactionType)
-> (Object -> Parser ChatRevenueTransactionType)
-> Value
-> Parser ChatRevenueTransactionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
refund_date_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"refund_date"
        Maybe Text
provider_    <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"provider"
        ChatRevenueTransactionType -> Parser ChatRevenueTransactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatRevenueTransactionType -> Parser ChatRevenueTransactionType)
-> ChatRevenueTransactionType -> Parser ChatRevenueTransactionType
forall a b. (a -> b) -> a -> b
$ ChatRevenueTransactionTypeRefund
          { refund_date :: Maybe Int
refund_date = Maybe Int
refund_date_
          , provider :: Maybe Text
provider    = Maybe Text
provider_
          }
  parseJSON Value
_ = Parser ChatRevenueTransactionType
forall a. Monoid a => a
mempty