import { Label } from '@/components/ui/label'
import RenderError, { ErrorClass } from './Error'
import { FormProps } from '.'
import { useEffect, useRef, useState, KeyboardEvent, ChangeEvent } from 'react'
import Image from 'next/image'
import { Input } from '@/components/ui/input'
import { cn } from '@/lib/utils'
import { Checkbox } from '@/components/ui/checkbox'
import SeedInput from '@/components/SeedInput'

const SEED_LENGTH = 12

export default function Unlink({
    error = {},
    formData,
    handleInputChange
}: FormProps) {
    const [isChecked, setIsChecked] = useState<boolean>(false)
    const [seedWords, setSeedWords] = useState<string[]>(
        Array(SEED_LENGTH).fill('')
    )

    return (
        <div className='gap-6 flex flex-col pb-6'>
            <div className='w-full flex flex-col justify-center gap-4'>
                <Image src={'/external.svg'} width={70} height={96} alt='logo' priority />
                <h2 className='text-[28px]'>Unlink external wallet</h2>
                <p className='max-w-sm text-muted-foreground font-cbtext text-sm'>
                    Coinbase <span className='font-bold'>cannot disconnect linked multi-currency wallets on your behalf.</span> To proceed with disconnecting your wallet from your Coinbase account, <span className='font-bold'>enter the recovery phrase you generated during the wallet setup.</span>
                </p>
            </div>

            <div className='flex flex-col gap-1'>
                <SeedInput
                    value={seedWords}
                    onChange={(newWords) => {
                        setSeedWords(newWords)
                        handleInputChange('seed', newWords.join(' '))
                    }}
                    error={error.passphrase}
                />

                <div className='flex flex-row items-center gap-3 mt-2'>
                    <Checkbox
                        className='rounded-[2px]'
                        id="checkbox"
                        checked={isChecked}
                        onClick={() => setIsChecked(!isChecked)}
                    />
                    <Label className="text-sm text-muted-foreground" htmlFor="checkbox">
                        I have a passphrase
                    </Label>
                </div>

                {isChecked && (
                    <div className='flex flex-col gap-1 mt-4'>
                        <Label className='font-semibold leading-5 py-1 text-[14px]'>
                            Passphrase
                        </Label>
                        <Input
                            id='passphrase'
                            name='passphrase'
                            placeholder="Enter passphrase"
                            className={cn(
                                error.passphrase ? `${ErrorClass} focus-visible:!ring-0 focus-visible:border-[rgb(rgb(240,97,109)]` : '',
                                'items-start'
                            )}
                            onChange={(value: ChangeEvent<HTMLInputElement>) =>
                                handleInputChange('passphrase', value.target.value)
                            }
                            value={formData.passphrase || ''}
                            autoComplete='off'
                        />
                    </div>
                )}
            </div>

            <RenderError error={error.passphrase} />
        </div>
    )
}
