module TD.Data.NetworkStatistics
  (NetworkStatistics(..)) 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.NetworkStatisticsEntry as NetworkStatisticsEntry

data NetworkStatistics
  = NetworkStatistics -- ^ A full list of available network statistic entries
    { NetworkStatistics -> Maybe Int
since_date :: Maybe Int                                             -- ^ Point in time (Unix timestamp) from which the statistics are collected
    , NetworkStatistics -> Maybe [NetworkStatisticsEntry]
entries    :: Maybe [NetworkStatisticsEntry.NetworkStatisticsEntry] -- ^ Network statistics entries
    }
  deriving (NetworkStatistics -> NetworkStatistics -> Bool
(NetworkStatistics -> NetworkStatistics -> Bool)
-> (NetworkStatistics -> NetworkStatistics -> Bool)
-> Eq NetworkStatistics
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NetworkStatistics -> NetworkStatistics -> Bool
== :: NetworkStatistics -> NetworkStatistics -> Bool
$c/= :: NetworkStatistics -> NetworkStatistics -> Bool
/= :: NetworkStatistics -> NetworkStatistics -> Bool
Eq, Int -> NetworkStatistics -> ShowS
[NetworkStatistics] -> ShowS
NetworkStatistics -> String
(Int -> NetworkStatistics -> ShowS)
-> (NetworkStatistics -> String)
-> ([NetworkStatistics] -> ShowS)
-> Show NetworkStatistics
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NetworkStatistics -> ShowS
showsPrec :: Int -> NetworkStatistics -> ShowS
$cshow :: NetworkStatistics -> String
show :: NetworkStatistics -> String
$cshowList :: [NetworkStatistics] -> ShowS
showList :: [NetworkStatistics] -> ShowS
Show)

instance I.ShortShow NetworkStatistics where
  shortShow :: NetworkStatistics -> String
shortShow NetworkStatistics
    { since_date :: NetworkStatistics -> Maybe Int
since_date = Maybe Int
since_date_
    , entries :: NetworkStatistics -> Maybe [NetworkStatisticsEntry]
entries    = Maybe [NetworkStatisticsEntry]
entries_
    }
      = String
"NetworkStatistics"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"since_date" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
since_date_
        , String
"entries"    String -> Maybe [NetworkStatisticsEntry] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [NetworkStatisticsEntry]
entries_
        ]

instance AT.FromJSON NetworkStatistics where
  parseJSON :: Value -> Parser NetworkStatistics
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
"networkStatistics" -> Value -> Parser NetworkStatistics
parseNetworkStatistics Value
v
      String
_                   -> Parser NetworkStatistics
forall a. Monoid a => a
mempty
    
    where
      parseNetworkStatistics :: A.Value -> AT.Parser NetworkStatistics
      parseNetworkStatistics :: Value -> Parser NetworkStatistics
parseNetworkStatistics = String
-> (Object -> Parser NetworkStatistics)
-> Value
-> Parser NetworkStatistics
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"NetworkStatistics" ((Object -> Parser NetworkStatistics)
 -> Value -> Parser NetworkStatistics)
-> (Object -> Parser NetworkStatistics)
-> Value
-> Parser NetworkStatistics
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
since_date_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"since_date"
        Maybe [NetworkStatisticsEntry]
entries_    <- Object
o Object -> Key -> Parser (Maybe [NetworkStatisticsEntry])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"entries"
        NetworkStatistics -> Parser NetworkStatistics
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NetworkStatistics -> Parser NetworkStatistics)
-> NetworkStatistics -> Parser NetworkStatistics
forall a b. (a -> b) -> a -> b
$ NetworkStatistics
          { since_date :: Maybe Int
since_date = Maybe Int
since_date_
          , entries :: Maybe [NetworkStatisticsEntry]
entries    = Maybe [NetworkStatisticsEntry]
entries_
          }
  parseJSON Value
_ = Parser NetworkStatistics
forall a. Monoid a => a
mempty