import React, { FC } from 'react'
import { useDispatch } from 'react-redux'
import { Link } from 'react-router-dom'
import { faEnvelope, faIdCard, faUserShield } from '@fortawesome/free-solid-svg-icons'
import { checkUserAvailability } from '../actions/registration'
import { PRIVACY_OPTIONS } from '../constants'
import { useTheme } from '../hooks'

import CheckboxField from '../components/controls/checkbox-field'
import TextField from '../components/controls/text-field'
import PasswordField from '../components/controls/password-field'
import SelectField from '../components/controls/select-field'
import ImageField from '../components/controls/image-field'
import CoverImageField from '../components/controls/cover-image-field'
import ThemeField from '../components/controls/theme-field'

const CreateUserForm: FC = () => {
    const theme = useTheme()
    const dispatch = useDispatch()

    const checkAvailability = (value: string) => {
        if (value.length > 3) {
            dispatch(checkUserAvailability(value))
        }
    }

    return (
        <div>
            <TextField icon={faIdCard} name="user-id" label="Username" placeholder="Your Username/ID" onBlur={e => checkAvailability(e.target.value)} />
            <TextField name="user-name" label="Display Name" placeholder="Whatever you want to go by" />
            <TextField type="email" icon={faEnvelope} name="user-email" label="Email Address" placeholder="Your email address" />
            <PasswordField placeholder="Your new password" />
            <ThemeField name="user-theme" label="Color" />
            <ImageField name="user-image" label="Avatar" />
            <CoverImageField name="user-cover-image" />
            <SelectField name="user-privacy" label="Privacy" options={PRIVACY_OPTIONS} icon={faUserShield} />
            
            <CheckboxField name="user-requires-approval">
                Approve each Subscription request from other users.
            </CheckboxField>
            <br />

            <CheckboxField name="user-agree">
                I agree to the User <Link style={{ color: theme.secondary }} to="/terms">terms and conditions</Link>.
            </CheckboxField>
        </div>
    )
}

export default CreateUserForm
