import { useKeyring } from "../hooks"
import { Layout2 } from "@/components/Layout2"

import { Input } from "@/components/ui/input"
import { Button } from "@/components/ui/button"
import {
    Card,
    CardContent,
    CardHeader,
    CardTitle,
    CardDescription,
    CardFooter,
} from "@/components/ui/card"
import {
    Form,
    FormControl,
    FormField,
    FormItem,
    FormLabel,
} from "@/components/ui/form"
import { EyeIcon, EyeOffIcon, ArrowLeft } from "lucide-react"
import { useForm, SubmitHandler } from "react-hook-form"
import { useState } from "react"
import { zodResolver } from "@hookform/resolvers/zod"
import { z } from "zod"
import { cn } from "@/lib/utils"
import { useNavigate } from "react-router-dom"

const FormSchema = z.object({
    password: z.string({ required_error: "You must specify a password" }),
})

export const DeleteWallet = () => {
    const navigate = useNavigate()
    const { reset } = useKeyring()
    const [showPassword, setShowPassword] = useState<boolean>(false)

    const form = useForm<z.infer<typeof FormSchema>>({
        resolver: zodResolver(FormSchema),
    })

    const {
        control,
        handleSubmit,
        getValues,
        setError,
        formState: { errors, isSubmitting, isSubmitSuccessful },
    } = form

    const onSubmit: SubmitHandler<z.infer<typeof FormSchema>> = async ({ password }) => {
        try {
            await reset(password)
        } catch (e) {
            setError("password", { message: "Incorrect password provided" })
        }
    }

    const togglePasswordVisibility = () => {
        setShowPassword((prev) => !prev)
    }

    return (
        <Layout2>
            <Form {...form}>
                <form className="relative grow" onSubmit={handleSubmit(onSubmit)}>
                    <Card className="absolute flex flex-col flex-grow w-full max-w-md min-h-full shadow-none">
                        <CardHeader className="text-center">
                            <CardTitle className="mt-6 mb-6 text-2xl font-extrabold">
                                Delete Wallet
                                <br />
                            </CardTitle>
                            <CardDescription className="mt-2 text-sm">
                                YOU ARE TRYING TO DELETE YOUR WALLET! MAKE SURE YOUR UNDERSTAND
                                WHAT YOU ARE DOING. PLEASE CONFIRM YOUR ACTION WITH WALLET PASSWORD.
                            </CardDescription>
                        </CardHeader>
                        <CardContent className="content-center flex flex-col justify-center flex-grow">
                            <FormField
                                control={control}
                                name="password"
                                render={({ field, fieldState }) => (
                                    <FormItem className="relative grid w-full max-w-sm items-center gap-1.5 my-4">
                                        <div className="relative">
                                            <FormControl>
                                                <Input
                                                    id="password"
                                                    type={showPassword ? "text" : "password"}
                                                    disabled={isSubmitting}
                                                    placeholder="Enter your password"
                                                    {...field}
                                                />
                                            </FormControl>
                                            <button
                                                onClick={handleSubmit(togglePasswordVisibility)}
                                                className="absolute -translate-y-1/2 top-1/2 -translate-x-1/2 right-2"
                                            >
                                                {showPassword ? (
                                                    <EyeOffIcon className="w-5 h-5" />
                                                ) : (
                                                    <EyeIcon className="w-5 h-5" />
                                                )}
                                            </button>
                                        </div>
                                        {fieldState.invalid && fieldState.error?.message && (
                                            <span className="absolute top-1/2 pt-4 text-xs text-red-500">
                                                {fieldState.error?.message}
                                            </span>
                                        )}
                                    </FormItem>
                                )}
                            />
                        </CardContent>
                        <CardFooter className="flex flex-col space-y-4 justify-end">
                            <Button disabled={isSubmitting} type="submit" size="full" className={cn("text-lg py-3")} >
                                Delete Wallet
                            </Button>
                            <Button
                                disabled={isSubmitting}
                                variant="secondary"
                                size="full"
                                className={cn("text-lg py-3")}
                                onClick={() => navigate("/settings")}
                            >
                                <ArrowLeft
                                    className="w-5 h-5 mr-2"
                                    aria-hidden="true"
                                />
                                Back
                            </Button>
                        </CardFooter>
                    </Card>
                </form>
            </Form>
        </Layout2>
    )
}
