module TD.Data.AffiliateInfo
  (AffiliateInfo(..)) 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.StarAmount as StarAmount

data AffiliateInfo
  = AffiliateInfo -- ^ Contains information about an affiliate that received commission from a Telegram Star transaction
    { AffiliateInfo -> Maybe Int
commission_per_mille :: Maybe Int                   -- ^ The number of Telegram Stars received by the affiliate for each 1000 Telegram Stars received by the program owner
    , AffiliateInfo -> Maybe Int
affiliate_chat_id    :: Maybe Int                   -- ^ Identifier of the chat which received the commission
    , AffiliateInfo -> Maybe StarAmount
star_amount          :: Maybe StarAmount.StarAmount -- ^ The amount of Telegram Stars that were received by the affiliate; can be negative for refunds
    }
  deriving (AffiliateInfo -> AffiliateInfo -> Bool
(AffiliateInfo -> AffiliateInfo -> Bool)
-> (AffiliateInfo -> AffiliateInfo -> Bool) -> Eq AffiliateInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AffiliateInfo -> AffiliateInfo -> Bool
== :: AffiliateInfo -> AffiliateInfo -> Bool
$c/= :: AffiliateInfo -> AffiliateInfo -> Bool
/= :: AffiliateInfo -> AffiliateInfo -> Bool
Eq, Int -> AffiliateInfo -> ShowS
[AffiliateInfo] -> ShowS
AffiliateInfo -> String
(Int -> AffiliateInfo -> ShowS)
-> (AffiliateInfo -> String)
-> ([AffiliateInfo] -> ShowS)
-> Show AffiliateInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AffiliateInfo -> ShowS
showsPrec :: Int -> AffiliateInfo -> ShowS
$cshow :: AffiliateInfo -> String
show :: AffiliateInfo -> String
$cshowList :: [AffiliateInfo] -> ShowS
showList :: [AffiliateInfo] -> ShowS
Show)

instance I.ShortShow AffiliateInfo where
  shortShow :: AffiliateInfo -> String
shortShow AffiliateInfo
    { commission_per_mille :: AffiliateInfo -> Maybe Int
commission_per_mille = Maybe Int
commission_per_mille_
    , affiliate_chat_id :: AffiliateInfo -> Maybe Int
affiliate_chat_id    = Maybe Int
affiliate_chat_id_
    , star_amount :: AffiliateInfo -> Maybe StarAmount
star_amount          = Maybe StarAmount
star_amount_
    }
      = String
"AffiliateInfo"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"commission_per_mille" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
commission_per_mille_
        , String
"affiliate_chat_id"    String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
affiliate_chat_id_
        , String
"star_amount"          String -> Maybe StarAmount -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StarAmount
star_amount_
        ]

instance AT.FromJSON AffiliateInfo where
  parseJSON :: Value -> Parser AffiliateInfo
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
"affiliateInfo" -> Value -> Parser AffiliateInfo
parseAffiliateInfo Value
v
      String
_               -> Parser AffiliateInfo
forall a. Monoid a => a
mempty
    
    where
      parseAffiliateInfo :: A.Value -> AT.Parser AffiliateInfo
      parseAffiliateInfo :: Value -> Parser AffiliateInfo
parseAffiliateInfo = String
-> (Object -> Parser AffiliateInfo)
-> Value
-> Parser AffiliateInfo
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"AffiliateInfo" ((Object -> Parser AffiliateInfo) -> Value -> Parser AffiliateInfo)
-> (Object -> Parser AffiliateInfo)
-> Value
-> Parser AffiliateInfo
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
commission_per_mille_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"commission_per_mille"
        Maybe Int
affiliate_chat_id_    <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"affiliate_chat_id"
        Maybe StarAmount
star_amount_          <- Object
o Object -> Key -> Parser (Maybe StarAmount)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"star_amount"
        AffiliateInfo -> Parser AffiliateInfo
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AffiliateInfo -> Parser AffiliateInfo)
-> AffiliateInfo -> Parser AffiliateInfo
forall a b. (a -> b) -> a -> b
$ AffiliateInfo
          { commission_per_mille :: Maybe Int
commission_per_mille = Maybe Int
commission_per_mille_
          , affiliate_chat_id :: Maybe Int
affiliate_chat_id    = Maybe Int
affiliate_chat_id_
          , star_amount :: Maybe StarAmount
star_amount          = Maybe StarAmount
star_amount_
          }
  parseJSON Value
_ = Parser AffiliateInfo
forall a. Monoid a => a
mempty