module TD.Data.InputTextQuote
  ( InputTextQuote(..)    
  , defaultInputTextQuote 
  ) 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 InputTextQuote
  = InputTextQuote -- ^ Describes manually chosen quote from another message
    { InputTextQuote -> Maybe FormattedText
text     :: Maybe FormattedText.FormattedText -- ^ Text of the quote; 0-getOption("message_reply_quote_length_max") characters. Only Bold, Italic, Underline, Strikethrough, Spoiler, and CustomEmoji entities are allowed to be kept and must be kept in the quote
    , InputTextQuote -> Maybe Int
position :: Maybe Int                         -- ^ Quote position in the original message in UTF-16 code units
    }
  deriving (InputTextQuote -> InputTextQuote -> Bool
(InputTextQuote -> InputTextQuote -> Bool)
-> (InputTextQuote -> InputTextQuote -> Bool) -> Eq InputTextQuote
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InputTextQuote -> InputTextQuote -> Bool
== :: InputTextQuote -> InputTextQuote -> Bool
$c/= :: InputTextQuote -> InputTextQuote -> Bool
/= :: InputTextQuote -> InputTextQuote -> Bool
Eq, Int -> InputTextQuote -> ShowS
[InputTextQuote] -> ShowS
InputTextQuote -> String
(Int -> InputTextQuote -> ShowS)
-> (InputTextQuote -> String)
-> ([InputTextQuote] -> ShowS)
-> Show InputTextQuote
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InputTextQuote -> ShowS
showsPrec :: Int -> InputTextQuote -> ShowS
$cshow :: InputTextQuote -> String
show :: InputTextQuote -> String
$cshowList :: [InputTextQuote] -> ShowS
showList :: [InputTextQuote] -> ShowS
Show)

instance I.ShortShow InputTextQuote where
  shortShow :: InputTextQuote -> String
shortShow InputTextQuote
    { text :: InputTextQuote -> Maybe FormattedText
text     = Maybe FormattedText
text_
    , position :: InputTextQuote -> Maybe Int
position = Maybe Int
position_
    }
      = String
"InputTextQuote"
        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_
        ]

instance AT.FromJSON InputTextQuote where
  parseJSON :: Value -> Parser InputTextQuote
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
"inputTextQuote" -> Value -> Parser InputTextQuote
parseInputTextQuote Value
v
      String
_                -> Parser InputTextQuote
forall a. Monoid a => a
mempty
    
    where
      parseInputTextQuote :: A.Value -> AT.Parser InputTextQuote
      parseInputTextQuote :: Value -> Parser InputTextQuote
parseInputTextQuote = String
-> (Object -> Parser InputTextQuote)
-> Value
-> Parser InputTextQuote
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputTextQuote" ((Object -> Parser InputTextQuote)
 -> Value -> Parser InputTextQuote)
-> (Object -> Parser InputTextQuote)
-> Value
-> Parser InputTextQuote
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"
        InputTextQuote -> Parser InputTextQuote
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputTextQuote -> Parser InputTextQuote)
-> InputTextQuote -> Parser InputTextQuote
forall a b. (a -> b) -> a -> b
$ InputTextQuote
          { text :: Maybe FormattedText
text     = Maybe FormattedText
text_
          , position :: Maybe Int
position = Maybe Int
position_
          }
  parseJSON Value
_ = Parser InputTextQuote
forall a. Monoid a => a
mempty

instance AT.ToJSON InputTextQuote where
  toJSON :: InputTextQuote -> Value
toJSON InputTextQuote
    { text :: InputTextQuote -> Maybe FormattedText
text     = Maybe FormattedText
text_
    , position :: InputTextQuote -> Maybe Int
position = Maybe Int
position_
    }
      = [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
"inputTextQuote"
        , Key
"text"     Key -> Maybe FormattedText -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe FormattedText
text_
        , Key
"position" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
position_
        ]

defaultInputTextQuote :: InputTextQuote
defaultInputTextQuote :: InputTextQuote
defaultInputTextQuote =
  InputTextQuote
    { text :: Maybe FormattedText
text     = Maybe FormattedText
forall a. Maybe a
Nothing
    , position :: Maybe Int
position = Maybe Int
forall a. Maybe a
Nothing
    }