module TD.Data.File
  (File(..)) 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.LocalFile as LocalFile
import qualified TD.Data.RemoteFile as RemoteFile

data File
  = File -- ^ Represents a file
    { File -> Maybe Int
_id           :: Maybe Int                   -- ^ Unique file identifier
    , File -> Maybe Int
size          :: Maybe Int                   -- ^ File size, in bytes; 0 if unknown
    , File -> Maybe Int
expected_size :: Maybe Int                   -- ^ Approximate file size in bytes in case the exact file size is unknown. Can be used to show download/upload progress
    , File -> Maybe LocalFile
local         :: Maybe LocalFile.LocalFile   -- ^ Information about the local copy of the file
    , File -> Maybe RemoteFile
remote        :: Maybe RemoteFile.RemoteFile -- ^ Information about the remote copy of the file
    }
  deriving (File -> File -> Bool
(File -> File -> Bool) -> (File -> File -> Bool) -> Eq File
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: File -> File -> Bool
== :: File -> File -> Bool
$c/= :: File -> File -> Bool
/= :: File -> File -> Bool
Eq, Int -> File -> ShowS
[File] -> ShowS
File -> String
(Int -> File -> ShowS)
-> (File -> String) -> ([File] -> ShowS) -> Show File
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> File -> ShowS
showsPrec :: Int -> File -> ShowS
$cshow :: File -> String
show :: File -> String
$cshowList :: [File] -> ShowS
showList :: [File] -> ShowS
Show)

instance I.ShortShow File where
  shortShow :: File -> String
shortShow File
    { _id :: File -> Maybe Int
_id           = Maybe Int
_id_
    , size :: File -> Maybe Int
size          = Maybe Int
size_
    , expected_size :: File -> Maybe Int
expected_size = Maybe Int
expected_size_
    , local :: File -> Maybe LocalFile
local         = Maybe LocalFile
local_
    , remote :: File -> Maybe RemoteFile
remote        = Maybe RemoteFile
remote_
    }
      = String
"File"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"           String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_id_
        , String
"size"          String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
size_
        , String
"expected_size" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
expected_size_
        , String
"local"         String -> Maybe LocalFile -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe LocalFile
local_
        , String
"remote"        String -> Maybe RemoteFile -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe RemoteFile
remote_
        ]

instance AT.FromJSON File where
  parseJSON :: Value -> Parser File
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
"file" -> Value -> Parser File
parseFile Value
v
      String
_      -> Parser File
forall a. Monoid a => a
mempty
    
    where
      parseFile :: A.Value -> AT.Parser File
      parseFile :: Value -> Parser File
parseFile = String -> (Object -> Parser File) -> Value -> Parser File
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"File" ((Object -> Parser File) -> Value -> Parser File)
-> (Object -> Parser File) -> Value -> Parser File
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_           <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe Int
size_          <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"size"
        Maybe Int
expected_size_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"expected_size"
        Maybe LocalFile
local_         <- Object
o Object -> Key -> Parser (Maybe LocalFile)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"local"
        Maybe RemoteFile
remote_        <- Object
o Object -> Key -> Parser (Maybe RemoteFile)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"remote"
        File -> Parser File
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (File -> Parser File) -> File -> Parser File
forall a b. (a -> b) -> a -> b
$ File
          { _id :: Maybe Int
_id           = Maybe Int
_id_
          , size :: Maybe Int
size          = Maybe Int
size_
          , expected_size :: Maybe Int
expected_size = Maybe Int
expected_size_
          , local :: Maybe LocalFile
local         = Maybe LocalFile
local_
          , remote :: Maybe RemoteFile
remote        = Maybe RemoteFile
remote_
          }
  parseJSON Value
_ = Parser File
forall a. Monoid a => a
mempty