import ActionLink from '@/components/shared/ActionLink'
import Captcha from '@/components/shared/Captcha'
import TenantSelector from '@/components/shared/TenantSelector'
import Alert from '@/components/ui/Alert'
import Button from '@/components/ui/Button'
import { FormContainer, FormItem } from '@/components/ui/Form'
import Input from '@/components/ui/Input'
import { APP_NAME } from '@/constants/app.constant'
import { ROUTES_ENUM } from '@/routes/route.constant'
import { sendExtendLoginRequest } from '@/services/account.service'
import { store } from '@/store'
import { useLocalization } from '@/utils/hooks/useLocalization'
import useTimeOutMessage from '@/utils/hooks/useTimeOutMessage'
import { Field, Form, Formik } from 'formik'
import { useState } from 'react'
import { Helmet } from 'react-helmet'
import * as Yup from 'yup'

type ExtendLoginFormSchema = {
  email: string
  captchaResponse: string
}

const validationSchema = Yup.object().shape({
  email: Yup.string().required(),
  captchaResponse: Yup.string().required(),
})

const ExtendLogin = () => {
  const { userName } = store.getState().auth.user
  const disableSubmit = false
  const signInUrl = ROUTES_ENUM.authenticated.login

  const [emailSent, setEmailSent] = useState(false)

  const [message, setMessage] = useTimeOutMessage(10000)
  const { translate } = useLocalization()

  const onSendMail = async (
    values: ExtendLoginFormSchema,
    setSubmitting: (isSubmitting: boolean) => void,
  ) => {
    const { email, captchaResponse } = values
    setSubmitting(true)
    try {
      const resp = await sendExtendLoginRequest({ email, captchaResponse })
      if (resp.data) {
        setSubmitting(false)
        setEmailSent(true)
      }
    } catch (error: any) {
      setMessage(error?.response?.data || error.toString())
      setSubmitting(false)
    }
  }

  return emailSent ? (
    <>
      <Helmet
        titleTemplate={`%s | ${APP_NAME}`}
        title={translate('AbpAccount::' + 'Abp.Account.ExtendLogin')}
        defaultTitle={APP_NAME}
      ></Helmet>
      <div>
        <h3 className="mb-1">{translate('::Abp.Account.ExtendLogin.Title')}</h3>
        <p>{translate('::Abp.Account.ExtendLogin.Description')}</p>
        <div className="mt-4 text-center">
          <span>{translate('::Abp.Account.Backto')} </span>
          <ActionLink to={signInUrl}>{translate('::Abp.Account.SignIn')}</ActionLink>
        </div>{' '}
      </div>
    </>
  ) : (
    <div>
      <Helmet
        titleTemplate={`%s | ${APP_NAME}`}
        title={translate('::' + 'Abp.Account.ExtendLogin')}
        defaultTitle={APP_NAME}
      ></Helmet>
      <div className="mb-6">
        <h3 className="mb-1">{translate('::Abp.Account.ExtendLogin')}</h3>
      </div>
      {message && (
        <Alert showIcon className="mb-4" type="danger">
          {message}
        </Alert>
      )}
      <TenantSelector />
      <Formik
        initialValues={{
          email: userName,
          captchaResponse: '',
        }}
        validationSchema={validationSchema}
        onSubmit={(values, { setSubmitting }) => {
          if (!disableSubmit) {
            onSendMail(values, setSubmitting)
          } else {
            setSubmitting(false)
          }
        }}
      >
        {({ touched, errors, isSubmitting, setFieldValue }) => (
          <Form>
            <FormContainer>
              <div className={emailSent ? 'hidden' : ''}>
                <FormItem invalid={errors.email && touched.email} errorMessage={errors.email}>
                  <Field
                    type="email"
                    autoComplete="off"
                    name="email"
                    placeholder={translate('::Abp.Account.EmailAddress')}
                    component={Input}
                  />
                </FormItem>
              </div>
              <Captcha
                onError={() => setFieldValue('captchaResponse', '')}
                onExpire={() => setFieldValue('captchaResponse', '')}
                onSuccess={(token: string) => setFieldValue('captchaResponse', token)}
              />
              <Button block loading={isSubmitting} variant="solid" type="submit">
                {emailSent ? 'Resend Email' : 'Send Email'}
              </Button>
              <div className="mt-4 text-center">
                <span>{translate('::Abp.Account.Backto')} </span>
                <ActionLink to={signInUrl}>{translate('::Abp.Account.SignIn')}</ActionLink>
              </div>
            </FormContainer>
          </Form>
        )}
      </Formik>
    </div>
  )
}

export default ExtendLogin
