module TD.Data.FactCheck
  (FactCheck(..)) 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.FormattedText as FormattedText
import qualified Data.Text as T

data FactCheck
  = FactCheck -- ^ Describes a fact-check added to the message by an independent checker
    { FactCheck -> Maybe FormattedText
text         :: Maybe FormattedText.FormattedText -- ^ Text of the fact-check
    , FactCheck -> Maybe Text
country_code :: Maybe T.Text                      -- ^ A two-letter ISO 3166-1 alpha-2 country code of the country for which the fact-check is shown
    }
  deriving (FactCheck -> FactCheck -> Bool
(FactCheck -> FactCheck -> Bool)
-> (FactCheck -> FactCheck -> Bool) -> Eq FactCheck
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FactCheck -> FactCheck -> Bool
== :: FactCheck -> FactCheck -> Bool
$c/= :: FactCheck -> FactCheck -> Bool
/= :: FactCheck -> FactCheck -> Bool
Eq, Int -> FactCheck -> ShowS
[FactCheck] -> ShowS
FactCheck -> String
(Int -> FactCheck -> ShowS)
-> (FactCheck -> String)
-> ([FactCheck] -> ShowS)
-> Show FactCheck
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FactCheck -> ShowS
showsPrec :: Int -> FactCheck -> ShowS
$cshow :: FactCheck -> String
show :: FactCheck -> String
$cshowList :: [FactCheck] -> ShowS
showList :: [FactCheck] -> ShowS
Show)

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

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