module TD.Data.Notification
  (Notification(..)) 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.NotificationType as NotificationType

data Notification
  = Notification -- ^ Contains information about a notification
    { Notification -> Maybe Int
_id       :: Maybe Int                               -- ^ Unique persistent identifier of this notification
    , Notification -> Maybe Int
date      :: Maybe Int                               -- ^ Notification date
    , Notification -> Maybe Bool
is_silent :: Maybe Bool                              -- ^ True, if the notification was explicitly sent without sound
    , Notification -> Maybe NotificationType
_type     :: Maybe NotificationType.NotificationType -- ^ Notification type
    }
  deriving (Notification -> Notification -> Bool
(Notification -> Notification -> Bool)
-> (Notification -> Notification -> Bool) -> Eq Notification
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Notification -> Notification -> Bool
== :: Notification -> Notification -> Bool
$c/= :: Notification -> Notification -> Bool
/= :: Notification -> Notification -> Bool
Eq, Int -> Notification -> ShowS
[Notification] -> ShowS
Notification -> String
(Int -> Notification -> ShowS)
-> (Notification -> String)
-> ([Notification] -> ShowS)
-> Show Notification
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Notification -> ShowS
showsPrec :: Int -> Notification -> ShowS
$cshow :: Notification -> String
show :: Notification -> String
$cshowList :: [Notification] -> ShowS
showList :: [Notification] -> ShowS
Show)

instance I.ShortShow Notification where
  shortShow :: Notification -> String
shortShow Notification
    { _id :: Notification -> Maybe Int
_id       = Maybe Int
_id_
    , date :: Notification -> Maybe Int
date      = Maybe Int
date_
    , is_silent :: Notification -> Maybe Bool
is_silent = Maybe Bool
is_silent_
    , _type :: Notification -> Maybe NotificationType
_type     = Maybe NotificationType
_type_
    }
      = String
"Notification"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"       String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_id_
        , String
"date"      String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
        , String
"is_silent" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_silent_
        , String
"_type"     String -> Maybe NotificationType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe NotificationType
_type_
        ]

instance AT.FromJSON Notification where
  parseJSON :: Value -> Parser Notification
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
"notification" -> Value -> Parser Notification
parseNotification Value
v
      String
_              -> Parser Notification
forall a. Monoid a => a
mempty
    
    where
      parseNotification :: A.Value -> AT.Parser Notification
      parseNotification :: Value -> Parser Notification
parseNotification = String
-> (Object -> Parser Notification) -> Value -> Parser Notification
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Notification" ((Object -> Parser Notification) -> Value -> Parser Notification)
-> (Object -> Parser Notification) -> Value -> Parser Notification
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_       <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe Int
date_      <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"date"
        Maybe Bool
is_silent_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_silent"
        Maybe NotificationType
_type_     <- Object
o Object -> Key -> Parser (Maybe NotificationType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"type"
        Notification -> Parser Notification
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Notification -> Parser Notification)
-> Notification -> Parser Notification
forall a b. (a -> b) -> a -> b
$ Notification
          { _id :: Maybe Int
_id       = Maybe Int
_id_
          , date :: Maybe Int
date      = Maybe Int
date_
          , is_silent :: Maybe Bool
is_silent = Maybe Bool
is_silent_
          , _type :: Maybe NotificationType
_type     = Maybe NotificationType
_type_
          }
  parseJSON Value
_ = Parser Notification
forall a. Monoid a => a
mempty