import type { PaymentStackScreenComponentProps } from "@/navigations/Types";
import BeasyLogoIcon from "@components/BeasyLogoIcon";
import Button from "@components/Button";
import GoBackIconButton from "@components/GoBackIconButton";
import Input from "@components/Input";
import PaymentOption from "@components/PaymentOption";
import BackgroundDefault from "@components/backgrounds/BackgroundDefault";
import Box from "@components/bases/Box";
import Text from "@components/bases/Text";
import useOrangeMoney from "@hooks/useOrangeMoney";
import { useCallback, useState } from "react";
import { Keyboard, View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";

const PaymentAmountInputScreen: PaymentStackScreenComponentProps<"paymentAmountInputScreen"> = ({
	route,
	navigation,
}) => {
	const { paymentType } = route.params;
	const [amountToPay, setAmountToPay] = useState(0);

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

	const insets = useSafeAreaInsets();

	console.log("isWaitingForPaymentUrl", isWaitingForOmPaymentUrl);
	console.log("isBrowserOpen", 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();
				console.log("OM so we stays on screen !!");
				console.log("Calling api with amount :: ", amountToPay);

				await orangeTransactionInitializerMutation.mutateAsync(amountToPay);
				console.log(
					"Browser got closed !! ",
					orangeTransactionInitializerMutation.isSuccess,
				);
				if (orangeTransactionInitializerMutation.isSuccess) {
					console.log("initilization was a success");
					await transactionsStatusMutation.mutateAsync(
						orangeTransactionInitializerMutation.data.order_id,
					);
					console.log("result of transactions", transactionsStatusMutation.data);
				}

				// console.log("Response transaction", response);
				break;
			}
			default:
				navigation.navigate("numberAndOtpForPaymentScreen");
				break;
		}
	}, [
		amountToPay,
		orangeTransactionInitializerMutation,
		paymentType,
		navigation,
		transactionsStatusMutation,
	]);

	return (
		<BackgroundDefault>
			{/* <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"}>
						<Input
							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> */}
		</BackgroundDefault>
	);
};

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>
	);
};
