module TD.Data.InputVoiceNote
  ( InputVoiceNote(..)    
  , defaultInputVoiceNote 
  ) 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.InputFile as InputFile
import qualified Data.ByteString as BS

data InputVoiceNote
  = InputVoiceNote -- ^ A video note to be sent
    { InputVoiceNote -> Maybe InputFile
voice_note :: Maybe InputFile.InputFile -- ^ Voice note file to be sent. The voice note must be encoded with the Opus codec and stored inside an OGG container with a single audio channel, or be in MP3 or M4A format as regular audio
    , InputVoiceNote -> Maybe Int
duration   :: Maybe Int                 -- ^ Duration of the voice note, in seconds
    , InputVoiceNote -> Maybe ByteString
waveform   :: Maybe BS.ByteString       -- ^ Waveform representation of the voice note in 5-bit format
    }
  deriving (InputVoiceNote -> InputVoiceNote -> Bool
(InputVoiceNote -> InputVoiceNote -> Bool)
-> (InputVoiceNote -> InputVoiceNote -> Bool) -> Eq InputVoiceNote
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InputVoiceNote -> InputVoiceNote -> Bool
== :: InputVoiceNote -> InputVoiceNote -> Bool
$c/= :: InputVoiceNote -> InputVoiceNote -> Bool
/= :: InputVoiceNote -> InputVoiceNote -> Bool
Eq, Int -> InputVoiceNote -> ShowS
[InputVoiceNote] -> ShowS
InputVoiceNote -> String
(Int -> InputVoiceNote -> ShowS)
-> (InputVoiceNote -> String)
-> ([InputVoiceNote] -> ShowS)
-> Show InputVoiceNote
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InputVoiceNote -> ShowS
showsPrec :: Int -> InputVoiceNote -> ShowS
$cshow :: InputVoiceNote -> String
show :: InputVoiceNote -> String
$cshowList :: [InputVoiceNote] -> ShowS
showList :: [InputVoiceNote] -> ShowS
Show)

instance I.ShortShow InputVoiceNote where
  shortShow :: InputVoiceNote -> String
shortShow InputVoiceNote
    { voice_note :: InputVoiceNote -> Maybe InputFile
voice_note = Maybe InputFile
voice_note_
    , duration :: InputVoiceNote -> Maybe Int
duration   = Maybe Int
duration_
    , waveform :: InputVoiceNote -> Maybe ByteString
waveform   = Maybe ByteString
waveform_
    }
      = String
"InputVoiceNote"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"voice_note" String -> Maybe InputFile -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe InputFile
voice_note_
        , 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_
        ]

instance AT.FromJSON InputVoiceNote where
  parseJSON :: Value -> Parser InputVoiceNote
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
"inputVoiceNote" -> Value -> Parser InputVoiceNote
parseInputVoiceNote Value
v
      String
_                -> Parser InputVoiceNote
forall a. Monoid a => a
mempty
    
    where
      parseInputVoiceNote :: A.Value -> AT.Parser InputVoiceNote
      parseInputVoiceNote :: Value -> Parser InputVoiceNote
parseInputVoiceNote = String
-> (Object -> Parser InputVoiceNote)
-> Value
-> Parser InputVoiceNote
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputVoiceNote" ((Object -> Parser InputVoiceNote)
 -> Value -> Parser InputVoiceNote)
-> (Object -> Parser InputVoiceNote)
-> Value
-> Parser InputVoiceNote
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe InputFile
voice_note_ <- Object
o Object -> Key -> Parser (Maybe InputFile)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"voice_note"
        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"
        InputVoiceNote -> Parser InputVoiceNote
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputVoiceNote -> Parser InputVoiceNote)
-> InputVoiceNote -> Parser InputVoiceNote
forall a b. (a -> b) -> a -> b
$ InputVoiceNote
          { voice_note :: Maybe InputFile
voice_note = Maybe InputFile
voice_note_
          , duration :: Maybe Int
duration   = Maybe Int
duration_
          , waveform :: Maybe ByteString
waveform   = Maybe ByteString
waveform_
          }
  parseJSON Value
_ = Parser InputVoiceNote
forall a. Monoid a => a
mempty

instance AT.ToJSON InputVoiceNote where
  toJSON :: InputVoiceNote -> Value
toJSON InputVoiceNote
    { voice_note :: InputVoiceNote -> Maybe InputFile
voice_note = Maybe InputFile
voice_note_
    , duration :: InputVoiceNote -> Maybe Int
duration   = Maybe Int
duration_
    , waveform :: InputVoiceNote -> Maybe ByteString
waveform   = Maybe ByteString
waveform_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"      Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"inputVoiceNote"
        , Key
"voice_note" Key -> Maybe InputFile -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe InputFile
voice_note_
        , Key
"duration"   Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
duration_
        , Key
"waveform"   Key -> Maybe Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= (ByteString -> Value) -> Maybe ByteString -> Maybe Value
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Value
I.writeBytes  Maybe ByteString
waveform_
        ]

defaultInputVoiceNote :: InputVoiceNote
defaultInputVoiceNote :: InputVoiceNote
defaultInputVoiceNote =
  InputVoiceNote
    { voice_note :: Maybe InputFile
voice_note = Maybe InputFile
forall a. Maybe a
Nothing
    , duration :: Maybe Int
duration   = Maybe Int
forall a. Maybe a
Nothing
    , waveform :: Maybe ByteString
waveform   = Maybe ByteString
forall a. Maybe a
Nothing
    }