import { useModalsManagerContext } from "@/contexts/ModalsManagerContext";
import type { PaymentStackScreenComponentProps } from "@/navigations/Types";
import BeasyLogoIcon from "@components/BeasyLogoIcon";
import Button from "@components/Button";
import GoBackIconButton from "@components/GoBackIconButton";
import InputWithTopLabel from "@components/InputWithTopLabel";
import PaymentOption from "@components/PaymentOption";
import BeasyDefaultBackgroundWrapper from "@components/backgrounds/BeasyDefaultBackground";
import Box from "@components/bases/Box";
import Text from "@components/bases/Text";
import useOrangeMoney from "@hooks/useOrangeMoney";
import useWave from "@hooks/useWave";
import { LOG } from "@logger";
import { useCallback, useState } from "react";
import { Keyboard, View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";

const log = LOG.extend("PaymentAmountInputScreen");
const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountInputScreen"> = ({
	route,
	navigation,
}) => {
	log.debug("PaymentAmountInputScreen");
	const { paymentType } = route.params;
	const [amountToPay, setAmountToPay] = useState(0);
	const { showModal, closeModal } = useModalsManagerContext();

	const {
		orangeTransactionInitializerMutation,
		handlePaymentUsingBrowser,
		isBrowserOpen,
		isWaitingForOmPaymentUrl,
		isCheckingForTransactionStatus,
		transactionsStatusMutation,
		orangePaymentTransactionHandler,
	} = useOrangeMoney(navigation);

	const { waveTransactionHandler } = useWave(navigation);

	const insets = useSafeAreaInsets();

	log.debug({
		isWaitingForOmPaymentUrl,
		isCheckingForTransactionStatus,
		isBrowserOpen,
	});

	const updateAmountToPay = (amount: string) => {
		const amountParsed = Number.parseInt(amount);
		if (!Number.isNaN(amountParsed)) {
			return setAmountToPay(amountParsed);
		}

		return setAmountToPay(0);
	};

	const handlePaymentButton = useCallback(async () => {
		switch (paymentType) {
			case "OM": {
				Keyboard.dismiss();
				log.info("OM so we stays on screen !!");
				// await orangePaymentSequence();
				try {
					await orangePaymentTransactionHandler(amountToPay);
					// navigation.getParent()?.navigate("paymentResultScreen");
				} catch (error) {
					log.error("handlePaymentButton |", error);
				}

				break;
			}
			case "WAVE": {
				try {
					log.info("Wave so we stay on screen.");
					await waveTransactionHandler(amountToPay);
				} catch (error) {
					log.error("handlePaymentButton Wave|", error);
				}
				break;
			}
			default:
				log.info("Navigating to numberAndOtpForPaymentScreen");
				navigation.navigate("numberAndOtpForPaymentScreen");
				break;
		}
	}, [
		paymentType,
		navigation,
		amountToPay,
		orangePaymentTransactionHandler,
		waveTransactionHandler,
	]);

	return (
		<BeasyDefaultBackgroundWrapper>
			{/* <SafeAreaView> */}

			{transactionsStatusMutation.isPending && <LoadingScreen />}
			<Box
				style={{
					height: "100%",
					width: "100%",
					// marginTop: insets.top,
				}}
			>
				<Box
					px={"l"}
					flexDirection={"row"}
					justifyContent={"space-between"}
					alignItems={"center"}
					mb={"m"}
				>
					<BeasyLogoIcon />
					<GoBackIconButton onPress={() => navigation.goBack()} />
				</Box>
				{/* <Box height={150} alignItems={"center"} justifyContent={"center"}>
						<BalanceContainer balance={78000} label="Total des ventes" />
					</Box> */}
				<Box height={90} padding={"s"} paddingLeft={"l"}>
					<Text color={"black"}>Montant à payé</Text>
					<Text color={"black"} variant={"header"}>
						{amountToPay}
					</Text>
				</Box>
				<Box
					p={"l"}
					paddingTop={"xl"}
					backgroundColor={"white"}
					flex={1}
					borderTopLeftRadius={20}
					borderTopRightRadius={20}
				>
					<Box width={75} height={50} mb={"l"} borderRadius={10} overflow={"hidden"}>
						<PaymentOption onPress={() => {}} paymentMethod={paymentType} />
					</Box>
					<Box mb={"xl"}>
						<InputWithTopLabel
							label="Entrez le montant"
							autoFocus={true}
							keyboardType="numeric"
							onChangeText={(e) => updateAmountToPay(e)}
						/>
					</Box>
					<Button
						onPress={handlePaymentButton}
						variant={"full"}
						textVariants={"primary"}
						label={`${isWaitingForOmPaymentUrl ? "Chargement..." : "Payer"} `}
					/>
				</Box>
			</Box>
			{/* </SafeAreaView> */}
		</BeasyDefaultBackgroundWrapper>
	);
};

export default PaymentAmountInputScreen;

const LoadingScreen = () => {
	return (
		<View
			style={{
				flex: 1,
				width: "100%",
				height: "100%",
				justifyContent: "center",
				alignItems: "center",
				zIndex: 1000000000000,
				backgroundColor: "black",
				opacity: 0.5,
				position: "absolute",
			}}
		>
			<Text color={"primary"}>Verification du status de la transaction.</Text>
			<Text>Veuillez patienter</Text>
		</View>
	);
};
