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

data DatedFile
  = DatedFile -- ^ File with the date it was uploaded
    { DatedFile -> Maybe File
file :: Maybe File.File -- ^ The file
    , DatedFile -> Maybe Int
date :: Maybe Int       -- ^ Point in time (Unix timestamp) when the file was uploaded
    }
  deriving (DatedFile -> DatedFile -> Bool
(DatedFile -> DatedFile -> Bool)
-> (DatedFile -> DatedFile -> Bool) -> Eq DatedFile
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DatedFile -> DatedFile -> Bool
== :: DatedFile -> DatedFile -> Bool
$c/= :: DatedFile -> DatedFile -> Bool
/= :: DatedFile -> DatedFile -> Bool
Eq, Int -> DatedFile -> ShowS
[DatedFile] -> ShowS
DatedFile -> String
(Int -> DatedFile -> ShowS)
-> (DatedFile -> String)
-> ([DatedFile] -> ShowS)
-> Show DatedFile
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DatedFile -> ShowS
showsPrec :: Int -> DatedFile -> ShowS
$cshow :: DatedFile -> String
show :: DatedFile -> String
$cshowList :: [DatedFile] -> ShowS
showList :: [DatedFile] -> ShowS
Show)

instance I.ShortShow DatedFile where
  shortShow :: DatedFile -> String
shortShow DatedFile
    { file :: DatedFile -> Maybe File
file = Maybe File
file_
    , date :: DatedFile -> Maybe Int
date = Maybe Int
date_
    }
      = String
"DatedFile"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"file" String -> Maybe File -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe File
file_
        , String
"date" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
        ]

instance AT.FromJSON DatedFile where
  parseJSON :: Value -> Parser DatedFile
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
"datedFile" -> Value -> Parser DatedFile
parseDatedFile Value
v
      String
_           -> Parser DatedFile
forall a. Monoid a => a
mempty
    
    where
      parseDatedFile :: A.Value -> AT.Parser DatedFile
      parseDatedFile :: Value -> Parser DatedFile
parseDatedFile = String -> (Object -> Parser DatedFile) -> Value -> Parser DatedFile
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"DatedFile" ((Object -> Parser DatedFile) -> Value -> Parser DatedFile)
-> (Object -> Parser DatedFile) -> Value -> Parser DatedFile
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe File
file_ <- Object
o Object -> Key -> Parser (Maybe File)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"file"
        Maybe Int
date_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"date"
        DatedFile -> Parser DatedFile
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DatedFile -> Parser DatedFile) -> DatedFile -> Parser DatedFile
forall a b. (a -> b) -> a -> b
$ DatedFile
          { file :: Maybe File
file = Maybe File
file_
          , date :: Maybe Int
date = Maybe Int
date_
          }
  parseJSON Value
_ = Parser DatedFile
forall a. Monoid a => a
mempty