import React, { useEffect } from 'react'
import PropTypes from 'prop-types'
import { Formik, Form } from 'formik'
import { Persist } from 'formik-persist'
import * as Yup from 'yup'
import FormikControl from '../FormikControls'
import ButtonPrimary from '../controls/ButtonPrimary'
import ButtonSecondary from '../controls/ButtonSecondary'
import Link from '../controls/Link'
import { FormRegistration } from './FormRegistration'

export const FormLogin = (props) => {
    const { setPopupTitle, setPopupContent, setPopupOpened, setPopupBack, close } = props

    useEffect(() => {
        if (setPopupTitle) { setPopupTitle('Enter into restricted area') }
    })

    const initialValues = {
        login: '',
        password: '',
        remember_me: false,
    }

    const validationSchema = Yup.object({
        email: Yup.string()
            .required('Required'),
        password: Yup.string()
            .required('Required'),
    })

    const onSubmit = async values => {
        console.log('Form data', values)
    }

    const goRegistration = () => {
        setPopupOpened(false)
        setTimeout(() => {
            setPopupContent(<FormRegistration setPopupTitle={setPopupTitle} setPopupContent={setPopupContent} setPopupOpened={setPopupOpened} setPopupBack={setPopupBack} close={close} />)
            setPopupOpened(true)
        }, 200);
    }

    const goRestorePassword = () => {
        setPopupOpened(false)
        setTimeout(() => {
            setPopupContent(<FormRegistration setPopupTitle={setPopupTitle} setPopupContent={setPopupContent} setPopupOpened={setPopupOpened} setPopupBack={setPopupBack} close={close} />)
            setPopupOpened(true)
        }, 200);
    }

    return (
        <Formik
            initialValues={initialValues}
            validationSchema={validationSchema}
            onSubmit={onSubmit}
        >
            {formik => {
                return (
                    <div className="overflow-y-auto max-h-screen-75">
                        <Form>
                            <div className="p-3">
                                <FormikControl
                                    control='string'
                                    name='email'
                                    label='Email'
                                    placeholder='Enter email'
                                />
                                <FormikControl
                                    control='password'
                                    name='password'
                                    label='Password'
                                    placeholder='Enter password'
                                />
                                <div className="flex xs:flex-row justify-between items-center xs:items-center">
                                    <div>
                                        <FormikControl
                                            control='checkbox'
                                            name='remember_me'
                                            label='Remember me'
                                        />
                                    </div>
                                    <Link action={goRestorePassword} className="block px-2 mt-6 mb-4 leading-8 text-lg">Forgot password?</Link>
                                </div>
                            </div>
                            <div className="px-2 pt-4 pb-1 mb-0 bg-primary-200 border-t sm:rounded-b-lg">
                                <ButtonPrimary type="submit" text="Enter" disabled={!formik.isValid || !formik.dirty} />
                                <div className="my-3">
                                    <ButtonSecondary type="button" text="Have no account? - Create" action={goRegistration} />
                                </div>
                            </div>
                            <Persist name="login-form" isSessionStorage />
                        </Form>
                    </div>
                )
            }}
        </Formik>
    )
}

FormLogin.propTypes = {
    setPopupTitle: PropTypes.func,
    setPopupContent: PropTypes.func,
    setPopupOpened: PropTypes.func,
    setPopupBack: PropTypes.func,
    close: PropTypes.func,
}