module TD.Data.Updates
  (Updates(..)) 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.Update as Update

data Updates
  = Updates -- ^ Contains a list of updates
    { Updates -> Maybe [Update]
updates :: Maybe [Update.Update] -- ^ List of updates
    }
  deriving (Updates -> Updates -> Bool
(Updates -> Updates -> Bool)
-> (Updates -> Updates -> Bool) -> Eq Updates
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Updates -> Updates -> Bool
== :: Updates -> Updates -> Bool
$c/= :: Updates -> Updates -> Bool
/= :: Updates -> Updates -> Bool
Eq, Int -> Updates -> ShowS
[Updates] -> ShowS
Updates -> String
(Int -> Updates -> ShowS)
-> (Updates -> String) -> ([Updates] -> ShowS) -> Show Updates
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Updates -> ShowS
showsPrec :: Int -> Updates -> ShowS
$cshow :: Updates -> String
show :: Updates -> String
$cshowList :: [Updates] -> ShowS
showList :: [Updates] -> ShowS
Show)

instance I.ShortShow Updates where
  shortShow :: Updates -> String
shortShow Updates
    { updates :: Updates -> Maybe [Update]
updates = Maybe [Update]
updates_
    }
      = String
"Updates"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"updates" String -> Maybe [Update] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Update]
updates_
        ]

instance AT.FromJSON Updates where
  parseJSON :: Value -> Parser Updates
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
"updates" -> Value -> Parser Updates
parseUpdates Value
v
      String
_         -> Parser Updates
forall a. Monoid a => a
mempty
    
    where
      parseUpdates :: A.Value -> AT.Parser Updates
      parseUpdates :: Value -> Parser Updates
parseUpdates = String -> (Object -> Parser Updates) -> Value -> Parser Updates
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Updates" ((Object -> Parser Updates) -> Value -> Parser Updates)
-> (Object -> Parser Updates) -> Value -> Parser Updates
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [Update]
updates_ <- Object
o Object -> Key -> Parser (Maybe [Update])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"updates"
        Updates -> Parser Updates
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Updates -> Parser Updates) -> Updates -> Parser Updates
forall a b. (a -> b) -> a -> b
$ Updates
          { updates :: Maybe [Update]
updates = Maybe [Update]
updates_
          }
  parseJSON Value
_ = Parser Updates
forall a. Monoid a => a
mempty