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
import qualified TD.Data.RichText as RichText

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, CustomEmoji, and DateTime entities are allowed to be kept and must be kept in the quote. Ignored if rich_texts are non-empty
    , InputTextQuote -> Maybe [RichText]
rich_texts :: Maybe [RichText.RichText]         -- ^ Quote from a rich message; pass an empty array if the quote isn't chosen from a rich message. Must contain one element for each quoted text of the message in the order of their appearance. The first and the last elements can contain only a part of the corresponding text; all other elements must contain the whole text. Formatting of the quoted parts must be kept. Mathematical expressions must be quoted entirely. Buttons in pageBlockButtonRow, anchors and dividers can't be quoted. There can be up to 32 quoted texts with total length up to getOption("message_reply_quote_length_max") characters
    , InputTextQuote -> Maybe Int
position   :: Maybe Int                         -- ^ Quote position in the original message in UTF-16 code units. For quotes from rich messages, it is the position in the text formed by joining plain texts of all quotable texts of the message, separated by line feeds
    }
  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_
    , rich_texts :: InputTextQuote -> Maybe [RichText]
rich_texts = Maybe [RichText]
rich_texts_
    , 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
"rich_texts" String -> Maybe [RichText] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [RichText]
rich_texts_
        , 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 [RichText]
rich_texts_ <- Object
o Object -> Key -> Parser (Maybe [RichText])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"rich_texts"
        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_
          , rich_texts :: Maybe [RichText]
rich_texts = Maybe [RichText]
rich_texts_
          , 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_
    , rich_texts :: InputTextQuote -> Maybe [RichText]
rich_texts = Maybe [RichText]
rich_texts_
    , 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
"rich_texts" Key -> Maybe [RichText] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe [RichText]
rich_texts_
        , 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
    , rich_texts :: Maybe [RichText]
rich_texts = Maybe [RichText]
forall a. Maybe a
Nothing
    , position :: Maybe Int
position   = Maybe Int
forall a. Maybe a
Nothing
    }