import { FC, useCallback, useEffect, useRef, useState } from "react" import { TextStyle, View, ViewStyle } from "react-native" import { KeyboardAwareScrollView } from "react-native-keyboard-controller" import { Button } from "@/components/Button" import { Dialog } from "@/components/Dialog" import { Screen } from "@/components/Screen" import { Text } from "@/components/Text" import { TextField } from "@/components/TextField" import { useAuth } from "@/context/AuthContext" import { translate } from "@/i18n/translate" import { AppStackScreenProps } from "@/navigators/navigationTypes" import { useAppTheme } from "@/theme/context" import { $styles } from "@/theme/styles" import type { ThemedStyle } from "@/theme/types" import { s } from "@/utils/responsive" import { useHeader } from "@/utils/useHeader" const COUNTDOWN_SECONDS = 60 export const ChangeEmailScreen: FC> = function ChangeEmailScreen({ navigation }) { const { user, emailChangeStep, sendEmailCode, verifyCurrentEmail, bindNewEmail, resetEmailChangeStep, isLoading, error, clearError, } = useAuth() const { themed } = useAppTheme() // Step 1: Verify current email const [currentEmailCode, setCurrentEmailCode] = useState("") // Step 2: Bind new email const [newEmail, setNewEmail] = useState("") const [newEmailCode, setNewEmailCode] = useState("") const [newEmailCodeSent, setNewEmailCodeSent] = useState(false) const [localError, setLocalError] = useState("") const [successDialogVisible, setSuccessDialogVisible] = useState(false) // Countdown timers const [currentEmailCountdown, setCurrentEmailCountdown] = useState(0) const [newEmailCountdown, setNewEmailCountdown] = useState(0) const currentEmailTimerRef = useRef | null>(null) const newEmailTimerRef = useRef | null>(null) // Start countdown for current email const startCurrentEmailCountdown = useCallback(() => { setCurrentEmailCountdown(COUNTDOWN_SECONDS) if (currentEmailTimerRef.current) { clearInterval(currentEmailTimerRef.current) } currentEmailTimerRef.current = setInterval(() => { setCurrentEmailCountdown((prev) => { if (prev <= 1) { if (currentEmailTimerRef.current) { clearInterval(currentEmailTimerRef.current) currentEmailTimerRef.current = null } return 0 } return prev - 1 }) }, 1000) }, []) // Start countdown for new email const startNewEmailCountdown = useCallback(() => { setNewEmailCountdown(COUNTDOWN_SECONDS) if (newEmailTimerRef.current) { clearInterval(newEmailTimerRef.current) } newEmailTimerRef.current = setInterval(() => { setNewEmailCountdown((prev) => { if (prev <= 1) { if (newEmailTimerRef.current) { clearInterval(newEmailTimerRef.current) newEmailTimerRef.current = null } return 0 } return prev - 1 }) }, 1000) }, []) // Reset state when unmounting useEffect(() => { return () => { resetEmailChangeStep() if (currentEmailTimerRef.current) { clearInterval(currentEmailTimerRef.current) } if (newEmailTimerRef.current) { clearInterval(newEmailTimerRef.current) } } }, [resetEmailChangeStep]) // Step 1: Send verification code to current email const handleSendCurrentEmailCode = useCallback(async () => { clearError() setLocalError("") if (user?.email) { const success = await sendEmailCode(user.email) if (success) { startCurrentEmailCountdown() } } }, [sendEmailCode, clearError, user?.email, startCurrentEmailCountdown]) // Step 1: Verify current email code const handleVerifyCurrentEmail = useCallback(async () => { clearError() setLocalError("") if (!currentEmailCode) { setLocalError(translate("changeEmailScreen:codeRequired")) return } if (currentEmailCode.length !== 6) { setLocalError(translate("changeEmailScreen:codeInvalid")) return } await verifyCurrentEmail(currentEmailCode) }, [currentEmailCode, verifyCurrentEmail, clearError]) // Step 2: Send verification code to new email const handleSendNewEmailCode = useCallback(async () => { clearError() setLocalError("") if (!newEmail) { setLocalError(translate("changeEmailScreen:newEmailRequired")) return } // Basic email validation if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(newEmail)) { setLocalError(translate("changeEmailScreen:emailInvalid")) return } if (newEmail.toLowerCase() === user?.email?.toLowerCase()) { setLocalError(translate("changeEmailScreen:sameEmail")) return } const success = await sendEmailCode(newEmail) if (success) { setNewEmailCodeSent(true) startNewEmailCountdown() } }, [newEmail, user?.email, sendEmailCode, clearError, startNewEmailCountdown]) // Step 2: Bind new email const handleBindNewEmail = useCallback(async () => { clearError() setLocalError("") if (!newEmailCode) { setLocalError(translate("changeEmailScreen:codeRequired")) return } if (newEmailCode.length !== 6) { setLocalError(translate("changeEmailScreen:codeInvalid")) return } const success = await bindNewEmail(newEmail, newEmailCode) if (success) { setSuccessDialogVisible(true) } }, [newEmail, newEmailCode, bindNewEmail, clearError]) const displayError = localError || error // Render Step 1 content (without buttons) const renderStep1Content = () => ( {translate("changeEmailScreen:currentEmail")} {user?.email || ""} {emailChangeStep !== "idle" && ( <> {displayError ? ( {displayError} ) : null} )} ) // Render Step 2 content (without buttons) const renderStep2Content = () => ( {!newEmailCodeSent ? ( displayError ? ( {displayError} ) : null ) : ( <> {displayError ? ( {displayError} ) : null} )} ) // Render bottom buttons for Step 1 const renderStep1Buttons = () => { if (emailChangeStep === "idle") { return (