module TD.Data.FormattedText
  ( FormattedText(..)    
  , defaultFormattedText 
  ) 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
import qualified TD.Data.TextEntity as TextEntity

data FormattedText
  = FormattedText -- ^ A text with some entities
    { FormattedText -> Maybe Text
text     :: Maybe T.Text                  -- ^ The text
    , FormattedText -> Maybe [TextEntity]
entities :: Maybe [TextEntity.TextEntity] -- ^ Entities contained in the text. Entities can be nested, but must not mutually intersect with each other. Pre, Code and PreCode entities can't contain other entities. BlockQuote entities can't contain other BlockQuote entities. Bold, Italic, Underline, Strikethrough, and Spoiler entities can contain and can be part of any other entities. All other entities can't contain each other
    }
  deriving (FormattedText -> FormattedText -> Bool
(FormattedText -> FormattedText -> Bool)
-> (FormattedText -> FormattedText -> Bool) -> Eq FormattedText
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FormattedText -> FormattedText -> Bool
== :: FormattedText -> FormattedText -> Bool
$c/= :: FormattedText -> FormattedText -> Bool
/= :: FormattedText -> FormattedText -> Bool
Eq, Int -> FormattedText -> ShowS
[FormattedText] -> ShowS
FormattedText -> String
(Int -> FormattedText -> ShowS)
-> (FormattedText -> String)
-> ([FormattedText] -> ShowS)
-> Show FormattedText
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FormattedText -> ShowS
showsPrec :: Int -> FormattedText -> ShowS
$cshow :: FormattedText -> String
show :: FormattedText -> String
$cshowList :: [FormattedText] -> ShowS
showList :: [FormattedText] -> ShowS
Show)

instance I.ShortShow FormattedText where
  shortShow :: FormattedText -> String
shortShow FormattedText
    { text :: FormattedText -> Maybe Text
text     = Maybe Text
text_
    , entities :: FormattedText -> Maybe [TextEntity]
entities = Maybe [TextEntity]
entities_
    }
      = String
"FormattedText"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"text"     String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
text_
        , String
"entities" String -> Maybe [TextEntity] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [TextEntity]
entities_
        ]

instance AT.FromJSON FormattedText where
  parseJSON :: Value -> Parser FormattedText
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
"formattedText" -> Value -> Parser FormattedText
parseFormattedText Value
v
      String
_               -> Parser FormattedText
forall a. Monoid a => a
mempty
    
    where
      parseFormattedText :: A.Value -> AT.Parser FormattedText
      parseFormattedText :: Value -> Parser FormattedText
parseFormattedText = String
-> (Object -> Parser FormattedText)
-> Value
-> Parser FormattedText
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"FormattedText" ((Object -> Parser FormattedText) -> Value -> Parser FormattedText)
-> (Object -> Parser FormattedText)
-> Value
-> Parser FormattedText
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
text_     <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"text"
        Maybe [TextEntity]
entities_ <- Object
o Object -> Key -> Parser (Maybe [TextEntity])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"entities"
        FormattedText -> Parser FormattedText
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FormattedText -> Parser FormattedText)
-> FormattedText -> Parser FormattedText
forall a b. (a -> b) -> a -> b
$ FormattedText
          { text :: Maybe Text
text     = Maybe Text
text_
          , entities :: Maybe [TextEntity]
entities = Maybe [TextEntity]
entities_
          }
  parseJSON Value
_ = Parser FormattedText
forall a. Monoid a => a
mempty

instance AT.ToJSON FormattedText where
  toJSON :: FormattedText -> Value
toJSON FormattedText
    { text :: FormattedText -> Maybe Text
text     = Maybe Text
text_
    , entities :: FormattedText -> Maybe [TextEntity]
entities = Maybe [TextEntity]
entities_
    }
      = [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
"formattedText"
        , Key
"text"     Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
text_
        , Key
"entities" Key -> Maybe [TextEntity] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe [TextEntity]
entities_
        ]

defaultFormattedText :: FormattedText
defaultFormattedText :: FormattedText
defaultFormattedText =
  FormattedText
    { text :: Maybe Text
text     = Maybe Text
forall a. Maybe a
Nothing
    , entities :: Maybe [TextEntity]
entities = Maybe [TextEntity]
forall a. Maybe a
Nothing
    }