module TD.Data.RemoteFile
  (RemoteFile(..)) 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

data RemoteFile
  = RemoteFile -- ^ Represents a remote file
    { RemoteFile -> Maybe Text
_id                    :: Maybe T.Text -- ^ Remote file identifier; may be empty. Can be used by the current user across application restarts or even from other devices. Uniquely identifies a file, but a file can have a lot of different valid identifiers. If the identifier starts with "http://" or "https://", it represents the HTTP URL of the file. TDLib is currently unable to download files if only their URL is known. If downloadFile/addFileToDownloads is called on such a file or if it is sent to a secret chat, TDLib starts a file generation process by sending updateFileGenerationStart to the application with the HTTP URL in the original_path and "#url#" as the conversion string. Application must generate the file by downloading it to the specified location
    , RemoteFile -> Maybe Text
unique_id              :: Maybe T.Text -- ^ Unique file identifier; may be empty if unknown. The unique file identifier which is the same for the same file even for different users and is persistent over time
    , RemoteFile -> Maybe Bool
is_uploading_active    :: Maybe Bool   -- ^ True, if the file is currently being uploaded (or a remote copy is being generated by some other means)
    , RemoteFile -> Maybe Bool
is_uploading_completed :: Maybe Bool   -- ^ True, if a remote copy is fully available
    , RemoteFile -> Maybe Int
uploaded_size          :: Maybe Int    -- ^ Size of the remote available part of the file, in bytes; 0 if unknown
    }
  deriving (RemoteFile -> RemoteFile -> Bool
(RemoteFile -> RemoteFile -> Bool)
-> (RemoteFile -> RemoteFile -> Bool) -> Eq RemoteFile
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RemoteFile -> RemoteFile -> Bool
== :: RemoteFile -> RemoteFile -> Bool
$c/= :: RemoteFile -> RemoteFile -> Bool
/= :: RemoteFile -> RemoteFile -> Bool
Eq, Int -> RemoteFile -> ShowS
[RemoteFile] -> ShowS
RemoteFile -> String
(Int -> RemoteFile -> ShowS)
-> (RemoteFile -> String)
-> ([RemoteFile] -> ShowS)
-> Show RemoteFile
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RemoteFile -> ShowS
showsPrec :: Int -> RemoteFile -> ShowS
$cshow :: RemoteFile -> String
show :: RemoteFile -> String
$cshowList :: [RemoteFile] -> ShowS
showList :: [RemoteFile] -> ShowS
Show)

instance I.ShortShow RemoteFile where
  shortShow :: RemoteFile -> String
shortShow RemoteFile
    { _id :: RemoteFile -> Maybe Text
_id                    = Maybe Text
_id_
    , unique_id :: RemoteFile -> Maybe Text
unique_id              = Maybe Text
unique_id_
    , is_uploading_active :: RemoteFile -> Maybe Bool
is_uploading_active    = Maybe Bool
is_uploading_active_
    , is_uploading_completed :: RemoteFile -> Maybe Bool
is_uploading_completed = Maybe Bool
is_uploading_completed_
    , uploaded_size :: RemoteFile -> Maybe Int
uploaded_size          = Maybe Int
uploaded_size_
    }
      = String
"RemoteFile"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"                    String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
_id_
        , String
"unique_id"              String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
unique_id_
        , String
"is_uploading_active"    String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_uploading_active_
        , String
"is_uploading_completed" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_uploading_completed_
        , String
"uploaded_size"          String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
uploaded_size_
        ]

instance AT.FromJSON RemoteFile where
  parseJSON :: Value -> Parser RemoteFile
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
"remoteFile" -> Value -> Parser RemoteFile
parseRemoteFile Value
v
      String
_            -> Parser RemoteFile
forall a. Monoid a => a
mempty
    
    where
      parseRemoteFile :: A.Value -> AT.Parser RemoteFile
      parseRemoteFile :: Value -> Parser RemoteFile
parseRemoteFile = String
-> (Object -> Parser RemoteFile) -> Value -> Parser RemoteFile
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"RemoteFile" ((Object -> Parser RemoteFile) -> Value -> Parser RemoteFile)
-> (Object -> Parser RemoteFile) -> Value -> Parser RemoteFile
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
_id_                    <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe Text
unique_id_              <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"unique_id"
        Maybe Bool
is_uploading_active_    <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_uploading_active"
        Maybe Bool
is_uploading_completed_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_uploading_completed"
        Maybe Int
uploaded_size_          <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"uploaded_size"
        RemoteFile -> Parser RemoteFile
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RemoteFile -> Parser RemoteFile)
-> RemoteFile -> Parser RemoteFile
forall a b. (a -> b) -> a -> b
$ RemoteFile
          { _id :: Maybe Text
_id                    = Maybe Text
_id_
          , unique_id :: Maybe Text
unique_id              = Maybe Text
unique_id_
          , is_uploading_active :: Maybe Bool
is_uploading_active    = Maybe Bool
is_uploading_active_
          , is_uploading_completed :: Maybe Bool
is_uploading_completed = Maybe Bool
is_uploading_completed_
          , uploaded_size :: Maybe Int
uploaded_size          = Maybe Int
uploaded_size_
          }
  parseJSON Value
_ = Parser RemoteFile
forall a. Monoid a => a
mempty