import { Head, useForm } from '@inertiajs/react';
import { FormEventHandler, useRef } from 'react';

import { Button } from '@admin/components/ui/button';
import { FormError } from '@admin/components/ui/form-error';
import { Input } from '@admin/components/ui/input';
import { Label } from '@admin/components/ui/label';
import { TextLink } from '@admin/components/ui/text-link';
import { formClassName } from '@admin/components/ui/field-styles';
import { PasswordStrengthIndicator } from '@admin/components/ui/forms/password-strength-indicator';
import AuthLayout from '@admin/layouts/auth-layout';

export default function RequiredPasswordChange() {
    const passwordInput = useRef<HTMLInputElement>(null);
    const currentPasswordInput = useRef<HTMLInputElement>(null);

    const { data, setData, post, processing, errors, reset } = useForm({
        current_password: '',
        password: '',
        password_confirmation: '',
    });

    const submit: FormEventHandler = (e) => {
        e.preventDefault();

        post(route('password.required.store'), {
            onError: (formErrors) => {
                if (formErrors.password) {
                    reset('password', 'password_confirmation');
                    passwordInput.current?.focus();
                }

                if (formErrors.current_password) {
                    reset('current_password');
                    currentPasswordInput.current?.focus();
                }
            },
        });
    };

    return (
        <AuthLayout
            title="Change your password"
            description="You signed in with a temporary password. Choose a new password before continuing."
        >
            <Head title="Change password" />

            <form onSubmit={submit} className={formClassName}>
                <div className="grid gap-y-4">
                    <div>
                        <Label htmlFor="current_password">Temporary password</Label>
                        <Input
                            id="current_password"
                            ref={currentPasswordInput}
                            type="password"
                            autoComplete="current-password"
                            value={data.current_password}
                            autoFocus
                            required
                            onChange={(e) => setData('current_password', e.target.value)}
                        />
                        <FormError message={errors.current_password} />
                    </div>

                    <div>
                        <Label htmlFor="password">New password</Label>
                        <Input
                            id="password"
                            ref={passwordInput}
                            type="password"
                            autoComplete="new-password"
                            value={data.password}
                            required
                            onChange={(e) => setData('password', e.target.value)}
                        />
                        <PasswordStrengthIndicator password={data.password} />
                        <FormError message={errors.password} />
                    </div>

                    <div>
                        <Label htmlFor="password_confirmation">Confirm new password</Label>
                        <Input
                            id="password_confirmation"
                            type="password"
                            autoComplete="new-password"
                            value={data.password_confirmation}
                            required
                            onChange={(e) => setData('password_confirmation', e.target.value)}
                        />
                        <FormError message={errors.password_confirmation} />
                    </div>

                    <Button type="submit" className="w-full" disabled={processing}>
                        {processing ? 'Updating…' : 'Update password'}
                    </Button>
                </div>
            </form>

            <p className="mt-5 text-center text-sm text-gray-600 dark:text-white/70">
                <TextLink href={route('logout')} method="post">
                    Sign out
                </TextLink>
            </p>
        </AuthLayout>
    );
}
