import IndoteknikLogo from '@/images/logo.png'
import Alert from '@/core/components/elements/Alert/Alert'
import Image from 'next/image'
import Link from '@/core/components/elements/Link/Link'
import useRegister from '../hooks/useRegister'
import MobileView from '@/core/components/views/MobileView'
import BottomPopup from '@/core/components/elements/Popup/BottomPopup'
import PageContent from '@/lib/content/components/PageContent'
import ReCAPTCHA from 'react-google-recaptcha'
import { signIn, useSession } from 'next-auth/react'
import { useRouter } from 'next/router'
import { setCookie } from 'cookies-next'
import { useEffect } from 'react'
import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner'

const RegisterMobile = () => {
  const {
    handleChangeInput,
    handleSubmit,
    isLoading,
    isValid,
    alert,
    companyNameRef,
    fullnameRef,
    emailRef,
    passwordRef,
    recaptchaRef,
    tnd,
    setTnd
  } = useRegister()

  const { data: session } = useSession()
  const router = useRouter()
  
  const handleGoogle = async () => {
    await signIn('google', { callbackUrl: '/register?source=google' })
  }

  useEffect(() => {
    if(session){
      setCookie('auth', JSON.stringify(session?.odooUser))
      router.push('/')
    }
  },[session])

  if (router.query.source) {
    return (
      <BottomPopup active={true} close=''>
        <div className='leading-7 text-gray_r-12/80 flex justify-center'>Mohon Tunggu</div>
        <div className='container flex justify-center my-4'>
          <LogoSpinner width={48} height={48} />
        </div>
      </BottomPopup>
    )
  }
  return (
    <MobileView>
      <div className='p-6 pt-10 flex flex-col items-center min-h-screen'>
        <Link href='/'>
          <Image src={IndoteknikLogo} alt='Logo Indoteknik' width={150} height={50} />
        </Link>

        <h1 className='text-2xl mt-4 font-semibold'>Daftar Akun Indoteknik</h1>
        <h2 className='text-gray_r-11 font-normal mt-1 mb-4 text-center'>
          Buat akun sekarang lebih mudah dan terverifikasi
        </h2>

        {alert && (
          <Alert className='text-center' type={alert.type}>
            {alert.children}
          </Alert>
        )}

        <form className='w-full mt-6 flex flex-col gap-y-4' onSubmit={handleSubmit}>
          <div>
            <label htmlFor='companyName'>
              Nama Perusahaan <span className='text-gray_r-11'>(opsional)</span>
            </label>
            <input
              type='text'
              id='companyName'
              name='companyName'
              className='form-input w-full mt-3'
              ref={companyNameRef}
              onChange={handleChangeInput}
              placeholder='cth: INDOTEKNIK DOTCOM GEMILANG'
              autoCapitalize='true'
            />
          </div>

          <div>
            <label htmlFor='fullname'>Nama Lengkap</label>
            <input
              type='text'
              id='fullname'
              name='fullname'
              className='form-input w-full mt-3'
              ref={fullnameRef}
              onChange={handleChangeInput}
              placeholder='John Doe'
            />
          </div>
          <div>
            <label htmlFor='email'>Alamat Email</label>
            <input
              type='email'
              id='email'
              name='email'
              className='form-input w-full mt-3'
              ref={emailRef}
              onChange={handleChangeInput}
              placeholder='contoh@email.com'
            />
          </div>
          <div>
            <label htmlFor='password'>Kata Sandi</label>
            <input
              type='password'
              id='password'
              name='password'
              className='form-input w-full mt-3'
              ref={passwordRef}
              onChange={handleChangeInput}
              placeholder='••••••••••••'
            />
          </div>
          <ReCAPTCHA ref={recaptchaRef} sitekey={process.env.NEXT_PUBLIC_RECAPTCHA_GOOGLE} />
          <div className='flex items-center mt-4 '>
            <input
              type='checkbox'
              id='sdk'
              className='form-input flex items-start w-fit mr-2'
              required
            />
            <label className='inline'>
              Dengan ini saya menyetujui{' '}
              <span onClick={() => setTnd(true)} className='cursor-pointer text-danger-500'>
                syarat dan ketentuan
              </span>{' '}
              yang berlaku
            </label>
          </div>
          <button type='submit' className='btn-yellow w-full mt-2' disabled={!isValid || isLoading}>
            {!isLoading ? 'Daftar' : 'Loading...'}
          </button>
        </form>

        <div>
          <BottomPopup active={tnd} close={() => setTnd(false)}>
            <PageContent path='/register#tnd'></PageContent>
          </BottomPopup>
        </div>

        <div className='flex  items-center mt-3 mb-3'>
          <hr className='flex-1' />
          <p className='text-gray-400'>ATAU</p>
          <hr className='flex-1' />
        </div>

        <button
          type='submit'
          className='border border-gray-500 p-2 rounded-md hover:bg-gray-100  w-full mt-2 flex items-center justify-center gap-x-2'
          onClick={() => handleGoogle()}
        >
          <Image
            src='/images/icons8-google.svg'
            alt='google image'
            className='h-7 w-7'
            width={10}
            height={10}
          />
          <p>Daftar dengan Google</p>
        </button>

        <div className='text-gray_r-11 mt-4'>
          Sudah punya akun Indoteknik?{' '}
          <Link href='/login' className='inline'>
            Masuk
          </Link>
        </div>
      </div>
    </MobileView>
  )
}

export default RegisterMobile
