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

data Text
  = Text -- ^ Contains some text
    { Text -> Maybe Text
text :: Maybe T.Text -- ^ Text
    }
  deriving (Text -> Text -> Bool
(Text -> Text -> Bool) -> (Text -> Text -> Bool) -> Eq Text
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Text -> Text -> Bool
== :: Text -> Text -> Bool
$c/= :: Text -> Text -> Bool
/= :: Text -> Text -> Bool
Eq, Int -> Text -> ShowS
[Text] -> ShowS
Text -> String
(Int -> Text -> ShowS)
-> (Text -> String) -> ([Text] -> ShowS) -> Show Text
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Text -> ShowS
showsPrec :: Int -> Text -> ShowS
$cshow :: Text -> String
show :: Text -> String
$cshowList :: [Text] -> ShowS
showList :: [Text] -> ShowS
Show)

instance I.ShortShow Text where
  shortShow :: Text -> String
shortShow Text
    { text :: Text -> Maybe Text
text = Maybe Text
text_
    }
      = String
"Text"
        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_
        ]

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