module TD.Data.PersonalDocument
  (PersonalDocument(..)) 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.DatedFile as DatedFile

data PersonalDocument
  = PersonalDocument -- ^ A personal document, containing some information about a user
    { PersonalDocument -> Maybe [DatedFile]
files       :: Maybe [DatedFile.DatedFile] -- ^ List of files containing the pages of the document
    , PersonalDocument -> Maybe [DatedFile]
translation :: Maybe [DatedFile.DatedFile] -- ^ List of files containing a certified English translation of the document
    }
  deriving (PersonalDocument -> PersonalDocument -> Bool
(PersonalDocument -> PersonalDocument -> Bool)
-> (PersonalDocument -> PersonalDocument -> Bool)
-> Eq PersonalDocument
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PersonalDocument -> PersonalDocument -> Bool
== :: PersonalDocument -> PersonalDocument -> Bool
$c/= :: PersonalDocument -> PersonalDocument -> Bool
/= :: PersonalDocument -> PersonalDocument -> Bool
Eq, Int -> PersonalDocument -> ShowS
[PersonalDocument] -> ShowS
PersonalDocument -> String
(Int -> PersonalDocument -> ShowS)
-> (PersonalDocument -> String)
-> ([PersonalDocument] -> ShowS)
-> Show PersonalDocument
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PersonalDocument -> ShowS
showsPrec :: Int -> PersonalDocument -> ShowS
$cshow :: PersonalDocument -> String
show :: PersonalDocument -> String
$cshowList :: [PersonalDocument] -> ShowS
showList :: [PersonalDocument] -> ShowS
Show)

instance I.ShortShow PersonalDocument where
  shortShow :: PersonalDocument -> String
shortShow PersonalDocument
    { files :: PersonalDocument -> Maybe [DatedFile]
files       = Maybe [DatedFile]
files_
    , translation :: PersonalDocument -> Maybe [DatedFile]
translation = Maybe [DatedFile]
translation_
    }
      = String
"PersonalDocument"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"files"       String -> Maybe [DatedFile] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [DatedFile]
files_
        , String
"translation" String -> Maybe [DatedFile] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [DatedFile]
translation_
        ]

instance AT.FromJSON PersonalDocument where
  parseJSON :: Value -> Parser PersonalDocument
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
"personalDocument" -> Value -> Parser PersonalDocument
parsePersonalDocument Value
v
      String
_                  -> Parser PersonalDocument
forall a. Monoid a => a
mempty
    
    where
      parsePersonalDocument :: A.Value -> AT.Parser PersonalDocument
      parsePersonalDocument :: Value -> Parser PersonalDocument
parsePersonalDocument = String
-> (Object -> Parser PersonalDocument)
-> Value
-> Parser PersonalDocument
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PersonalDocument" ((Object -> Parser PersonalDocument)
 -> Value -> Parser PersonalDocument)
-> (Object -> Parser PersonalDocument)
-> Value
-> Parser PersonalDocument
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [DatedFile]
files_       <- Object
o Object -> Key -> Parser (Maybe [DatedFile])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"files"
        Maybe [DatedFile]
translation_ <- Object
o Object -> Key -> Parser (Maybe [DatedFile])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"translation"
        PersonalDocument -> Parser PersonalDocument
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PersonalDocument -> Parser PersonalDocument)
-> PersonalDocument -> Parser PersonalDocument
forall a b. (a -> b) -> a -> b
$ PersonalDocument
          { files :: Maybe [DatedFile]
files       = Maybe [DatedFile]
files_
          , translation :: Maybe [DatedFile]
translation = Maybe [DatedFile]
translation_
          }
  parseJSON Value
_ = Parser PersonalDocument
forall a. Monoid a => a
mempty