module TD.Data.PaidReactionType
  (PaidReactionType(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I

-- | Describes type of paid message reaction
data PaidReactionType
  = PaidReactionTypeRegular -- ^ A paid reaction on behalf of the current user
  | PaidReactionTypeAnonymous -- ^ An anonymous paid reaction
  | PaidReactionTypeChat -- ^ A paid reaction on behalf of an owned chat
    { PaidReactionType -> Maybe Int
chat_id :: Maybe Int -- ^ Identifier of the chat
    }
  deriving (PaidReactionType -> PaidReactionType -> Bool
(PaidReactionType -> PaidReactionType -> Bool)
-> (PaidReactionType -> PaidReactionType -> Bool)
-> Eq PaidReactionType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PaidReactionType -> PaidReactionType -> Bool
== :: PaidReactionType -> PaidReactionType -> Bool
$c/= :: PaidReactionType -> PaidReactionType -> Bool
/= :: PaidReactionType -> PaidReactionType -> Bool
Eq, Int -> PaidReactionType -> ShowS
[PaidReactionType] -> ShowS
PaidReactionType -> String
(Int -> PaidReactionType -> ShowS)
-> (PaidReactionType -> String)
-> ([PaidReactionType] -> ShowS)
-> Show PaidReactionType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PaidReactionType -> ShowS
showsPrec :: Int -> PaidReactionType -> ShowS
$cshow :: PaidReactionType -> String
show :: PaidReactionType -> String
$cshowList :: [PaidReactionType] -> ShowS
showList :: [PaidReactionType] -> ShowS
Show)

instance I.ShortShow PaidReactionType where
  shortShow :: PaidReactionType -> String
shortShow PaidReactionType
PaidReactionTypeRegular
      = String
"PaidReactionTypeRegular"
  shortShow PaidReactionType
PaidReactionTypeAnonymous
      = String
"PaidReactionTypeAnonymous"
  shortShow PaidReactionTypeChat
    { chat_id :: PaidReactionType -> Maybe Int
chat_id = Maybe Int
chat_id_
    }
      = String
"PaidReactionTypeChat"
        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_
        ]

instance AT.FromJSON PaidReactionType where
  parseJSON :: Value -> Parser PaidReactionType
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
"paidReactionTypeRegular"   -> PaidReactionType -> Parser PaidReactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PaidReactionType
PaidReactionTypeRegular
      String
"paidReactionTypeAnonymous" -> PaidReactionType -> Parser PaidReactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PaidReactionType
PaidReactionTypeAnonymous
      String
"paidReactionTypeChat"      -> Value -> Parser PaidReactionType
parsePaidReactionTypeChat Value
v
      String
_                           -> Parser PaidReactionType
forall a. Monoid a => a
mempty
    
    where
      parsePaidReactionTypeChat :: A.Value -> AT.Parser PaidReactionType
      parsePaidReactionTypeChat :: Value -> Parser PaidReactionType
parsePaidReactionTypeChat = String
-> (Object -> Parser PaidReactionType)
-> Value
-> Parser PaidReactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PaidReactionTypeChat" ((Object -> Parser PaidReactionType)
 -> Value -> Parser PaidReactionType)
-> (Object -> Parser PaidReactionType)
-> Value
-> Parser PaidReactionType
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"
        PaidReactionType -> Parser PaidReactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PaidReactionType -> Parser PaidReactionType)
-> PaidReactionType -> Parser PaidReactionType
forall a b. (a -> b) -> a -> b
$ PaidReactionTypeChat
          { chat_id :: Maybe Int
chat_id = Maybe Int
chat_id_
          }
  parseJSON Value
_ = Parser PaidReactionType
forall a. Monoid a => a
mempty

instance AT.ToJSON PaidReactionType where
  toJSON :: PaidReactionType -> Value
toJSON PaidReactionType
PaidReactionTypeRegular
      = [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
"paidReactionTypeRegular"
        ]
  toJSON PaidReactionType
PaidReactionTypeAnonymous
      = [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
"paidReactionTypeAnonymous"
        ]
  toJSON PaidReactionTypeChat
    { chat_id :: PaidReactionType -> Maybe Int
chat_id = Maybe Int
chat_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
"paidReactionTypeChat"
        , 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_
        ]