module TD.Data.TextEntity
  ( TextEntity(..)    
  , defaultTextEntity 
  ) 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.TextEntityType as TextEntityType

data TextEntity
  = TextEntity -- ^ Represents a part of the text that needs to be formatted in some unusual way
    { TextEntity -> Maybe Int
offset  :: Maybe Int                           -- ^ Offset of the entity, in UTF-16 code units
    , TextEntity -> Maybe Int
_length :: Maybe Int                           -- ^ Length of the entity, in UTF-16 code units
    , TextEntity -> Maybe TextEntityType
_type   :: Maybe TextEntityType.TextEntityType -- ^ Type of the entity
    }
  deriving (TextEntity -> TextEntity -> Bool
(TextEntity -> TextEntity -> Bool)
-> (TextEntity -> TextEntity -> Bool) -> Eq TextEntity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TextEntity -> TextEntity -> Bool
== :: TextEntity -> TextEntity -> Bool
$c/= :: TextEntity -> TextEntity -> Bool
/= :: TextEntity -> TextEntity -> Bool
Eq, Int -> TextEntity -> ShowS
[TextEntity] -> ShowS
TextEntity -> String
(Int -> TextEntity -> ShowS)
-> (TextEntity -> String)
-> ([TextEntity] -> ShowS)
-> Show TextEntity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TextEntity -> ShowS
showsPrec :: Int -> TextEntity -> ShowS
$cshow :: TextEntity -> String
show :: TextEntity -> String
$cshowList :: [TextEntity] -> ShowS
showList :: [TextEntity] -> ShowS
Show)

instance I.ShortShow TextEntity where
  shortShow :: TextEntity -> String
shortShow TextEntity
    { offset :: TextEntity -> Maybe Int
offset  = Maybe Int
offset_
    , _length :: TextEntity -> Maybe Int
_length = Maybe Int
_length_
    , _type :: TextEntity -> Maybe TextEntityType
_type   = Maybe TextEntityType
_type_
    }
      = String
"TextEntity"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"offset"  String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
offset_
        , String
"_length" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_length_
        , String
"_type"   String -> Maybe TextEntityType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe TextEntityType
_type_
        ]

instance AT.FromJSON TextEntity where
  parseJSON :: Value -> Parser TextEntity
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
"textEntity" -> Value -> Parser TextEntity
parseTextEntity Value
v
      String
_            -> Parser TextEntity
forall a. Monoid a => a
mempty
    
    where
      parseTextEntity :: A.Value -> AT.Parser TextEntity
      parseTextEntity :: Value -> Parser TextEntity
parseTextEntity = String
-> (Object -> Parser TextEntity) -> Value -> Parser TextEntity
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TextEntity" ((Object -> Parser TextEntity) -> Value -> Parser TextEntity)
-> (Object -> Parser TextEntity) -> Value -> Parser TextEntity
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
offset_  <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"offset"
        Maybe Int
_length_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"length"
        Maybe TextEntityType
_type_   <- Object
o Object -> Key -> Parser (Maybe TextEntityType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"type"
        TextEntity -> Parser TextEntity
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TextEntity -> Parser TextEntity)
-> TextEntity -> Parser TextEntity
forall a b. (a -> b) -> a -> b
$ TextEntity
          { offset :: Maybe Int
offset  = Maybe Int
offset_
          , _length :: Maybe Int
_length = Maybe Int
_length_
          , _type :: Maybe TextEntityType
_type   = Maybe TextEntityType
_type_
          }
  parseJSON Value
_ = Parser TextEntity
forall a. Monoid a => a
mempty

instance AT.ToJSON TextEntity where
  toJSON :: TextEntity -> Value
toJSON TextEntity
    { offset :: TextEntity -> Maybe Int
offset  = Maybe Int
offset_
    , _length :: TextEntity -> Maybe Int
_length = Maybe Int
_length_
    , _type :: TextEntity -> Maybe TextEntityType
_type   = Maybe TextEntityType
_type_
    }
      = [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
"textEntity"
        , Key
"offset" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
offset_
        , Key
"length" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
_length_
        , Key
"type"   Key -> Maybe TextEntityType -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe TextEntityType
_type_
        ]

defaultTextEntity :: TextEntity
defaultTextEntity :: TextEntity
defaultTextEntity =
  TextEntity
    { offset :: Maybe Int
offset  = Maybe Int
forall a. Maybe a
Nothing
    , _length :: Maybe Int
_length = Maybe Int
forall a. Maybe a
Nothing
    , _type :: Maybe TextEntityType
_type   = Maybe TextEntityType
forall a. Maybe a
Nothing
    }