module TD.Data.PhoneNumberCodeType
  (PhoneNumberCodeType(..)) 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 type of the request for which a code is sent to a phone number
data PhoneNumberCodeType
  = PhoneNumberCodeTypeChange -- ^ Checks ownership of a new phone number to change the user's authentication phone number; for official Android and iOS applications only
  | PhoneNumberCodeTypeVerify -- ^ Verifies ownership of a phone number to be added to the user's Telegram Passport
  | PhoneNumberCodeTypeConfirmOwnership -- ^ Confirms ownership of a phone number to prevent account deletion while handling links of the type internalLinkTypePhoneNumberConfirmation
    { PhoneNumberCodeType -> Maybe Text
hash :: Maybe T.Text -- ^ Hash value from the link
    }
  deriving (PhoneNumberCodeType -> PhoneNumberCodeType -> Bool
(PhoneNumberCodeType -> PhoneNumberCodeType -> Bool)
-> (PhoneNumberCodeType -> PhoneNumberCodeType -> Bool)
-> Eq PhoneNumberCodeType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PhoneNumberCodeType -> PhoneNumberCodeType -> Bool
== :: PhoneNumberCodeType -> PhoneNumberCodeType -> Bool
$c/= :: PhoneNumberCodeType -> PhoneNumberCodeType -> Bool
/= :: PhoneNumberCodeType -> PhoneNumberCodeType -> Bool
Eq, Int -> PhoneNumberCodeType -> ShowS
[PhoneNumberCodeType] -> ShowS
PhoneNumberCodeType -> String
(Int -> PhoneNumberCodeType -> ShowS)
-> (PhoneNumberCodeType -> String)
-> ([PhoneNumberCodeType] -> ShowS)
-> Show PhoneNumberCodeType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PhoneNumberCodeType -> ShowS
showsPrec :: Int -> PhoneNumberCodeType -> ShowS
$cshow :: PhoneNumberCodeType -> String
show :: PhoneNumberCodeType -> String
$cshowList :: [PhoneNumberCodeType] -> ShowS
showList :: [PhoneNumberCodeType] -> ShowS
Show)

instance I.ShortShow PhoneNumberCodeType where
  shortShow :: PhoneNumberCodeType -> String
shortShow PhoneNumberCodeType
PhoneNumberCodeTypeChange
      = String
"PhoneNumberCodeTypeChange"
  shortShow PhoneNumberCodeType
PhoneNumberCodeTypeVerify
      = String
"PhoneNumberCodeTypeVerify"
  shortShow PhoneNumberCodeTypeConfirmOwnership
    { hash :: PhoneNumberCodeType -> Maybe Text
hash = Maybe Text
hash_
    }
      = String
"PhoneNumberCodeTypeConfirmOwnership"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"hash" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
hash_
        ]

instance AT.FromJSON PhoneNumberCodeType where
  parseJSON :: Value -> Parser PhoneNumberCodeType
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
"phoneNumberCodeTypeChange"           -> PhoneNumberCodeType -> Parser PhoneNumberCodeType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PhoneNumberCodeType
PhoneNumberCodeTypeChange
      String
"phoneNumberCodeTypeVerify"           -> PhoneNumberCodeType -> Parser PhoneNumberCodeType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PhoneNumberCodeType
PhoneNumberCodeTypeVerify
      String
"phoneNumberCodeTypeConfirmOwnership" -> Value -> Parser PhoneNumberCodeType
parsePhoneNumberCodeTypeConfirmOwnership Value
v
      String
_                                     -> Parser PhoneNumberCodeType
forall a. Monoid a => a
mempty
    
    where
      parsePhoneNumberCodeTypeConfirmOwnership :: A.Value -> AT.Parser PhoneNumberCodeType
      parsePhoneNumberCodeTypeConfirmOwnership :: Value -> Parser PhoneNumberCodeType
parsePhoneNumberCodeTypeConfirmOwnership = String
-> (Object -> Parser PhoneNumberCodeType)
-> Value
-> Parser PhoneNumberCodeType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PhoneNumberCodeTypeConfirmOwnership" ((Object -> Parser PhoneNumberCodeType)
 -> Value -> Parser PhoneNumberCodeType)
-> (Object -> Parser PhoneNumberCodeType)
-> Value
-> Parser PhoneNumberCodeType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
hash_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"hash"
        PhoneNumberCodeType -> Parser PhoneNumberCodeType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PhoneNumberCodeType -> Parser PhoneNumberCodeType)
-> PhoneNumberCodeType -> Parser PhoneNumberCodeType
forall a b. (a -> b) -> a -> b
$ PhoneNumberCodeTypeConfirmOwnership
          { hash :: Maybe Text
hash = Maybe Text
hash_
          }
  parseJSON Value
_ = Parser PhoneNumberCodeType
forall a. Monoid a => a
mempty

instance AT.ToJSON PhoneNumberCodeType where
  toJSON :: PhoneNumberCodeType -> Value
toJSON PhoneNumberCodeType
PhoneNumberCodeTypeChange
      = [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
"phoneNumberCodeTypeChange"
        ]
  toJSON PhoneNumberCodeType
PhoneNumberCodeTypeVerify
      = [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
"phoneNumberCodeTypeVerify"
        ]
  toJSON PhoneNumberCodeTypeConfirmOwnership
    { hash :: PhoneNumberCodeType -> Maybe Text
hash = Maybe Text
hash_
    }
      = [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
"phoneNumberCodeTypeConfirmOwnership"
        , Key
"hash"  Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
hash_
        ]