module TD.Data.NotificationSound
  (NotificationSound(..)) where

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

data NotificationSound
  = NotificationSound -- ^ Describes a notification sound in MP3 format
    { NotificationSound -> Maybe Int
_id      :: Maybe Int       -- ^ Unique identifier of the notification sound
    , NotificationSound -> Maybe Int
duration :: Maybe Int       -- ^ Duration of the sound, in seconds
    , NotificationSound -> Maybe Int
date     :: Maybe Int       -- ^ Point in time (Unix timestamp) when the sound was created
    , NotificationSound -> Maybe Text
title    :: Maybe T.Text    -- ^ Title of the notification sound
    , NotificationSound -> Maybe Text
_data    :: Maybe T.Text    -- ^ Arbitrary data, defined while the sound was uploaded
    , NotificationSound -> Maybe File
sound    :: Maybe File.File -- ^ File containing the sound
    }
  deriving (NotificationSound -> NotificationSound -> Bool
(NotificationSound -> NotificationSound -> Bool)
-> (NotificationSound -> NotificationSound -> Bool)
-> Eq NotificationSound
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NotificationSound -> NotificationSound -> Bool
== :: NotificationSound -> NotificationSound -> Bool
$c/= :: NotificationSound -> NotificationSound -> Bool
/= :: NotificationSound -> NotificationSound -> Bool
Eq, Int -> NotificationSound -> ShowS
[NotificationSound] -> ShowS
NotificationSound -> String
(Int -> NotificationSound -> ShowS)
-> (NotificationSound -> String)
-> ([NotificationSound] -> ShowS)
-> Show NotificationSound
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NotificationSound -> ShowS
showsPrec :: Int -> NotificationSound -> ShowS
$cshow :: NotificationSound -> String
show :: NotificationSound -> String
$cshowList :: [NotificationSound] -> ShowS
showList :: [NotificationSound] -> ShowS
Show)

instance I.ShortShow NotificationSound where
  shortShow :: NotificationSound -> String
shortShow NotificationSound
    { _id :: NotificationSound -> Maybe Int
_id      = Maybe Int
_id_
    , duration :: NotificationSound -> Maybe Int
duration = Maybe Int
duration_
    , date :: NotificationSound -> Maybe Int
date     = Maybe Int
date_
    , title :: NotificationSound -> Maybe Text
title    = Maybe Text
title_
    , _data :: NotificationSound -> Maybe Text
_data    = Maybe Text
_data_
    , sound :: NotificationSound -> Maybe File
sound    = Maybe File
sound_
    }
      = String
"NotificationSound"
        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
"duration" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
duration_
        , String
"date"     String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
        , String
"title"    String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
title_
        , String
"_data"    String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
_data_
        , String
"sound"    String -> Maybe File -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe File
sound_
        ]

instance AT.FromJSON NotificationSound where
  parseJSON :: Value -> Parser NotificationSound
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
"notificationSound" -> Value -> Parser NotificationSound
parseNotificationSound Value
v
      String
_                   -> Parser NotificationSound
forall a. Monoid a => a
mempty
    
    where
      parseNotificationSound :: A.Value -> AT.Parser NotificationSound
      parseNotificationSound :: Value -> Parser NotificationSound
parseNotificationSound = String
-> (Object -> Parser NotificationSound)
-> Value
-> Parser NotificationSound
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"NotificationSound" ((Object -> Parser NotificationSound)
 -> Value -> Parser NotificationSound)
-> (Object -> Parser NotificationSound)
-> Value
-> Parser NotificationSound
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_      <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe Int
duration_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"duration"
        Maybe Int
date_     <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"date"
        Maybe Text
title_    <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"title"
        Maybe Text
_data_    <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"data"
        Maybe File
sound_    <- Object
o Object -> Key -> Parser (Maybe File)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"sound"
        NotificationSound -> Parser NotificationSound
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NotificationSound -> Parser NotificationSound)
-> NotificationSound -> Parser NotificationSound
forall a b. (a -> b) -> a -> b
$ NotificationSound
          { _id :: Maybe Int
_id      = Maybe Int
_id_
          , duration :: Maybe Int
duration = Maybe Int
duration_
          , date :: Maybe Int
date     = Maybe Int
date_
          , title :: Maybe Text
title    = Maybe Text
title_
          , _data :: Maybe Text
_data    = Maybe Text
_data_
          , sound :: Maybe File
sound    = Maybe File
sound_
          }
  parseJSON Value
_ = Parser NotificationSound
forall a. Monoid a => a
mempty