module TD.Data.TextQuote
  (TextQuote(..)) 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.FormattedText as FormattedText

data TextQuote
  = TextQuote -- ^ Describes manually or automatically chosen quote from another message
    { TextQuote -> Maybe FormattedText
text      :: Maybe FormattedText.FormattedText -- ^ Text of the quote. Only Bold, Italic, Underline, Strikethrough, Spoiler, and CustomEmoji entities can be present in the text
    , TextQuote -> Maybe Int
position  :: Maybe Int                         -- ^ Approximate quote position in the original message in UTF-16 code units as specified by the message sender
    , TextQuote -> Maybe Bool
is_manual :: Maybe Bool                        -- ^ True, if the quote was manually chosen by the message sender
    }
  deriving (TextQuote -> TextQuote -> Bool
(TextQuote -> TextQuote -> Bool)
-> (TextQuote -> TextQuote -> Bool) -> Eq TextQuote
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TextQuote -> TextQuote -> Bool
== :: TextQuote -> TextQuote -> Bool
$c/= :: TextQuote -> TextQuote -> Bool
/= :: TextQuote -> TextQuote -> Bool
Eq, Int -> TextQuote -> ShowS
[TextQuote] -> ShowS
TextQuote -> String
(Int -> TextQuote -> ShowS)
-> (TextQuote -> String)
-> ([TextQuote] -> ShowS)
-> Show TextQuote
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TextQuote -> ShowS
showsPrec :: Int -> TextQuote -> ShowS
$cshow :: TextQuote -> String
show :: TextQuote -> String
$cshowList :: [TextQuote] -> ShowS
showList :: [TextQuote] -> ShowS
Show)

instance I.ShortShow TextQuote where
  shortShow :: TextQuote -> String
shortShow TextQuote
    { text :: TextQuote -> Maybe FormattedText
text      = Maybe FormattedText
text_
    , position :: TextQuote -> Maybe Int
position  = Maybe Int
position_
    , is_manual :: TextQuote -> Maybe Bool
is_manual = Maybe Bool
is_manual_
    }
      = String
"TextQuote"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"text"      String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
text_
        , String
"position"  String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
position_
        , String
"is_manual" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_manual_
        ]

instance AT.FromJSON TextQuote where
  parseJSON :: Value -> Parser TextQuote
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
"textQuote" -> Value -> Parser TextQuote
parseTextQuote Value
v
      String
_           -> Parser TextQuote
forall a. Monoid a => a
mempty
    
    where
      parseTextQuote :: A.Value -> AT.Parser TextQuote
      parseTextQuote :: Value -> Parser TextQuote
parseTextQuote = String -> (Object -> Parser TextQuote) -> Value -> Parser TextQuote
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TextQuote" ((Object -> Parser TextQuote) -> Value -> Parser TextQuote)
-> (Object -> Parser TextQuote) -> Value -> Parser TextQuote
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe FormattedText
text_      <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"text"
        Maybe Int
position_  <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"position"
        Maybe Bool
is_manual_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_manual"
        TextQuote -> Parser TextQuote
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TextQuote -> Parser TextQuote) -> TextQuote -> Parser TextQuote
forall a b. (a -> b) -> a -> b
$ TextQuote
          { text :: Maybe FormattedText
text      = Maybe FormattedText
text_
          , position :: Maybe Int
position  = Maybe Int
position_
          , is_manual :: Maybe Bool
is_manual = Maybe Bool
is_manual_
          }
  parseJSON Value
_ = Parser TextQuote
forall a. Monoid a => a
mempty