module TD.Data.StarSubscription
  (StarSubscription(..)) 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.StarSubscriptionPricing as StarSubscriptionPricing

data StarSubscription
  = StarSubscription -- ^ Contains information about subscription to a channel chat paid in Telegram Stars
    { StarSubscription -> Maybe Text
_id             :: Maybe T.Text                                          -- ^ Unique identifier of the subscription
    , StarSubscription -> Maybe Int
chat_id         :: Maybe Int                                             -- ^ Identifier of the channel chat that is subscribed
    , StarSubscription -> Maybe Int
expiration_date :: Maybe Int                                             -- ^ Point in time (Unix timestamp) when the subscription will expire or expired
    , StarSubscription -> Maybe Bool
can_reuse       :: Maybe Bool                                            -- ^ True, if the subscription is active and the user can use the method reuseStarSubscription to join the subscribed chat again
    , StarSubscription -> Maybe Bool
is_canceled     :: Maybe Bool                                            -- ^ True, if the subscription was canceled
    , StarSubscription -> Maybe Bool
is_expiring     :: Maybe Bool                                            -- ^ True, if the subscription expires soon and there are no enough Telegram Stars on the user's balance to extend it
    , StarSubscription -> Maybe Text
invite_link     :: Maybe T.Text                                          -- ^ The invite link that can be used to renew the subscription if it has been expired; may be empty, if the link isn't available anymore
    , StarSubscription -> Maybe StarSubscriptionPricing
pricing         :: Maybe StarSubscriptionPricing.StarSubscriptionPricing -- ^ The subscription plan
    }
  deriving (StarSubscription -> StarSubscription -> Bool
(StarSubscription -> StarSubscription -> Bool)
-> (StarSubscription -> StarSubscription -> Bool)
-> Eq StarSubscription
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StarSubscription -> StarSubscription -> Bool
== :: StarSubscription -> StarSubscription -> Bool
$c/= :: StarSubscription -> StarSubscription -> Bool
/= :: StarSubscription -> StarSubscription -> Bool
Eq, Int -> StarSubscription -> ShowS
[StarSubscription] -> ShowS
StarSubscription -> String
(Int -> StarSubscription -> ShowS)
-> (StarSubscription -> String)
-> ([StarSubscription] -> ShowS)
-> Show StarSubscription
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StarSubscription -> ShowS
showsPrec :: Int -> StarSubscription -> ShowS
$cshow :: StarSubscription -> String
show :: StarSubscription -> String
$cshowList :: [StarSubscription] -> ShowS
showList :: [StarSubscription] -> ShowS
Show)

instance I.ShortShow StarSubscription where
  shortShow :: StarSubscription -> String
shortShow StarSubscription
    { _id :: StarSubscription -> Maybe Text
_id             = Maybe Text
_id_
    , chat_id :: StarSubscription -> Maybe Int
chat_id         = Maybe Int
chat_id_
    , expiration_date :: StarSubscription -> Maybe Int
expiration_date = Maybe Int
expiration_date_
    , can_reuse :: StarSubscription -> Maybe Bool
can_reuse       = Maybe Bool
can_reuse_
    , is_canceled :: StarSubscription -> Maybe Bool
is_canceled     = Maybe Bool
is_canceled_
    , is_expiring :: StarSubscription -> Maybe Bool
is_expiring     = Maybe Bool
is_expiring_
    , invite_link :: StarSubscription -> Maybe Text
invite_link     = Maybe Text
invite_link_
    , pricing :: StarSubscription -> Maybe StarSubscriptionPricing
pricing         = Maybe StarSubscriptionPricing
pricing_
    }
      = String
"StarSubscription"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"             String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
_id_
        , String
"chat_id"         String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
chat_id_
        , String
"expiration_date" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
expiration_date_
        , String
"can_reuse"       String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
can_reuse_
        , String
"is_canceled"     String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_canceled_
        , String
"is_expiring"     String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_expiring_
        , String
"invite_link"     String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
invite_link_
        , String
"pricing"         String -> Maybe StarSubscriptionPricing -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe StarSubscriptionPricing
pricing_
        ]

instance AT.FromJSON StarSubscription where
  parseJSON :: Value -> Parser StarSubscription
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
"starSubscription" -> Value -> Parser StarSubscription
parseStarSubscription Value
v
      String
_                  -> Parser StarSubscription
forall a. Monoid a => a
mempty
    
    where
      parseStarSubscription :: A.Value -> AT.Parser StarSubscription
      parseStarSubscription :: Value -> Parser StarSubscription
parseStarSubscription = String
-> (Object -> Parser StarSubscription)
-> Value
-> Parser StarSubscription
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StarSubscription" ((Object -> Parser StarSubscription)
 -> Value -> Parser StarSubscription)
-> (Object -> Parser StarSubscription)
-> Value
-> Parser StarSubscription
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
_id_             <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        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
expiration_date_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"expiration_date"
        Maybe Bool
can_reuse_       <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"can_reuse"
        Maybe Bool
is_canceled_     <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_canceled"
        Maybe Bool
is_expiring_     <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_expiring"
        Maybe Text
invite_link_     <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"invite_link"
        Maybe StarSubscriptionPricing
pricing_         <- Object
o Object -> Key -> Parser (Maybe StarSubscriptionPricing)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"pricing"
        StarSubscription -> Parser StarSubscription
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StarSubscription -> Parser StarSubscription)
-> StarSubscription -> Parser StarSubscription
forall a b. (a -> b) -> a -> b
$ StarSubscription
          { _id :: Maybe Text
_id             = Maybe Text
_id_
          , chat_id :: Maybe Int
chat_id         = Maybe Int
chat_id_
          , expiration_date :: Maybe Int
expiration_date = Maybe Int
expiration_date_
          , can_reuse :: Maybe Bool
can_reuse       = Maybe Bool
can_reuse_
          , is_canceled :: Maybe Bool
is_canceled     = Maybe Bool
is_canceled_
          , is_expiring :: Maybe Bool
is_expiring     = Maybe Bool
is_expiring_
          , invite_link :: Maybe Text
invite_link     = Maybe Text
invite_link_
          , pricing :: Maybe StarSubscriptionPricing
pricing         = Maybe StarSubscriptionPricing
pricing_
          }
  parseJSON Value
_ = Parser StarSubscription
forall a. Monoid a => a
mempty