module TD.Data.ResendCodeReason
  (ResendCodeReason(..)) 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

-- | Describes the reason why a code needs to be re-sent
data ResendCodeReason
  = ResendCodeReasonUserRequest -- ^ The user requested to resend the code
  | ResendCodeReasonVerificationFailed -- ^ The code is re-sent, because device verification has failed
    { ResendCodeReason -> Maybe Text
error_message :: Maybe T.Text -- ^ Cause of the verification failure, for example, PLAY_SERVICES_NOT_AVAILABLE, APNS_RECEIVE_TIMEOUT, or APNS_INIT_FAILED
    }
  deriving (ResendCodeReason -> ResendCodeReason -> Bool
(ResendCodeReason -> ResendCodeReason -> Bool)
-> (ResendCodeReason -> ResendCodeReason -> Bool)
-> Eq ResendCodeReason
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ResendCodeReason -> ResendCodeReason -> Bool
== :: ResendCodeReason -> ResendCodeReason -> Bool
$c/= :: ResendCodeReason -> ResendCodeReason -> Bool
/= :: ResendCodeReason -> ResendCodeReason -> Bool
Eq, Int -> ResendCodeReason -> ShowS
[ResendCodeReason] -> ShowS
ResendCodeReason -> String
(Int -> ResendCodeReason -> ShowS)
-> (ResendCodeReason -> String)
-> ([ResendCodeReason] -> ShowS)
-> Show ResendCodeReason
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ResendCodeReason -> ShowS
showsPrec :: Int -> ResendCodeReason -> ShowS
$cshow :: ResendCodeReason -> String
show :: ResendCodeReason -> String
$cshowList :: [ResendCodeReason] -> ShowS
showList :: [ResendCodeReason] -> ShowS
Show)

instance I.ShortShow ResendCodeReason where
  shortShow :: ResendCodeReason -> String
shortShow ResendCodeReason
ResendCodeReasonUserRequest
      = String
"ResendCodeReasonUserRequest"
  shortShow ResendCodeReasonVerificationFailed
    { error_message :: ResendCodeReason -> Maybe Text
error_message = Maybe Text
error_message_
    }
      = String
"ResendCodeReasonVerificationFailed"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"error_message" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
error_message_
        ]

instance AT.FromJSON ResendCodeReason where
  parseJSON :: Value -> Parser ResendCodeReason
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
"resendCodeReasonUserRequest"        -> ResendCodeReason -> Parser ResendCodeReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ResendCodeReason
ResendCodeReasonUserRequest
      String
"resendCodeReasonVerificationFailed" -> Value -> Parser ResendCodeReason
parseResendCodeReasonVerificationFailed Value
v
      String
_                                    -> Parser ResendCodeReason
forall a. Monoid a => a
mempty
    
    where
      parseResendCodeReasonVerificationFailed :: A.Value -> AT.Parser ResendCodeReason
      parseResendCodeReasonVerificationFailed :: Value -> Parser ResendCodeReason
parseResendCodeReasonVerificationFailed = String
-> (Object -> Parser ResendCodeReason)
-> Value
-> Parser ResendCodeReason
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ResendCodeReasonVerificationFailed" ((Object -> Parser ResendCodeReason)
 -> Value -> Parser ResendCodeReason)
-> (Object -> Parser ResendCodeReason)
-> Value
-> Parser ResendCodeReason
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
error_message_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"error_message"
        ResendCodeReason -> Parser ResendCodeReason
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ResendCodeReason -> Parser ResendCodeReason)
-> ResendCodeReason -> Parser ResendCodeReason
forall a b. (a -> b) -> a -> b
$ ResendCodeReasonVerificationFailed
          { error_message :: Maybe Text
error_message = Maybe Text
error_message_
          }
  parseJSON Value
_ = Parser ResendCodeReason
forall a. Monoid a => a
mempty

instance AT.ToJSON ResendCodeReason where
  toJSON :: ResendCodeReason -> Value
toJSON ResendCodeReason
ResendCodeReasonUserRequest
      = [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
"resendCodeReasonUserRequest"
        ]
  toJSON ResendCodeReasonVerificationFailed
    { error_message :: ResendCodeReason -> Maybe Text
error_message = Maybe Text
error_message_
    }
      = [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
"resendCodeReasonVerificationFailed"
        , Key
"error_message" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
error_message_
        ]