module TD.Data.StorageStatistics
  (StorageStatistics(..)) 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.StorageStatisticsByChat as StorageStatisticsByChat

data StorageStatistics
  = StorageStatistics -- ^ Contains the exact storage usage statistics split by chats and file type
    { StorageStatistics -> Maybe Int
size    :: Maybe Int                                               -- ^ Total size of files, in bytes
    , StorageStatistics -> Maybe Int
count   :: Maybe Int                                               -- ^ Total number of files
    , StorageStatistics -> Maybe [StorageStatisticsByChat]
by_chat :: Maybe [StorageStatisticsByChat.StorageStatisticsByChat] -- ^ Statistics split by chats
    }
  deriving (StorageStatistics -> StorageStatistics -> Bool
(StorageStatistics -> StorageStatistics -> Bool)
-> (StorageStatistics -> StorageStatistics -> Bool)
-> Eq StorageStatistics
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StorageStatistics -> StorageStatistics -> Bool
== :: StorageStatistics -> StorageStatistics -> Bool
$c/= :: StorageStatistics -> StorageStatistics -> Bool
/= :: StorageStatistics -> StorageStatistics -> Bool
Eq, Int -> StorageStatistics -> ShowS
[StorageStatistics] -> ShowS
StorageStatistics -> String
(Int -> StorageStatistics -> ShowS)
-> (StorageStatistics -> String)
-> ([StorageStatistics] -> ShowS)
-> Show StorageStatistics
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StorageStatistics -> ShowS
showsPrec :: Int -> StorageStatistics -> ShowS
$cshow :: StorageStatistics -> String
show :: StorageStatistics -> String
$cshowList :: [StorageStatistics] -> ShowS
showList :: [StorageStatistics] -> ShowS
Show)

instance I.ShortShow StorageStatistics where
  shortShow :: StorageStatistics -> String
shortShow StorageStatistics
    { size :: StorageStatistics -> Maybe Int
size    = Maybe Int
size_
    , count :: StorageStatistics -> Maybe Int
count   = Maybe Int
count_
    , by_chat :: StorageStatistics -> Maybe [StorageStatisticsByChat]
by_chat = Maybe [StorageStatisticsByChat]
by_chat_
    }
      = String
"StorageStatistics"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"size"    String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
size_
        , String
"count"   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
count_
        , String
"by_chat" String -> Maybe [StorageStatisticsByChat] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [StorageStatisticsByChat]
by_chat_
        ]

instance AT.FromJSON StorageStatistics where
  parseJSON :: Value -> Parser StorageStatistics
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
"storageStatistics" -> Value -> Parser StorageStatistics
parseStorageStatistics Value
v
      String
_                   -> Parser StorageStatistics
forall a. Monoid a => a
mempty
    
    where
      parseStorageStatistics :: A.Value -> AT.Parser StorageStatistics
      parseStorageStatistics :: Value -> Parser StorageStatistics
parseStorageStatistics = String
-> (Object -> Parser StorageStatistics)
-> Value
-> Parser StorageStatistics
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StorageStatistics" ((Object -> Parser StorageStatistics)
 -> Value -> Parser StorageStatistics)
-> (Object -> Parser StorageStatistics)
-> Value
-> Parser StorageStatistics
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
size_    <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"size"
        Maybe Int
count_   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"count"
        Maybe [StorageStatisticsByChat]
by_chat_ <- Object
o Object -> Key -> Parser (Maybe [StorageStatisticsByChat])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"by_chat"
        StorageStatistics -> Parser StorageStatistics
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StorageStatistics -> Parser StorageStatistics)
-> StorageStatistics -> Parser StorageStatistics
forall a b. (a -> b) -> a -> b
$ StorageStatistics
          { size :: Maybe Int
size    = Maybe Int
size_
          , count :: Maybe Int
count   = Maybe Int
count_
          , by_chat :: Maybe [StorageStatisticsByChat]
by_chat = Maybe [StorageStatisticsByChat]
by_chat_
          }
  parseJSON Value
_ = Parser StorageStatistics
forall a. Monoid a => a
mempty