module TD.Data.Seconds
  (Seconds(..)) where

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

data Seconds
  = Seconds -- ^ Contains a value representing a number of seconds
    { Seconds -> Maybe Double
seconds :: Maybe Double -- ^ Number of seconds
    }
  deriving (Seconds -> Seconds -> Bool
(Seconds -> Seconds -> Bool)
-> (Seconds -> Seconds -> Bool) -> Eq Seconds
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Seconds -> Seconds -> Bool
== :: Seconds -> Seconds -> Bool
$c/= :: Seconds -> Seconds -> Bool
/= :: Seconds -> Seconds -> Bool
Eq, Int -> Seconds -> ShowS
[Seconds] -> ShowS
Seconds -> String
(Int -> Seconds -> ShowS)
-> (Seconds -> String) -> ([Seconds] -> ShowS) -> Show Seconds
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Seconds -> ShowS
showsPrec :: Int -> Seconds -> ShowS
$cshow :: Seconds -> String
show :: Seconds -> String
$cshowList :: [Seconds] -> ShowS
showList :: [Seconds] -> ShowS
Show)

instance I.ShortShow Seconds where
  shortShow :: Seconds -> String
shortShow Seconds
    { seconds :: Seconds -> Maybe Double
seconds = Maybe Double
seconds_
    }
      = String
"Seconds"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"seconds" String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
seconds_
        ]

instance AT.FromJSON Seconds where
  parseJSON :: Value -> Parser Seconds
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
"seconds" -> Value -> Parser Seconds
parseSeconds Value
v
      String
_         -> Parser Seconds
forall a. Monoid a => a
mempty
    
    where
      parseSeconds :: A.Value -> AT.Parser Seconds
      parseSeconds :: Value -> Parser Seconds
parseSeconds = String -> (Object -> Parser Seconds) -> Value -> Parser Seconds
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Seconds" ((Object -> Parser Seconds) -> Value -> Parser Seconds)
-> (Object -> Parser Seconds) -> Value -> Parser Seconds
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Double
seconds_ <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"seconds"
        Seconds -> Parser Seconds
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Seconds -> Parser Seconds) -> Seconds -> Parser Seconds
forall a b. (a -> b) -> a -> b
$ Seconds
          { seconds :: Maybe Double
seconds = Maybe Double
seconds_
          }
  parseJSON Value
_ = Parser Seconds
forall a. Monoid a => a
mempty