module TD.Data.SpeechRecognitionResult
  (SpeechRecognitionResult(..)) 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.Error as Error

-- | Describes result of speech recognition in a voice note
data SpeechRecognitionResult
  = SpeechRecognitionResultPending -- ^ The speech recognition is ongoing
    { SpeechRecognitionResult -> Maybe Text
partial_text :: Maybe T.Text -- ^ Partially recognized text
    }
  | SpeechRecognitionResultText -- ^ The speech recognition successfully finished
    { SpeechRecognitionResult -> Maybe Text
text :: Maybe T.Text -- ^ Recognized text
    }
  | SpeechRecognitionResultError -- ^ The speech recognition failed
    { SpeechRecognitionResult -> Maybe Error
_error :: Maybe Error.Error -- ^ Recognition error. An error with a message "MSG_VOICE_TOO_LONG" is returned when media duration is too big to be recognized
    }
  deriving (SpeechRecognitionResult -> SpeechRecognitionResult -> Bool
(SpeechRecognitionResult -> SpeechRecognitionResult -> Bool)
-> (SpeechRecognitionResult -> SpeechRecognitionResult -> Bool)
-> Eq SpeechRecognitionResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SpeechRecognitionResult -> SpeechRecognitionResult -> Bool
== :: SpeechRecognitionResult -> SpeechRecognitionResult -> Bool
$c/= :: SpeechRecognitionResult -> SpeechRecognitionResult -> Bool
/= :: SpeechRecognitionResult -> SpeechRecognitionResult -> Bool
Eq, Int -> SpeechRecognitionResult -> ShowS
[SpeechRecognitionResult] -> ShowS
SpeechRecognitionResult -> String
(Int -> SpeechRecognitionResult -> ShowS)
-> (SpeechRecognitionResult -> String)
-> ([SpeechRecognitionResult] -> ShowS)
-> Show SpeechRecognitionResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SpeechRecognitionResult -> ShowS
showsPrec :: Int -> SpeechRecognitionResult -> ShowS
$cshow :: SpeechRecognitionResult -> String
show :: SpeechRecognitionResult -> String
$cshowList :: [SpeechRecognitionResult] -> ShowS
showList :: [SpeechRecognitionResult] -> ShowS
Show)

instance I.ShortShow SpeechRecognitionResult where
  shortShow :: SpeechRecognitionResult -> String
shortShow SpeechRecognitionResultPending
    { partial_text :: SpeechRecognitionResult -> Maybe Text
partial_text = Maybe Text
partial_text_
    }
      = String
"SpeechRecognitionResultPending"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"partial_text" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
partial_text_
        ]
  shortShow SpeechRecognitionResultText
    { text :: SpeechRecognitionResult -> Maybe Text
text = Maybe Text
text_
    }
      = String
"SpeechRecognitionResultText"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"text" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
text_
        ]
  shortShow SpeechRecognitionResultError
    { _error :: SpeechRecognitionResult -> Maybe Error
_error = Maybe Error
_error_
    }
      = String
"SpeechRecognitionResultError"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_error" String -> Maybe Error -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Error
_error_
        ]

instance AT.FromJSON SpeechRecognitionResult where
  parseJSON :: Value -> Parser SpeechRecognitionResult
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
"speechRecognitionResultPending" -> Value -> Parser SpeechRecognitionResult
parseSpeechRecognitionResultPending Value
v
      String
"speechRecognitionResultText"    -> Value -> Parser SpeechRecognitionResult
parseSpeechRecognitionResultText Value
v
      String
"speechRecognitionResultError"   -> Value -> Parser SpeechRecognitionResult
parseSpeechRecognitionResultError Value
v
      String
_                                -> Parser SpeechRecognitionResult
forall a. Monoid a => a
mempty
    
    where
      parseSpeechRecognitionResultPending :: A.Value -> AT.Parser SpeechRecognitionResult
      parseSpeechRecognitionResultPending :: Value -> Parser SpeechRecognitionResult
parseSpeechRecognitionResultPending = String
-> (Object -> Parser SpeechRecognitionResult)
-> Value
-> Parser SpeechRecognitionResult
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"SpeechRecognitionResultPending" ((Object -> Parser SpeechRecognitionResult)
 -> Value -> Parser SpeechRecognitionResult)
-> (Object -> Parser SpeechRecognitionResult)
-> Value
-> Parser SpeechRecognitionResult
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
partial_text_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"partial_text"
        SpeechRecognitionResult -> Parser SpeechRecognitionResult
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SpeechRecognitionResult -> Parser SpeechRecognitionResult)
-> SpeechRecognitionResult -> Parser SpeechRecognitionResult
forall a b. (a -> b) -> a -> b
$ SpeechRecognitionResultPending
          { partial_text :: Maybe Text
partial_text = Maybe Text
partial_text_
          }
      parseSpeechRecognitionResultText :: A.Value -> AT.Parser SpeechRecognitionResult
      parseSpeechRecognitionResultText :: Value -> Parser SpeechRecognitionResult
parseSpeechRecognitionResultText = String
-> (Object -> Parser SpeechRecognitionResult)
-> Value
-> Parser SpeechRecognitionResult
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"SpeechRecognitionResultText" ((Object -> Parser SpeechRecognitionResult)
 -> Value -> Parser SpeechRecognitionResult)
-> (Object -> Parser SpeechRecognitionResult)
-> Value
-> Parser SpeechRecognitionResult
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
text_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"text"
        SpeechRecognitionResult -> Parser SpeechRecognitionResult
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SpeechRecognitionResult -> Parser SpeechRecognitionResult)
-> SpeechRecognitionResult -> Parser SpeechRecognitionResult
forall a b. (a -> b) -> a -> b
$ SpeechRecognitionResultText
          { text :: Maybe Text
text = Maybe Text
text_
          }
      parseSpeechRecognitionResultError :: A.Value -> AT.Parser SpeechRecognitionResult
      parseSpeechRecognitionResultError :: Value -> Parser SpeechRecognitionResult
parseSpeechRecognitionResultError = String
-> (Object -> Parser SpeechRecognitionResult)
-> Value
-> Parser SpeechRecognitionResult
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"SpeechRecognitionResultError" ((Object -> Parser SpeechRecognitionResult)
 -> Value -> Parser SpeechRecognitionResult)
-> (Object -> Parser SpeechRecognitionResult)
-> Value
-> Parser SpeechRecognitionResult
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Error
_error_ <- Object
o Object -> Key -> Parser (Maybe Error)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"error"
        SpeechRecognitionResult -> Parser SpeechRecognitionResult
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SpeechRecognitionResult -> Parser SpeechRecognitionResult)
-> SpeechRecognitionResult -> Parser SpeechRecognitionResult
forall a b. (a -> b) -> a -> b
$ SpeechRecognitionResultError
          { _error :: Maybe Error
_error = Maybe Error
_error_
          }
  parseJSON Value
_ = Parser SpeechRecognitionResult
forall a. Monoid a => a
mempty