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

-- | Describes type of message reaction
data ReactionType
  = ReactionTypeEmoji -- ^ A reaction with an emoji
    { ReactionType -> Maybe Text
emoji :: Maybe T.Text -- ^ Text representation of the reaction
    }
  | ReactionTypeCustomEmoji -- ^ A reaction with a custom emoji
    { ReactionType -> Maybe Int
custom_emoji_id :: Maybe Int -- ^ Unique identifier of the custom emoji
    }
  | ReactionTypePaid -- ^ The paid reaction in a channel chat
  deriving (ReactionType -> ReactionType -> Bool
(ReactionType -> ReactionType -> Bool)
-> (ReactionType -> ReactionType -> Bool) -> Eq ReactionType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ReactionType -> ReactionType -> Bool
== :: ReactionType -> ReactionType -> Bool
$c/= :: ReactionType -> ReactionType -> Bool
/= :: ReactionType -> ReactionType -> Bool
Eq, Int -> ReactionType -> ShowS
[ReactionType] -> ShowS
ReactionType -> String
(Int -> ReactionType -> ShowS)
-> (ReactionType -> String)
-> ([ReactionType] -> ShowS)
-> Show ReactionType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ReactionType -> ShowS
showsPrec :: Int -> ReactionType -> ShowS
$cshow :: ReactionType -> String
show :: ReactionType -> String
$cshowList :: [ReactionType] -> ShowS
showList :: [ReactionType] -> ShowS
Show)

instance I.ShortShow ReactionType where
  shortShow :: ReactionType -> String
shortShow ReactionTypeEmoji
    { emoji :: ReactionType -> Maybe Text
emoji = Maybe Text
emoji_
    }
      = String
"ReactionTypeEmoji"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"emoji" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
emoji_
        ]
  shortShow ReactionTypeCustomEmoji
    { custom_emoji_id :: ReactionType -> Maybe Int
custom_emoji_id = Maybe Int
custom_emoji_id_
    }
      = String
"ReactionTypeCustomEmoji"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"custom_emoji_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
custom_emoji_id_
        ]
  shortShow ReactionType
ReactionTypePaid
      = String
"ReactionTypePaid"

instance AT.FromJSON ReactionType where
  parseJSON :: Value -> Parser ReactionType
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
"reactionTypeEmoji"       -> Value -> Parser ReactionType
parseReactionTypeEmoji Value
v
      String
"reactionTypeCustomEmoji" -> Value -> Parser ReactionType
parseReactionTypeCustomEmoji Value
v
      String
"reactionTypePaid"        -> ReactionType -> Parser ReactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ReactionType
ReactionTypePaid
      String
_                         -> Parser ReactionType
forall a. Monoid a => a
mempty
    
    where
      parseReactionTypeEmoji :: A.Value -> AT.Parser ReactionType
      parseReactionTypeEmoji :: Value -> Parser ReactionType
parseReactionTypeEmoji = String
-> (Object -> Parser ReactionType) -> Value -> Parser ReactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ReactionTypeEmoji" ((Object -> Parser ReactionType) -> Value -> Parser ReactionType)
-> (Object -> Parser ReactionType) -> Value -> Parser ReactionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
emoji_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"emoji"
        ReactionType -> Parser ReactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ReactionType -> Parser ReactionType)
-> ReactionType -> Parser ReactionType
forall a b. (a -> b) -> a -> b
$ ReactionTypeEmoji
          { emoji :: Maybe Text
emoji = Maybe Text
emoji_
          }
      parseReactionTypeCustomEmoji :: A.Value -> AT.Parser ReactionType
      parseReactionTypeCustomEmoji :: Value -> Parser ReactionType
parseReactionTypeCustomEmoji = String
-> (Object -> Parser ReactionType) -> Value -> Parser ReactionType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ReactionTypeCustomEmoji" ((Object -> Parser ReactionType) -> Value -> Parser ReactionType)
-> (Object -> Parser ReactionType) -> Value -> Parser ReactionType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
custom_emoji_id_ <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"custom_emoji_id"
        ReactionType -> Parser ReactionType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ReactionType -> Parser ReactionType)
-> ReactionType -> Parser ReactionType
forall a b. (a -> b) -> a -> b
$ ReactionTypeCustomEmoji
          { custom_emoji_id :: Maybe Int
custom_emoji_id = Maybe Int
custom_emoji_id_
          }
  parseJSON Value
_ = Parser ReactionType
forall a. Monoid a => a
mempty

instance AT.ToJSON ReactionType where
  toJSON :: ReactionType -> Value
toJSON ReactionTypeEmoji
    { emoji :: ReactionType -> Maybe Text
emoji = Maybe Text
emoji_
    }
      = [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
"reactionTypeEmoji"
        , Key
"emoji" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
emoji_
        ]
  toJSON ReactionTypeCustomEmoji
    { custom_emoji_id :: ReactionType -> Maybe Int
custom_emoji_id = Maybe Int
custom_emoji_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
"reactionTypeCustomEmoji"
        , Key
"custom_emoji_id" Key -> Maybe Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= (Int -> Value) -> Maybe Int -> Maybe Value
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> Value
I.writeInt64  Maybe Int
custom_emoji_id_
        ]
  toJSON ReactionType
ReactionTypePaid
      = [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
"reactionTypePaid"
        ]