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

data DiffText
  = DiffText -- ^ A text with some changes highlighted
    { DiffText -> Maybe Text
text     :: Maybe T.Text                  -- ^ The text
    , DiffText -> Maybe [DiffEntity]
entities :: Maybe [DiffEntity.DiffEntity] -- ^ Entities describing changes in the text. Entities doesn't mutually intersect with each other
    }
  deriving (DiffText -> DiffText -> Bool
(DiffText -> DiffText -> Bool)
-> (DiffText -> DiffText -> Bool) -> Eq DiffText
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DiffText -> DiffText -> Bool
== :: DiffText -> DiffText -> Bool
$c/= :: DiffText -> DiffText -> Bool
/= :: DiffText -> DiffText -> Bool
Eq, Int -> DiffText -> ShowS
[DiffText] -> ShowS
DiffText -> String
(Int -> DiffText -> ShowS)
-> (DiffText -> String) -> ([DiffText] -> ShowS) -> Show DiffText
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DiffText -> ShowS
showsPrec :: Int -> DiffText -> ShowS
$cshow :: DiffText -> String
show :: DiffText -> String
$cshowList :: [DiffText] -> ShowS
showList :: [DiffText] -> ShowS
Show)

instance I.ShortShow DiffText where
  shortShow :: DiffText -> String
shortShow DiffText
    { text :: DiffText -> Maybe Text
text     = Maybe Text
text_
    , entities :: DiffText -> Maybe [DiffEntity]
entities = Maybe [DiffEntity]
entities_
    }
      = String
"DiffText"
        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 [DiffEntity] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [DiffEntity]
entities_
        ]

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