module TD.Data.StatisticalValue
  (StatisticalValue(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I

data StatisticalValue
  = StatisticalValue -- ^ A value with information about its recent changes
    { StatisticalValue -> Maybe Double
value                  :: Maybe Double -- ^ The current value
    , StatisticalValue -> Maybe Double
previous_value         :: Maybe Double -- ^ The value for the previous day
    , StatisticalValue -> Maybe Double
growth_rate_percentage :: Maybe Double -- ^ The growth rate of the value, as a percentage
    }
  deriving (StatisticalValue -> StatisticalValue -> Bool
(StatisticalValue -> StatisticalValue -> Bool)
-> (StatisticalValue -> StatisticalValue -> Bool)
-> Eq StatisticalValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StatisticalValue -> StatisticalValue -> Bool
== :: StatisticalValue -> StatisticalValue -> Bool
$c/= :: StatisticalValue -> StatisticalValue -> Bool
/= :: StatisticalValue -> StatisticalValue -> Bool
Eq, Int -> StatisticalValue -> ShowS
[StatisticalValue] -> ShowS
StatisticalValue -> String
(Int -> StatisticalValue -> ShowS)
-> (StatisticalValue -> String)
-> ([StatisticalValue] -> ShowS)
-> Show StatisticalValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StatisticalValue -> ShowS
showsPrec :: Int -> StatisticalValue -> ShowS
$cshow :: StatisticalValue -> String
show :: StatisticalValue -> String
$cshowList :: [StatisticalValue] -> ShowS
showList :: [StatisticalValue] -> ShowS
Show)

instance I.ShortShow StatisticalValue where
  shortShow :: StatisticalValue -> String
shortShow StatisticalValue
    { value :: StatisticalValue -> Maybe Double
value                  = Maybe Double
value_
    , previous_value :: StatisticalValue -> Maybe Double
previous_value         = Maybe Double
previous_value_
    , growth_rate_percentage :: StatisticalValue -> Maybe Double
growth_rate_percentage = Maybe Double
growth_rate_percentage_
    }
      = String
"StatisticalValue"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"value"                  String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
value_
        , String
"previous_value"         String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
previous_value_
        , String
"growth_rate_percentage" String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
growth_rate_percentage_
        ]

instance AT.FromJSON StatisticalValue where
  parseJSON :: Value -> Parser StatisticalValue
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
"statisticalValue" -> Value -> Parser StatisticalValue
parseStatisticalValue Value
v
      String
_                  -> Parser StatisticalValue
forall a. Monoid a => a
mempty
    
    where
      parseStatisticalValue :: A.Value -> AT.Parser StatisticalValue
      parseStatisticalValue :: Value -> Parser StatisticalValue
parseStatisticalValue = String
-> (Object -> Parser StatisticalValue)
-> Value
-> Parser StatisticalValue
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StatisticalValue" ((Object -> Parser StatisticalValue)
 -> Value -> Parser StatisticalValue)
-> (Object -> Parser StatisticalValue)
-> Value
-> Parser StatisticalValue
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Double
value_                  <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"value"
        Maybe Double
previous_value_         <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"previous_value"
        Maybe Double
growth_rate_percentage_ <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"growth_rate_percentage"
        StatisticalValue -> Parser StatisticalValue
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StatisticalValue -> Parser StatisticalValue)
-> StatisticalValue -> Parser StatisticalValue
forall a b. (a -> b) -> a -> b
$ StatisticalValue
          { value :: Maybe Double
value                  = Maybe Double
value_
          , previous_value :: Maybe Double
previous_value         = Maybe Double
previous_value_
          , growth_rate_percentage :: Maybe Double
growth_rate_percentage = Maybe Double
growth_rate_percentage_
          }
  parseJSON Value
_ = Parser StatisticalValue
forall a. Monoid a => a
mempty