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

-- | Contains authentication data for an email address
data EmailAddressAuthentication
  = EmailAddressAuthenticationCode -- ^ An authentication code delivered to a user's email address
    { EmailAddressAuthentication -> Maybe Text
code :: Maybe T.Text -- ^ The code
    }
  | EmailAddressAuthenticationAppleId -- ^ An authentication token received through Apple ID
    { EmailAddressAuthentication -> Maybe Text
token :: Maybe T.Text -- ^ The token
    }
  | EmailAddressAuthenticationGoogleId -- ^ An authentication token received through Google ID
    { token :: Maybe T.Text -- ^ The token
    }
  deriving (EmailAddressAuthentication -> EmailAddressAuthentication -> Bool
(EmailAddressAuthentication -> EmailAddressAuthentication -> Bool)
-> (EmailAddressAuthentication
    -> EmailAddressAuthentication -> Bool)
-> Eq EmailAddressAuthentication
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EmailAddressAuthentication -> EmailAddressAuthentication -> Bool
== :: EmailAddressAuthentication -> EmailAddressAuthentication -> Bool
$c/= :: EmailAddressAuthentication -> EmailAddressAuthentication -> Bool
/= :: EmailAddressAuthentication -> EmailAddressAuthentication -> Bool
Eq, Int -> EmailAddressAuthentication -> ShowS
[EmailAddressAuthentication] -> ShowS
EmailAddressAuthentication -> String
(Int -> EmailAddressAuthentication -> ShowS)
-> (EmailAddressAuthentication -> String)
-> ([EmailAddressAuthentication] -> ShowS)
-> Show EmailAddressAuthentication
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EmailAddressAuthentication -> ShowS
showsPrec :: Int -> EmailAddressAuthentication -> ShowS
$cshow :: EmailAddressAuthentication -> String
show :: EmailAddressAuthentication -> String
$cshowList :: [EmailAddressAuthentication] -> ShowS
showList :: [EmailAddressAuthentication] -> ShowS
Show)

instance I.ShortShow EmailAddressAuthentication where
  shortShow :: EmailAddressAuthentication -> String
shortShow EmailAddressAuthenticationCode
    { code :: EmailAddressAuthentication -> Maybe Text
code = Maybe Text
code_
    }
      = String
"EmailAddressAuthenticationCode"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"code" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
code_
        ]
  shortShow EmailAddressAuthenticationAppleId
    { token :: EmailAddressAuthentication -> Maybe Text
token = Maybe Text
token_
    }
      = String
"EmailAddressAuthenticationAppleId"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"token" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
token_
        ]
  shortShow EmailAddressAuthenticationGoogleId
    { token :: EmailAddressAuthentication -> Maybe Text
token = Maybe Text
token_
    }
      = String
"EmailAddressAuthenticationGoogleId"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"token" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
token_
        ]

instance AT.FromJSON EmailAddressAuthentication where
  parseJSON :: Value -> Parser EmailAddressAuthentication
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
"emailAddressAuthenticationCode"     -> Value -> Parser EmailAddressAuthentication
parseEmailAddressAuthenticationCode Value
v
      String
"emailAddressAuthenticationAppleId"  -> Value -> Parser EmailAddressAuthentication
parseEmailAddressAuthenticationAppleId Value
v
      String
"emailAddressAuthenticationGoogleId" -> Value -> Parser EmailAddressAuthentication
parseEmailAddressAuthenticationGoogleId Value
v
      String
_                                    -> Parser EmailAddressAuthentication
forall a. Monoid a => a
mempty
    
    where
      parseEmailAddressAuthenticationCode :: A.Value -> AT.Parser EmailAddressAuthentication
      parseEmailAddressAuthenticationCode :: Value -> Parser EmailAddressAuthentication
parseEmailAddressAuthenticationCode = String
-> (Object -> Parser EmailAddressAuthentication)
-> Value
-> Parser EmailAddressAuthentication
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"EmailAddressAuthenticationCode" ((Object -> Parser EmailAddressAuthentication)
 -> Value -> Parser EmailAddressAuthentication)
-> (Object -> Parser EmailAddressAuthentication)
-> Value
-> Parser EmailAddressAuthentication
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
code_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"code"
        EmailAddressAuthentication -> Parser EmailAddressAuthentication
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EmailAddressAuthentication -> Parser EmailAddressAuthentication)
-> EmailAddressAuthentication -> Parser EmailAddressAuthentication
forall a b. (a -> b) -> a -> b
$ EmailAddressAuthenticationCode
          { code :: Maybe Text
code = Maybe Text
code_
          }
      parseEmailAddressAuthenticationAppleId :: A.Value -> AT.Parser EmailAddressAuthentication
      parseEmailAddressAuthenticationAppleId :: Value -> Parser EmailAddressAuthentication
parseEmailAddressAuthenticationAppleId = String
-> (Object -> Parser EmailAddressAuthentication)
-> Value
-> Parser EmailAddressAuthentication
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"EmailAddressAuthenticationAppleId" ((Object -> Parser EmailAddressAuthentication)
 -> Value -> Parser EmailAddressAuthentication)
-> (Object -> Parser EmailAddressAuthentication)
-> Value
-> Parser EmailAddressAuthentication
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
token_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"token"
        EmailAddressAuthentication -> Parser EmailAddressAuthentication
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EmailAddressAuthentication -> Parser EmailAddressAuthentication)
-> EmailAddressAuthentication -> Parser EmailAddressAuthentication
forall a b. (a -> b) -> a -> b
$ EmailAddressAuthenticationAppleId
          { token :: Maybe Text
token = Maybe Text
token_
          }
      parseEmailAddressAuthenticationGoogleId :: A.Value -> AT.Parser EmailAddressAuthentication
      parseEmailAddressAuthenticationGoogleId :: Value -> Parser EmailAddressAuthentication
parseEmailAddressAuthenticationGoogleId = String
-> (Object -> Parser EmailAddressAuthentication)
-> Value
-> Parser EmailAddressAuthentication
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"EmailAddressAuthenticationGoogleId" ((Object -> Parser EmailAddressAuthentication)
 -> Value -> Parser EmailAddressAuthentication)
-> (Object -> Parser EmailAddressAuthentication)
-> Value
-> Parser EmailAddressAuthentication
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
token_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"token"
        EmailAddressAuthentication -> Parser EmailAddressAuthentication
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EmailAddressAuthentication -> Parser EmailAddressAuthentication)
-> EmailAddressAuthentication -> Parser EmailAddressAuthentication
forall a b. (a -> b) -> a -> b
$ EmailAddressAuthenticationGoogleId
          { token :: Maybe Text
token = Maybe Text
token_
          }
  parseJSON Value
_ = Parser EmailAddressAuthentication
forall a. Monoid a => a
mempty

instance AT.ToJSON EmailAddressAuthentication where
  toJSON :: EmailAddressAuthentication -> Value
toJSON EmailAddressAuthenticationCode
    { code :: EmailAddressAuthentication -> Maybe Text
code = Maybe Text
code_
    }
      = [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
"emailAddressAuthenticationCode"
        , Key
"code"  Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
code_
        ]
  toJSON EmailAddressAuthenticationAppleId
    { token :: EmailAddressAuthentication -> Maybe Text
token = Maybe Text
token_
    }
      = [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
"emailAddressAuthenticationAppleId"
        , Key
"token" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
token_
        ]
  toJSON EmailAddressAuthenticationGoogleId
    { token :: EmailAddressAuthentication -> Maybe Text
token = Maybe Text
token_
    }
      = [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
"emailAddressAuthenticationGoogleId"
        , Key
"token" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
token_
        ]