module TD.Data.VoiceNote
  (VoiceNote(..)) where

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

data VoiceNote
  = VoiceNote -- ^ Describes a voice note
    { VoiceNote -> Maybe Int
duration                  :: Maybe Int                                             -- ^ Duration of the voice note, in seconds; as defined by the sender
    , VoiceNote -> Maybe ByteString
waveform                  :: Maybe BS.ByteString                                   -- ^ A waveform representation of the voice note in 5-bit format
    , VoiceNote -> Maybe Text
mime_type                 :: Maybe T.Text                                          -- ^ MIME type of the file; as defined by the sender. Usually, one of "audio/ogg" for Opus in an OGG container, "audio/mpeg" for an MP3 audio, or "audio/mp4" for an M4A audio
    , VoiceNote -> Maybe SpeechRecognitionResult
speech_recognition_result :: Maybe SpeechRecognitionResult.SpeechRecognitionResult -- ^ Result of speech recognition in the voice note; may be null
    , VoiceNote -> Maybe File
voice                     :: Maybe File.File                                       -- ^ File containing the voice note
    }
  deriving (VoiceNote -> VoiceNote -> Bool
(VoiceNote -> VoiceNote -> Bool)
-> (VoiceNote -> VoiceNote -> Bool) -> Eq VoiceNote
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VoiceNote -> VoiceNote -> Bool
== :: VoiceNote -> VoiceNote -> Bool
$c/= :: VoiceNote -> VoiceNote -> Bool
/= :: VoiceNote -> VoiceNote -> Bool
Eq, Int -> VoiceNote -> ShowS
[VoiceNote] -> ShowS
VoiceNote -> String
(Int -> VoiceNote -> ShowS)
-> (VoiceNote -> String)
-> ([VoiceNote] -> ShowS)
-> Show VoiceNote
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VoiceNote -> ShowS
showsPrec :: Int -> VoiceNote -> ShowS
$cshow :: VoiceNote -> String
show :: VoiceNote -> String
$cshowList :: [VoiceNote] -> ShowS
showList :: [VoiceNote] -> ShowS
Show)

instance I.ShortShow VoiceNote where
  shortShow :: VoiceNote -> String
shortShow VoiceNote
    { duration :: VoiceNote -> Maybe Int
duration                  = Maybe Int
duration_
    , waveform :: VoiceNote -> Maybe ByteString
waveform                  = Maybe ByteString
waveform_
    , mime_type :: VoiceNote -> Maybe Text
mime_type                 = Maybe Text
mime_type_
    , speech_recognition_result :: VoiceNote -> Maybe SpeechRecognitionResult
speech_recognition_result = Maybe SpeechRecognitionResult
speech_recognition_result_
    , voice :: VoiceNote -> Maybe File
voice                     = Maybe File
voice_
    }
      = String
"VoiceNote"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"duration"                  String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
duration_
        , String
"waveform"                  String -> Maybe ByteString -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ByteString
waveform_
        , String
"mime_type"                 String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
mime_type_
        , String
"speech_recognition_result" String -> Maybe SpeechRecognitionResult -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe SpeechRecognitionResult
speech_recognition_result_
        , String
"voice"                     String -> Maybe File -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe File
voice_
        ]

instance AT.FromJSON VoiceNote where
  parseJSON :: Value -> Parser VoiceNote
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
"voiceNote" -> Value -> Parser VoiceNote
parseVoiceNote Value
v
      String
_           -> Parser VoiceNote
forall a. Monoid a => a
mempty
    
    where
      parseVoiceNote :: A.Value -> AT.Parser VoiceNote
      parseVoiceNote :: Value -> Parser VoiceNote
parseVoiceNote = String -> (Object -> Parser VoiceNote) -> Value -> Parser VoiceNote
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"VoiceNote" ((Object -> Parser VoiceNote) -> Value -> Parser VoiceNote)
-> (Object -> Parser VoiceNote) -> Value -> Parser VoiceNote
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
duration_                  <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"duration"
        Maybe ByteString
waveform_                  <- (String -> ByteString) -> Maybe String -> Maybe ByteString
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> ByteString
I.readBytes (Maybe String -> Maybe ByteString)
-> Parser (Maybe String) -> Parser (Maybe ByteString)
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
"waveform"
        Maybe Text
mime_type_                 <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"mime_type"
        Maybe SpeechRecognitionResult
speech_recognition_result_ <- Object
o Object -> Key -> Parser (Maybe SpeechRecognitionResult)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"speech_recognition_result"
        Maybe File
voice_                     <- Object
o Object -> Key -> Parser (Maybe File)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"voice"
        VoiceNote -> Parser VoiceNote
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (VoiceNote -> Parser VoiceNote) -> VoiceNote -> Parser VoiceNote
forall a b. (a -> b) -> a -> b
$ VoiceNote
          { duration :: Maybe Int
duration                  = Maybe Int
duration_
          , waveform :: Maybe ByteString
waveform                  = Maybe ByteString
waveform_
          , mime_type :: Maybe Text
mime_type                 = Maybe Text
mime_type_
          , speech_recognition_result :: Maybe SpeechRecognitionResult
speech_recognition_result = Maybe SpeechRecognitionResult
speech_recognition_result_
          , voice :: Maybe File
voice                     = Maybe File
voice_
          }
  parseJSON Value
_ = Parser VoiceNote
forall a. Monoid a => a
mempty