module TD.Data.LocalFile
  (LocalFile(..)) 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 LocalFile
  = LocalFile -- ^ Represents a local file
    { LocalFile -> Maybe Text
path                     :: Maybe T.Text -- ^ Local path to the locally available file part; may be empty
    , LocalFile -> Maybe Bool
can_be_downloaded        :: Maybe Bool   -- ^ True, if it is possible to download or generate the file
    , LocalFile -> Maybe Bool
can_be_deleted           :: Maybe Bool   -- ^ True, if the file can be deleted
    , LocalFile -> Maybe Bool
is_downloading_active    :: Maybe Bool   -- ^ True, if the file is currently being downloaded (or a local copy is being generated by some other means)
    , LocalFile -> Maybe Bool
is_downloading_completed :: Maybe Bool   -- ^ True, if the local copy is fully available
    , LocalFile -> Maybe Int
download_offset          :: Maybe Int    -- ^ Download will be started from this offset. downloaded_prefix_size is calculated from this offset
    , LocalFile -> Maybe Int
downloaded_prefix_size   :: Maybe Int    -- ^ If is_downloading_completed is false, then only some prefix of the file starting from download_offset is ready to be read. downloaded_prefix_size is the size of that prefix in bytes
    , LocalFile -> Maybe Int
downloaded_size          :: Maybe Int    -- ^ Total downloaded file size, in bytes. Can be used only for calculating download progress. The actual file size may be bigger, and some parts of it may contain garbage
    }
  deriving (LocalFile -> LocalFile -> Bool
(LocalFile -> LocalFile -> Bool)
-> (LocalFile -> LocalFile -> Bool) -> Eq LocalFile
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LocalFile -> LocalFile -> Bool
== :: LocalFile -> LocalFile -> Bool
$c/= :: LocalFile -> LocalFile -> Bool
/= :: LocalFile -> LocalFile -> Bool
Eq, Int -> LocalFile -> ShowS
[LocalFile] -> ShowS
LocalFile -> String
(Int -> LocalFile -> ShowS)
-> (LocalFile -> String)
-> ([LocalFile] -> ShowS)
-> Show LocalFile
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LocalFile -> ShowS
showsPrec :: Int -> LocalFile -> ShowS
$cshow :: LocalFile -> String
show :: LocalFile -> String
$cshowList :: [LocalFile] -> ShowS
showList :: [LocalFile] -> ShowS
Show)

instance I.ShortShow LocalFile where
  shortShow :: LocalFile -> String
shortShow LocalFile
    { path :: LocalFile -> Maybe Text
path                     = Maybe Text
path_
    , can_be_downloaded :: LocalFile -> Maybe Bool
can_be_downloaded        = Maybe Bool
can_be_downloaded_
    , can_be_deleted :: LocalFile -> Maybe Bool
can_be_deleted           = Maybe Bool
can_be_deleted_
    , is_downloading_active :: LocalFile -> Maybe Bool
is_downloading_active    = Maybe Bool
is_downloading_active_
    , is_downloading_completed :: LocalFile -> Maybe Bool
is_downloading_completed = Maybe Bool
is_downloading_completed_
    , download_offset :: LocalFile -> Maybe Int
download_offset          = Maybe Int
download_offset_
    , downloaded_prefix_size :: LocalFile -> Maybe Int
downloaded_prefix_size   = Maybe Int
downloaded_prefix_size_
    , downloaded_size :: LocalFile -> Maybe Int
downloaded_size          = Maybe Int
downloaded_size_
    }
      = String
"LocalFile"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"path"                     String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
path_
        , String
"can_be_downloaded"        String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
can_be_downloaded_
        , String
"can_be_deleted"           String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
can_be_deleted_
        , String
"is_downloading_active"    String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_downloading_active_
        , String
"is_downloading_completed" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_downloading_completed_
        , String
"download_offset"          String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
download_offset_
        , String
"downloaded_prefix_size"   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
downloaded_prefix_size_
        , String
"downloaded_size"          String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
downloaded_size_
        ]

instance AT.FromJSON LocalFile where
  parseJSON :: Value -> Parser LocalFile
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
"localFile" -> Value -> Parser LocalFile
parseLocalFile Value
v
      String
_           -> Parser LocalFile
forall a. Monoid a => a
mempty
    
    where
      parseLocalFile :: A.Value -> AT.Parser LocalFile
      parseLocalFile :: Value -> Parser LocalFile
parseLocalFile = String -> (Object -> Parser LocalFile) -> Value -> Parser LocalFile
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"LocalFile" ((Object -> Parser LocalFile) -> Value -> Parser LocalFile)
-> (Object -> Parser LocalFile) -> Value -> Parser LocalFile
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
path_                     <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"path"
        Maybe Bool
can_be_downloaded_        <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"can_be_downloaded"
        Maybe Bool
can_be_deleted_           <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"can_be_deleted"
        Maybe Bool
is_downloading_active_    <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_downloading_active"
        Maybe Bool
is_downloading_completed_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_downloading_completed"
        Maybe Int
download_offset_          <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"download_offset"
        Maybe Int
downloaded_prefix_size_   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"downloaded_prefix_size"
        Maybe Int
downloaded_size_          <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"downloaded_size"
        LocalFile -> Parser LocalFile
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LocalFile -> Parser LocalFile) -> LocalFile -> Parser LocalFile
forall a b. (a -> b) -> a -> b
$ LocalFile
          { path :: Maybe Text
path                     = Maybe Text
path_
          , can_be_downloaded :: Maybe Bool
can_be_downloaded        = Maybe Bool
can_be_downloaded_
          , can_be_deleted :: Maybe Bool
can_be_deleted           = Maybe Bool
can_be_deleted_
          , is_downloading_active :: Maybe Bool
is_downloading_active    = Maybe Bool
is_downloading_active_
          , is_downloading_completed :: Maybe Bool
is_downloading_completed = Maybe Bool
is_downloading_completed_
          , download_offset :: Maybe Int
download_offset          = Maybe Int
download_offset_
          , downloaded_prefix_size :: Maybe Int
downloaded_prefix_size   = Maybe Int
downloaded_prefix_size_
          , downloaded_size :: Maybe Int
downloaded_size          = Maybe Int
downloaded_size_
          }
  parseJSON Value
_ = Parser LocalFile
forall a. Monoid a => a
mempty