module TD.Data.VideoNote
  (VideoNote(..)) 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 TD.Data.Minithumbnail as Minithumbnail
import qualified TD.Data.Thumbnail as Thumbnail
import qualified TD.Data.SpeechRecognitionResult as SpeechRecognitionResult
import qualified TD.Data.File as File

data VideoNote
  = VideoNote -- ^ Describes a video note. The video must be equal in width and height, cropped to a circle, and stored in MPEG4 format
    { VideoNote -> Maybe Int
duration                  :: Maybe Int                                             -- ^ Duration of the video, in seconds; as defined by the sender
    , VideoNote -> Maybe ByteString
waveform                  :: Maybe BS.ByteString                                   -- ^ A waveform representation of the video note's audio in 5-bit format; may be empty if unknown
    , VideoNote -> Maybe Int
_length                   :: Maybe Int                                             -- ^ Video width and height; as defined by the sender
    , VideoNote -> Maybe Minithumbnail
minithumbnail             :: Maybe Minithumbnail.Minithumbnail                     -- ^ Video minithumbnail; may be null
    , VideoNote -> Maybe Thumbnail
thumbnail                 :: Maybe Thumbnail.Thumbnail                             -- ^ Video thumbnail in JPEG format; as defined by the sender; may be null
    , VideoNote -> Maybe SpeechRecognitionResult
speech_recognition_result :: Maybe SpeechRecognitionResult.SpeechRecognitionResult -- ^ Result of speech recognition in the video note; may be null
    , VideoNote -> Maybe File
video                     :: Maybe File.File                                       -- ^ File containing the video
    }
  deriving (VideoNote -> VideoNote -> Bool
(VideoNote -> VideoNote -> Bool)
-> (VideoNote -> VideoNote -> Bool) -> Eq VideoNote
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VideoNote -> VideoNote -> Bool
== :: VideoNote -> VideoNote -> Bool
$c/= :: VideoNote -> VideoNote -> Bool
/= :: VideoNote -> VideoNote -> Bool
Eq, Int -> VideoNote -> ShowS
[VideoNote] -> ShowS
VideoNote -> String
(Int -> VideoNote -> ShowS)
-> (VideoNote -> String)
-> ([VideoNote] -> ShowS)
-> Show VideoNote
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VideoNote -> ShowS
showsPrec :: Int -> VideoNote -> ShowS
$cshow :: VideoNote -> String
show :: VideoNote -> String
$cshowList :: [VideoNote] -> ShowS
showList :: [VideoNote] -> ShowS
Show)

instance I.ShortShow VideoNote where
  shortShow :: VideoNote -> String
shortShow VideoNote
    { duration :: VideoNote -> Maybe Int
duration                  = Maybe Int
duration_
    , waveform :: VideoNote -> Maybe ByteString
waveform                  = Maybe ByteString
waveform_
    , _length :: VideoNote -> Maybe Int
_length                   = Maybe Int
_length_
    , minithumbnail :: VideoNote -> Maybe Minithumbnail
minithumbnail             = Maybe Minithumbnail
minithumbnail_
    , thumbnail :: VideoNote -> Maybe Thumbnail
thumbnail                 = Maybe Thumbnail
thumbnail_
    , speech_recognition_result :: VideoNote -> Maybe SpeechRecognitionResult
speech_recognition_result = Maybe SpeechRecognitionResult
speech_recognition_result_
    , video :: VideoNote -> Maybe File
video                     = Maybe File
video_
    }
      = String
"VideoNote"
        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
"_length"                   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_length_
        , String
"minithumbnail"             String -> Maybe Minithumbnail -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Minithumbnail
minithumbnail_
        , String
"thumbnail"                 String -> Maybe Thumbnail -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Thumbnail
thumbnail_
        , String
"speech_recognition_result" String -> Maybe SpeechRecognitionResult -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe SpeechRecognitionResult
speech_recognition_result_
        , String
"video"                     String -> Maybe File -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe File
video_
        ]

instance AT.FromJSON VideoNote where
  parseJSON :: Value -> Parser VideoNote
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
"videoNote" -> Value -> Parser VideoNote
parseVideoNote Value
v
      String
_           -> Parser VideoNote
forall a. Monoid a => a
mempty
    
    where
      parseVideoNote :: A.Value -> AT.Parser VideoNote
      parseVideoNote :: Value -> Parser VideoNote
parseVideoNote = String -> (Object -> Parser VideoNote) -> Value -> Parser VideoNote
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"VideoNote" ((Object -> Parser VideoNote) -> Value -> Parser VideoNote)
-> (Object -> Parser VideoNote) -> Value -> Parser VideoNote
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 Int
_length_                   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"length"
        Maybe Minithumbnail
minithumbnail_             <- Object
o Object -> Key -> Parser (Maybe Minithumbnail)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"minithumbnail"
        Maybe Thumbnail
thumbnail_                 <- Object
o Object -> Key -> Parser (Maybe Thumbnail)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"thumbnail"
        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
video_                     <- Object
o Object -> Key -> Parser (Maybe File)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"video"
        VideoNote -> Parser VideoNote
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (VideoNote -> Parser VideoNote) -> VideoNote -> Parser VideoNote
forall a b. (a -> b) -> a -> b
$ VideoNote
          { duration :: Maybe Int
duration                  = Maybe Int
duration_
          , waveform :: Maybe ByteString
waveform                  = Maybe ByteString
waveform_
          , _length :: Maybe Int
_length                   = Maybe Int
_length_
          , minithumbnail :: Maybe Minithumbnail
minithumbnail             = Maybe Minithumbnail
minithumbnail_
          , thumbnail :: Maybe Thumbnail
thumbnail                 = Maybe Thumbnail
thumbnail_
          , speech_recognition_result :: Maybe SpeechRecognitionResult
speech_recognition_result = Maybe SpeechRecognitionResult
speech_recognition_result_
          , video :: Maybe File
video                     = Maybe File
video_
          }
  parseJSON Value
_ = Parser VideoNote
forall a. Monoid a => a
mempty