module TD.Data.DiffEntity
  (DiffEntity(..)) 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.DiffEntityType as DiffEntityType

data DiffEntity
  = DiffEntity -- ^ Represents a change of a text
    { DiffEntity -> Maybe Int
offset  :: Maybe Int                           -- ^ Offset of the entity, in UTF-16 code units
    , DiffEntity -> Maybe Int
_length :: Maybe Int                           -- ^ Length of the entity, in UTF-16 code units
    , DiffEntity -> Maybe DiffEntityType
_type   :: Maybe DiffEntityType.DiffEntityType -- ^ Type of the entity
    }
  deriving (DiffEntity -> DiffEntity -> Bool
(DiffEntity -> DiffEntity -> Bool)
-> (DiffEntity -> DiffEntity -> Bool) -> Eq DiffEntity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DiffEntity -> DiffEntity -> Bool
== :: DiffEntity -> DiffEntity -> Bool
$c/= :: DiffEntity -> DiffEntity -> Bool
/= :: DiffEntity -> DiffEntity -> Bool
Eq, Int -> DiffEntity -> ShowS
[DiffEntity] -> ShowS
DiffEntity -> String
(Int -> DiffEntity -> ShowS)
-> (DiffEntity -> String)
-> ([DiffEntity] -> ShowS)
-> Show DiffEntity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DiffEntity -> ShowS
showsPrec :: Int -> DiffEntity -> ShowS
$cshow :: DiffEntity -> String
show :: DiffEntity -> String
$cshowList :: [DiffEntity] -> ShowS
showList :: [DiffEntity] -> ShowS
Show)

instance I.ShortShow DiffEntity where
  shortShow :: DiffEntity -> String
shortShow DiffEntity
    { offset :: DiffEntity -> Maybe Int
offset  = Maybe Int
offset_
    , _length :: DiffEntity -> Maybe Int
_length = Maybe Int
_length_
    , _type :: DiffEntity -> Maybe DiffEntityType
_type   = Maybe DiffEntityType
_type_
    }
      = String
"DiffEntity"
        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 DiffEntityType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe DiffEntityType
_type_
        ]

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